1
0
Fork 0

simplify gist methods. set gist now sets the gist

This commit is contained in:
Enoch Riese 2022-06-24 12:59:08 -05:00
parent 94d59d44a2
commit 210c463a38
4 changed files with 9 additions and 19 deletions

View file

@ -9,6 +9,7 @@ const CoreSettingSaBool = props => {
const toggle = () => { const toggle = () => {
props.setGist({ props.setGist({
...props.gist,
saBool: !value, saBool: !value,
sa: value ? 0 : props.gist.saMm sa: value ? 0 : props.gist.saMm
}) })

View file

@ -15,6 +15,7 @@ const CoreSettingMm = props => {
setValue(newVal) setValue(newVal)
if (props.gist.sa) props.setGist({ if (props.gist.sa) props.setGist({
...props.gist,
saMm: newVal, saMm: newVal,
sa: newVal, sa: newVal,
}) })

View file

@ -67,11 +67,11 @@ const WorkbenchWrapper = ({ app, design, preload=false, from=false, layout=false
const doPreload = async () => { const doPreload = async () => {
if (preload && from && preloaders[from]) { if (preload && from && preloaders[from]) {
const g = await preloaders[from](preload, design) const g = await preloaders[from](preload, design)
setGist({...g.settings}) setGist({...gist, ...g.settings})
} }
} }
doPreload(); doPreload();
}, [preload, from]) }, [preload, from, gist])
// Helper methods to manage the gist state // Helper methods to manage the gist state
const updateWBGist = useMemo(() => (path, value, closeNav=false) => { const updateWBGist = useMemo(() => (path, value, closeNav=false) => {

View file

@ -19,18 +19,11 @@ const defaultGist = (design, locale='en') => {
// generate the gist state and its handlers // generate the gist state and its handlers
export function useGist(design, app) { export function useGist(design, app) {
// get the localstorage state and setter // get the localstorage state and setter
const [gist, _setGist, gistReady] = useLocalStorage(`${design.config.name}_gist`, defaultGist(design, app.locale)); const [gist, setGist, gistReady] = useLocalStorage(`${design.config.name}_gist`, defaultGist(design, app.locale));
/** apply new gist values onto existing gist */
const setGist = (newGist) => {
// setters that are passed a function always use the latest state value, so these don't need to be memoized
_setGist((gistState) => ({...gistState, ...newGist}))
}
/** update a single gist value */ /** update a single gist value */
const updateGist = (path, value) => { const updateGist = (path, value) => {
_setGist((gistState) => { setGist((gistState) => {
const newGist = {...gistState}; const newGist = {...gistState};
set(newGist, path, value); set(newGist, path, value);
return newGist; return newGist;
@ -39,22 +32,17 @@ export function useGist(design, app) {
/** unset a single gist value */ /** unset a single gist value */
const unsetGist = (path) => { const unsetGist = (path) => {
_setGist((gistState) => { setGist((gistState) => {
const newGist = {... gistState}; const newGist = {... gistState};
unset(newGist, path); unset(newGist, path);
return newGist; return newGist;
}) })
} }
/** replace the entire gist with the given gist */
const replaceGist = (newGist) => {
_setGist(newGist);
}
/** reset to the default gist */ /** reset to the default gist */
const clearGist = () => { const clearGist = () => {
replaceGist(defaultGist(design, gist.locale)) setGist(defaultGist(design, gist.locale))
} }
return {gist, setGist, unsetGist, replaceGist, clearGist, gistReady, updateGist}; return {gist, setGist, unsetGist, clearGist, gistReady, updateGist};
} }