2023-09-29 16:01:27 +02:00
|
|
|
// __SDEFILE__ - This file is a dependency for the stand-alone environment
|
2023-05-26 15:54:43 +02:00
|
|
|
// Dependencies
|
2023-09-27 12:04:26 +02:00
|
|
|
import {
|
|
|
|
workbenchHash,
|
|
|
|
capitalize,
|
|
|
|
shortDate,
|
|
|
|
notEmpty,
|
|
|
|
horFlexClassesNoSm,
|
|
|
|
} from 'shared/utils.mjs'
|
2023-08-28 16:23:50 +02:00
|
|
|
import yaml from 'js-yaml'
|
2023-09-04 08:40:05 +02:00
|
|
|
// Context
|
|
|
|
import { LoadingStatusContext } from 'shared/context/loading-status-context.mjs'
|
2023-05-26 15:54:43 +02:00
|
|
|
// Hooks
|
2023-09-04 12:18:52 +02:00
|
|
|
import { useState, useContext } from 'react'
|
2023-05-26 15:54:43 +02:00
|
|
|
import { useTranslation } from 'next-i18next'
|
|
|
|
import { useRouter } from 'next/router'
|
|
|
|
import { useBackend } from 'shared/hooks/use-backend.mjs'
|
|
|
|
// Components
|
2023-08-28 16:23:50 +02:00
|
|
|
import { AuthWrapper } from 'shared/components/wrappers/auth/index.mjs'
|
2023-09-27 12:04:26 +02:00
|
|
|
import { StringInput, MarkdownInput, ListInput } from 'shared/components/inputs.mjs'
|
|
|
|
import {
|
|
|
|
SaveIcon,
|
|
|
|
SaveAsIcon,
|
|
|
|
EditIcon,
|
|
|
|
PlusIcon,
|
|
|
|
BookmarkIcon,
|
|
|
|
ExportIcon,
|
|
|
|
} from 'shared/components/icons.mjs'
|
2023-08-28 16:23:50 +02:00
|
|
|
import { Popout } from 'shared/components/popout/index.mjs'
|
|
|
|
import { PageLink } from 'shared/components/link.mjs'
|
2023-10-09 11:42:16 +02:00
|
|
|
import { DynamicMdx } from 'shared/components/mdx/dynamic.mjs'
|
2023-05-26 15:54:43 +02:00
|
|
|
|
2023-08-28 16:23:50 +02:00
|
|
|
export const ns = ['workbench', 'status']
|
2023-05-26 15:54:43 +02:00
|
|
|
|
2023-09-27 12:04:26 +02:00
|
|
|
export const SaveView = ({ design, settings, setView, saveAs }) => {
|
2023-08-28 16:23:50 +02:00
|
|
|
// Hooks
|
2023-10-09 11:42:16 +02:00
|
|
|
const { t, i18n } = useTranslation(ns)
|
2023-08-28 16:23:50 +02:00
|
|
|
const backend = useBackend()
|
|
|
|
const router = useRouter()
|
2023-09-04 08:40:05 +02:00
|
|
|
const { setLoadingStatus } = useContext(LoadingStatusContext)
|
2023-05-26 15:54:43 +02:00
|
|
|
|
2023-05-31 15:32:54 +02:00
|
|
|
// State
|
2023-08-28 16:23:50 +02:00
|
|
|
const [name, setName] = useState(`${capitalize(design)} / ${shortDate(router.locale)}`)
|
|
|
|
const [withNotes, setWithNotes] = useState(false)
|
|
|
|
const [notes, setNotes] = useState('')
|
|
|
|
const [savedId, setSavedId] = useState()
|
|
|
|
const [bookmarkedId, setBookmarkedId] = useState()
|
2023-09-27 12:04:26 +02:00
|
|
|
const [editAfterSaveAs, setEditAfterSaveAs] = useState(true)
|
2023-08-28 16:23:50 +02:00
|
|
|
|
|
|
|
const addSettingsToNotes = () => {
|
|
|
|
setNotes(notes + '\n```yaml\n' + yaml.dump(settings) + '````')
|
2023-05-31 15:32:54 +02:00
|
|
|
}
|
|
|
|
|
2023-09-27 12:04:26 +02:00
|
|
|
const saveAsNewPattern = async () => {
|
2023-08-28 16:23:50 +02:00
|
|
|
setLoadingStatus([true, 'savingPattern'])
|
|
|
|
const patternData = { design, name, public: false, settings, data: {} }
|
|
|
|
if (withNotes) patternData.notes = notes
|
|
|
|
const result = await backend.createPattern(patternData)
|
2023-05-31 15:32:54 +02:00
|
|
|
if (result.success) {
|
2023-08-28 16:23:50 +02:00
|
|
|
const id = result.data.pattern.id
|
|
|
|
setLoadingStatus([
|
|
|
|
true,
|
|
|
|
<>
|
|
|
|
{t('status:patternSaved')} <small>[#{id}]</small>
|
|
|
|
</>,
|
|
|
|
true,
|
|
|
|
true,
|
|
|
|
])
|
2023-09-27 12:04:26 +02:00
|
|
|
router.push(
|
|
|
|
`/account/patterns/${id}` +
|
|
|
|
(editAfterSaveAs ? '/edit' + workbenchHash({ settings, view: 'draft' }) : '')
|
|
|
|
)
|
|
|
|
if (editAfterSaveAs) setView('draft')
|
|
|
|
} else setLoadingStatus([true, 'backendError', true, false])
|
|
|
|
}
|
|
|
|
|
|
|
|
const savePattern = async () => {
|
|
|
|
setLoadingStatus([true, 'savingPattern'])
|
|
|
|
const patternData = { design, name, public: false, settings, data: {} }
|
|
|
|
const result = await backend.updatePattern(saveAs.pattern, patternData)
|
|
|
|
if (result.success) {
|
|
|
|
setLoadingStatus([
|
|
|
|
true,
|
|
|
|
<>
|
|
|
|
{t('status:patternSaved')} <small>[#{saveAs.pattern}]</small>
|
|
|
|
</>,
|
|
|
|
true,
|
|
|
|
true,
|
|
|
|
])
|
|
|
|
setSavedId(saveAs.pattern)
|
2023-08-28 16:23:50 +02:00
|
|
|
} else setLoadingStatus([true, 'backendError', true, false])
|
2023-05-31 15:32:54 +02:00
|
|
|
}
|
|
|
|
|
2023-08-28 16:23:50 +02:00
|
|
|
const bookmarkPattern = async () => {
|
|
|
|
setLoadingStatus([true, 'creatingBookmark'])
|
|
|
|
const result = await backend.createBookmark({
|
|
|
|
type: 'pattern',
|
|
|
|
title: name,
|
|
|
|
url: window.location.pathname + window.location.search + window.location.hash,
|
|
|
|
})
|
2023-05-31 15:32:54 +02:00
|
|
|
if (result.success) {
|
2023-08-28 16:23:50 +02:00
|
|
|
const id = result.data.bookmark.id
|
|
|
|
setLoadingStatus([
|
|
|
|
true,
|
|
|
|
<>
|
|
|
|
{t('status:bookmarkCreated')} <small>[#{id}]</small>
|
|
|
|
</>,
|
|
|
|
true,
|
|
|
|
true,
|
|
|
|
])
|
|
|
|
setBookmarkedId(id)
|
|
|
|
} else setLoadingStatus([true, 'backendError', true, false])
|
2023-05-31 15:32:54 +02:00
|
|
|
}
|
|
|
|
|
2023-10-09 11:42:16 +02:00
|
|
|
const docs = {}
|
|
|
|
for (const field of ['name', 'notes', 'goto']) {
|
|
|
|
docs[field] = <DynamicMdx language={i18n.language} slug={`docs/site/patterns/${field}`} />
|
|
|
|
}
|
|
|
|
|
2023-05-31 15:32:54 +02:00
|
|
|
return (
|
2023-08-28 16:23:50 +02:00
|
|
|
<AuthWrapper>
|
|
|
|
<div className="m-auto mt-8 max-w-2xl px-4">
|
2023-09-27 12:04:26 +02:00
|
|
|
{saveAs && saveAs.pattern ? (
|
|
|
|
<>
|
|
|
|
<h2>{t('workbench:savePattern')}</h2>
|
|
|
|
{savedId && (
|
|
|
|
<Popout link>
|
|
|
|
<h5>{t('workbend:patternSaved')}</h5>
|
|
|
|
{t('workbench:see')}:{' '}
|
|
|
|
<PageLink
|
|
|
|
href={`/account/patterns/${savedId}`}
|
|
|
|
txt={`/account/patterns/${savedId}`}
|
|
|
|
/>
|
|
|
|
</Popout>
|
|
|
|
)}
|
|
|
|
<button
|
|
|
|
className={`${horFlexClassesNoSm} btn btn-primary btn-lg w-full mt-2 mb-8`}
|
|
|
|
onClick={savePattern}
|
|
|
|
>
|
|
|
|
<SaveIcon className="h-8 w-8" />
|
|
|
|
{t('workbench:savePattern')} #{saveAs.pattern}
|
|
|
|
</button>
|
|
|
|
</>
|
|
|
|
) : null}
|
|
|
|
<h2>{t('workbench:saveAsNewPattern')}</h2>
|
2023-08-28 16:23:50 +02:00
|
|
|
{bookmarkedId && (
|
|
|
|
<Popout link>
|
|
|
|
<h5>{t('workbench:patternBookmarkCreated')}</h5>
|
|
|
|
{t('workbench:see')}:{' '}
|
|
|
|
<PageLink
|
|
|
|
href={`/account/bookmarks/${bookmarkedId}`}
|
|
|
|
txt={`/account/bookmarks/${bookmarkedId}`}
|
|
|
|
/>
|
|
|
|
</Popout>
|
|
|
|
)}
|
|
|
|
<div className="mb-4">
|
|
|
|
<StringInput
|
|
|
|
label={t('workbench:name')}
|
|
|
|
current={name}
|
|
|
|
update={setName}
|
|
|
|
valid={notEmpty}
|
2023-10-09 11:42:16 +02:00
|
|
|
docs={docs.name}
|
2023-08-28 16:23:50 +02:00
|
|
|
/>
|
2023-09-27 12:04:26 +02:00
|
|
|
|
2023-08-28 16:23:50 +02:00
|
|
|
{withNotes ? (
|
2023-08-28 16:31:43 +02:00
|
|
|
<MarkdownInput
|
|
|
|
label={t('workbench:notes')}
|
|
|
|
current={notes}
|
|
|
|
update={setNotes}
|
2023-10-09 11:42:16 +02:00
|
|
|
docs={docs.notes}
|
2023-08-28 16:31:43 +02:00
|
|
|
/>
|
2023-08-28 16:23:50 +02:00
|
|
|
) : null}
|
|
|
|
</div>
|
2023-09-27 12:04:26 +02:00
|
|
|
<ListInput
|
|
|
|
update={setEditAfterSaveAs}
|
|
|
|
label={t('workbench:whereToGoAfterSaveAs')}
|
|
|
|
current={editAfterSaveAs}
|
2023-10-09 11:42:16 +02:00
|
|
|
docs={docs.goto}
|
2023-09-27 12:04:26 +02:00
|
|
|
list={[
|
|
|
|
{
|
|
|
|
val: true,
|
|
|
|
label: t('workbench:continueEditingTitle'),
|
|
|
|
desc: t('workbench:continueEditingDesc'),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
val: false,
|
|
|
|
label: t('workbench:goToPatternTitle'),
|
|
|
|
desc: t('workbench:goToPatternDesc'),
|
|
|
|
},
|
|
|
|
]}
|
|
|
|
/>
|
|
|
|
<div className="grid md:grid-cols-2 gap-2 mt-4 mb-8">
|
2023-08-28 16:23:50 +02:00
|
|
|
{withNotes ? (
|
|
|
|
<button
|
|
|
|
className={`${horFlexClassesNoSm} btn btn-primary btn-outline`}
|
|
|
|
onClick={addSettingsToNotes}
|
|
|
|
>
|
|
|
|
<PlusIcon />
|
|
|
|
{t('workbench:addSettingsToNotes')}
|
|
|
|
</button>
|
|
|
|
) : (
|
|
|
|
<button
|
|
|
|
className={`${horFlexClassesNoSm} btn btn-primary btn-outline`}
|
|
|
|
onClick={() => setWithNotes(true)}
|
|
|
|
>
|
|
|
|
<EditIcon />
|
|
|
|
{t('workbench:addNotes')}
|
|
|
|
</button>
|
|
|
|
)}
|
2023-09-27 12:04:26 +02:00
|
|
|
<button
|
|
|
|
className={`${horFlexClassesNoSm} btn btn-primary w-full`}
|
|
|
|
onClick={saveAsNewPattern}
|
|
|
|
>
|
|
|
|
<SaveAsIcon />
|
|
|
|
{t('workbench:saveAsNewPattern')}
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
<h2>
|
|
|
|
{t('workbench:bookmarkPattern')}
|
|
|
|
<span className="px-2">/</span>
|
|
|
|
{t('workbench:exportPattern')}
|
|
|
|
</h2>
|
|
|
|
<div className="grid md:grid-cols-2 gap-2 mt-4 mb-8">
|
2023-08-28 16:23:50 +02:00
|
|
|
<button
|
|
|
|
className={`${horFlexClassesNoSm} btn btn-primary btn-outline w-full`}
|
|
|
|
onClick={bookmarkPattern}
|
|
|
|
>
|
|
|
|
<BookmarkIcon />
|
|
|
|
{t('workbench:bookmarkPattern')}
|
|
|
|
</button>
|
2023-09-27 12:04:26 +02:00
|
|
|
<button
|
|
|
|
className={`${horFlexClassesNoSm} btn btn-primary btn-outline w-full`}
|
|
|
|
onClick={() => setView('export')}
|
|
|
|
>
|
|
|
|
<ExportIcon />
|
|
|
|
{t('workbench:exportPattern')}
|
|
|
|
</button>
|
2023-08-28 16:23:50 +02:00
|
|
|
</div>
|
2023-05-31 15:32:54 +02:00
|
|
|
</div>
|
2023-08-28 16:23:50 +02:00
|
|
|
</AuthWrapper>
|
2023-05-26 15:54:43 +02:00
|
|
|
)
|
|
|
|
}
|