2022-01-22 17:55:03 +01:00
|
|
|
import { useState, useEffect } from 'react'
|
2022-01-25 10:39:30 +01:00
|
|
|
import useLocalStorage from 'shared/hooks/useLocalStorage.js'
|
2022-01-22 17:55:03 +01:00
|
|
|
import Layout from 'shared/components/layouts/default'
|
2022-01-25 12:39:29 +01:00
|
|
|
import Menu from 'shared/components/workbench/menu/index.js'
|
2022-01-25 11:22:48 +01:00
|
|
|
import Measurements, { Input } from 'shared/components/workbench/measurements/index.js'
|
|
|
|
import LabDraft from 'shared/components/workbench/draft/index.js'
|
2022-01-22 17:55:03 +01:00
|
|
|
import set from 'lodash.set'
|
2022-01-25 18:14:31 +01:00
|
|
|
import unset from 'lodash.unset'
|
2022-01-27 12:26:56 +01:00
|
|
|
import defaultSettings from 'shared/components/workbench/default-settings.js'
|
|
|
|
|
2022-01-22 17:55:03 +01:00
|
|
|
|
|
|
|
// Generates a default pattern gist to start from
|
2022-01-27 18:07:37 +01:00
|
|
|
const defaultGist = (pattern, locale='en') => {
|
2022-01-27 12:26:56 +01:00
|
|
|
const gist = {
|
2022-01-22 17:55:03 +01:00
|
|
|
design: pattern.config.name,
|
|
|
|
version: pattern.config.version,
|
2022-01-27 18:07:37 +01:00
|
|
|
...defaultSettings
|
2022-01-22 17:55:03 +01:00
|
|
|
}
|
2022-01-27 18:07:37 +01:00
|
|
|
if (locale) gist.locale = locale
|
2022-01-27 12:26:56 +01:00
|
|
|
|
|
|
|
return gist
|
|
|
|
}
|
2022-01-22 17:55:03 +01:00
|
|
|
|
|
|
|
const hasRequiredMeasurements = (pattern, gist) => {
|
|
|
|
for (const m of pattern.config.measurements) {
|
2022-01-25 10:29:47 +01:00
|
|
|
if (!gist?.measurements?.[m]) return false
|
2022-01-22 17:55:03 +01:00
|
|
|
}
|
2022-01-25 10:29:47 +01:00
|
|
|
|
|
|
|
return true
|
2022-01-22 17:55:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This component wraps the workbench and is in charge of
|
|
|
|
* keeping the mode & gist state, which will trickly down
|
|
|
|
* to all workbench subcomponents
|
|
|
|
*
|
|
|
|
* mode: What to display (draft, sample, measurements, ...)
|
|
|
|
* gist: The runtime pattern configuration
|
|
|
|
*/
|
|
|
|
const WorkbenchWrapper = ({ app, pattern }) => {
|
|
|
|
|
|
|
|
// State for display mode and gist
|
2022-01-25 16:01:56 +01:00
|
|
|
const [mode, setMode] = useState('measurements')
|
2022-01-27 18:07:37 +01:00
|
|
|
const [gist, setGist] = useLocalStorage('gist', defaultGist(pattern, app.locale))
|
2022-01-22 17:55:03 +01:00
|
|
|
|
|
|
|
// If we don't have the requiremed measurements,
|
|
|
|
// force mode to measurements
|
|
|
|
useEffect(() => {
|
|
|
|
if (
|
|
|
|
mode !== 'measurements'
|
|
|
|
&& !hasRequiredMeasurements(pattern, gist)
|
|
|
|
) setMode('measurements')
|
|
|
|
})
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Update gist method. See lodash.set
|
|
|
|
*/
|
|
|
|
const updateGist = (path, content) => {
|
|
|
|
const newGist = {...gist}
|
|
|
|
set(newGist, path, content)
|
|
|
|
setGist(newGist)
|
|
|
|
}
|
2022-01-25 18:14:31 +01:00
|
|
|
const unsetGist = (path) => {
|
|
|
|
const newGist = {...gist}
|
|
|
|
unset(newGist, path)
|
|
|
|
setGist(newGist)
|
|
|
|
}
|
|
|
|
|
2022-01-22 17:55:03 +01:00
|
|
|
|
|
|
|
// Required props for layout
|
|
|
|
const layoutProps = {
|
|
|
|
app: app,
|
|
|
|
noSearch: true,
|
|
|
|
workbench: true,
|
2022-01-25 18:14:31 +01:00
|
|
|
AltMenu: <Menu
|
|
|
|
app={app}
|
|
|
|
pattern={pattern}
|
|
|
|
mode={mode}
|
|
|
|
setMode={setMode}
|
|
|
|
gist={gist}
|
|
|
|
updateGist={updateGist}
|
|
|
|
unsetGist={unsetGist}
|
2022-01-28 13:39:07 +01:00
|
|
|
setGist={setGist}
|
2022-01-25 18:14:31 +01:00
|
|
|
/>
|
2022-01-22 17:55:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Layout {...layoutProps}>
|
|
|
|
{mode === 'measurements' && (
|
|
|
|
<Measurements
|
|
|
|
app={app}
|
|
|
|
pattern={pattern}
|
|
|
|
gist={gist}
|
|
|
|
updateGist={updateGist}
|
|
|
|
/>
|
|
|
|
)}
|
2022-01-25 11:22:48 +01:00
|
|
|
{mode === 'draft' && (
|
|
|
|
<LabDraft
|
|
|
|
app={app}
|
|
|
|
pattern={pattern}
|
|
|
|
gist={gist}
|
|
|
|
updateGist={updateGist}
|
|
|
|
/>
|
|
|
|
)}
|
2022-01-22 17:55:03 +01:00
|
|
|
</Layout>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default WorkbenchWrapper
|
|
|
|
|