// Hooks
import { useEffect, useState } from 'react'
import { useTranslation } from 'next-i18next'
import { useView } from 'shared/hooks/use-view.mjs'
import { useAccount } from 'shared/hooks/use-account.mjs'
import { useControlState } from 'shared/components/account/control.mjs'
// Dependencies
import { pluginTheme } from '@freesewing/plugin-theme'
import { pluginI18n } from '@freesewing/plugin-i18n'
import { objUpdate } from 'shared/utils.mjs'
// Components
import { WorkbenchHeader } from './header.mjs'
import { ErrorView } from 'shared/components/error/view.mjs'
import { ModalSpinner } from 'shared/components/modal/spinner.mjs'
// Views
import { DraftView, ns as draftNs } from 'shared/components/workbench/views/draft/index.mjs'
import { SaveView, ns as saveNs } from 'shared/components/workbench/views/save/index.mjs'
import { PrintView, ns as printNs } from 'shared/components/workbench/views/print/index.mjs'
import { CutView, ns as cutNs } from 'shared/components/workbench/views/cut/index.mjs'
import { EditView, ns as editNs } from './views/edit/index.mjs'
import { TestView, ns as testNs } from 'shared/components/workbench/views/test/index.mjs'
import { ExportView, ns as exportNs } from 'shared/components/workbench/views/exporting/index.mjs'
import { LogView, ns as logNs } from 'shared/components/workbench/views/logs/index.mjs'
import { InspectView, ns as inspectNs } from 'shared/components/workbench/views/inspect/index.mjs'
export const ns = [
'account',
'workbench',
...draftNs,
...saveNs,
...printNs,
...cutNs,
...editNs,
...testNs,
...exportNs,
...logNs,
]
const defaultUi = {
renderer: 'react',
}
const views = {
draft: DraftView,
print: PrintView,
cut: CutView,
export: ExportView,
edit: EditView,
test: TestView,
logs: LogView,
inspect: InspectView,
}
const draftViews = ['draft', 'inspect']
export const Workbench = ({ design, Design, baseSettings, DynamicDocs, from }) => {
// Hooks
const { t, i18n } = useTranslation(ns)
const { language } = i18n
const { account } = useAccount()
const controlState = useControlState()
// State
const [view, setView] = useView()
const [settings, setSettings] = useState({ ...baseSettings, embed: true })
const [ui, setUi] = useState(defaultUi)
const [error, setError] = useState(false)
// Effect
useEffect(() => {
// Force re-render when baseSettings changes. Required when they are loaded async.
setSettings({ ...baseSettings, embed: true })
}, [baseSettings])
// Helper methods for settings/ui updates
const update = {
settings: (path, val) => setSettings(objUpdate({ ...settings }, path, val)),
ui: (path, val) => setUi(objUpdate({ ...ui }, path, val)),
toggleSa: () => {
if (settings.sabool) {
const mm = settings.sa
setSettings(objUpdate({ ...settings }, ['sabool'], 0))
setSettings(objUpdate({ ...settings }, ['sa'], 0))
} else {
setSettings(objUpdate({ ...settings }, ['sabool'], 1))
setSettings(objUpdate({ ...settings }, ['sa'], settings.samm))
}
},
setControl: controlState.update,
}
// Don't bother without a Design
if (!Design || !baseSettings) return