Re-get the openPanes from localStorage when navigating between projects (#3241)

* Re-get the openPanes from localStorage when navigating between projects

* fmt
This commit is contained in:
Frank Noirot
2024-08-02 15:39:05 -04:00
committed by GitHub
parent 9dcc955760
commit 874d19cbfd
2 changed files with 49 additions and 32 deletions

View File

@ -1,5 +1,5 @@
import { useMachine } from '@xstate/react' import { useMachine } from '@xstate/react'
import React, { createContext, useEffect, useRef } from 'react' import React, { createContext, useEffect, useMemo, useRef } from 'react'
import { import {
AnyStateMachine, AnyStateMachine,
ContextFrom, ContextFrom,
@ -8,7 +8,12 @@ import {
StateFrom, StateFrom,
assign, assign,
} from 'xstate' } from 'xstate'
import { SetSelections, modelingMachine } from 'machines/modelingMachine' import {
SetSelections,
getPersistedContext,
modelingMachine,
modelingMachineDefaultContext,
} from 'machines/modelingMachine'
import { useSetupEngineManager } from 'hooks/useSetupEngineManager' import { useSetupEngineManager } from 'hooks/useSetupEngineManager'
import { useSettingsAuthContext } from 'hooks/useSettingsAuthContext' import { useSettingsAuthContext } from 'hooks/useSettingsAuthContext'
import { import {
@ -99,6 +104,7 @@ export const ModelingMachineProvider = ({
} = useSettingsAuthContext() } = useSettingsAuthContext()
const token = auth?.context?.token const token = auth?.context?.token
const streamRef = useRef<HTMLDivElement>(null) const streamRef = useRef<HTMLDivElement>(null)
const persistedContext = useMemo(() => getPersistedContext(), [])
let [searchParams] = useSearchParams() let [searchParams] = useSearchParams()
const pool = searchParams.get('pool') const pool = searchParams.get('pool')
@ -121,6 +127,13 @@ export const ModelingMachineProvider = ({
const [modelingState, modelingSend, modelingActor] = useMachine( const [modelingState, modelingSend, modelingActor] = useMachine(
modelingMachine, modelingMachine,
{ {
context: {
...modelingMachineDefaultContext,
store: {
...modelingMachineDefaultContext.store,
...persistedContext,
},
},
actions: { actions: {
'disable copilot': () => { 'disable copilot': () => {
editorManager.setCopilotEnabled(false) editorManager.setCopilotEnabled(false)

View File

@ -255,10 +255,39 @@ interface PersistedModelingContext {
type PersistedKeys = keyof PersistedModelingContext type PersistedKeys = keyof PersistedModelingContext
export const PersistedValues: PersistedKeys[] = ['openPanes'] export const PersistedValues: PersistedKeys[] = ['openPanes']
const persistedContext: Partial<PersistedModelingContext> = (typeof window !== export const getPersistedContext = (): Partial<PersistedModelingContext> => {
'undefined' && const c = (typeof window !== 'undefined' &&
JSON.parse(localStorage.getItem(PERSIST_MODELING_CONTEXT) || '{}')) || { JSON.parse(localStorage.getItem(PERSIST_MODELING_CONTEXT) || '{}')) || {
openPanes: ['code'], openPanes: ['code'],
}
return c
}
export const modelingMachineDefaultContext = {
tool: null as Models['SceneToolType_type'] | null,
selection: [] as string[],
selectionRanges: {
otherSelections: [],
codeBasedSelections: [],
} as Selections,
sketchDetails: {
sketchPathToNode: [],
zAxis: [0, 0, 1],
yAxis: [0, 1, 0],
origin: [0, 0, 0],
} as null | SketchDetails,
sketchPlaneId: '' as string,
sketchEnginePathId: '' as string,
moveDescs: [] as MoveDesc[],
mouseState: { type: 'idle' } as MouseState,
segmentOverlays: {} as SegmentOverlays,
segmentHoverMap: {} as { [pathToNodeString: string]: number },
store: {
buttonDownInStream: undefined,
didDragInStream: false,
streamDimensions: { streamWidth: 1280, streamHeight: 720 },
openPanes: getPersistedContext().openPanes || ['code'],
} as Store,
} }
export const modelingMachine = createMachine( export const modelingMachine = createMachine(
@ -270,32 +299,7 @@ export const modelingMachine = createMachine(
predictableActionArguments: true, predictableActionArguments: true,
preserveActionOrder: true, preserveActionOrder: true,
context: { context: modelingMachineDefaultContext,
tool: null as Models['SceneToolType_type'] | null,
selection: [] as string[],
selectionRanges: {
otherSelections: [],
codeBasedSelections: [],
} as Selections,
sketchDetails: {
sketchPathToNode: [],
zAxis: [0, 0, 1],
yAxis: [0, 1, 0],
origin: [0, 0, 0],
} as null | SketchDetails,
sketchPlaneId: '' as string,
sketchEnginePathId: '' as string,
moveDescs: [] as MoveDesc[],
mouseState: { type: 'idle' } as MouseState,
segmentOverlays: {} as SegmentOverlays,
segmentHoverMap: {} as { [pathToNodeString: string]: number },
store: {
buttonDownInStream: undefined,
didDragInStream: false,
streamDimensions: { streamWidth: 1280, streamHeight: 720 },
openPanes: persistedContext.openPanes || ['code'],
} as Store,
},
schema: { schema: {
events: {} as ModelingMachineEvent, events: {} as ModelingMachineEvent,