Skip to content

Commit bb71e9c

Browse files
committed
refactor(hub): clarify panel state snapshots
1 parent 078fbc0 commit bb71e9c

3 files changed

Lines changed: 35 additions & 13 deletions

File tree

‎packages/hub-ui/src/client/state/context.ts‎

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { DevframeClientCommand, DevframeDockEntry, DevframeDockUserEntry, DevframeRpcClientFunctions, DevframeViewIframe } from '@devframes/hub'
1+
import type { DevframeClientCommand, DevframeDockEntry, DevframeDockPanelState, DevframeDockUserEntry, DevframeRpcClientFunctions, DevframeViewIframe } from '@devframes/hub'
22
import type { CommandsContext, DevframeClientContext, DevframeRpcClient, DockClientScriptContext, DockEntryState, DockPanelStorage, DockRegistration, DockRendererManifest, DocksContext, DockSessionStorage } from '@devframes/hub/client'
33
import type { SharedState } from 'devframe/utils/shared-state'
44
import type { WhenContext } from 'devframe/utils/when'
@@ -18,6 +18,26 @@ import { registerMainFrameDockActionHandler, triggerMainFrameDockAction, useIsDo
1818
import { executeSetupScript } from './setup-script'
1919

2020
const docksContextByRpc = new WeakMap<DevframeRpcClient, DocksContext>()
21+
22+
function createDockPanelState(
23+
visible: boolean,
24+
open: boolean,
25+
selectedDockId: string | null,
26+
): DevframeDockPanelState {
27+
let state: DevframeDockPanelState['state']
28+
if (!visible)
29+
state = 'hidden'
30+
else if (open)
31+
state = 'open'
32+
else
33+
state = 'closed'
34+
35+
const panelState: DevframeDockPanelState = { state }
36+
if (selectedDockId !== null)
37+
panelState.selectedDockId = selectedDockId
38+
return panelState
39+
}
40+
2141
export async function createDocksContext(
2242
clientType: 'embedded' | 'standalone',
2343
rpc: DevframeRpcClient,
@@ -671,10 +691,8 @@ export async function createDocksContext(
671691
([visible, open, currentSelectedDockId]) => {
672692
if (visible === undefined)
673693
return
674-
void reportDockPanelState(rpc, {
675-
state: visible ? (open ? 'open' : 'closed') : 'hidden',
676-
...(currentSelectedDockId !== null ? { selectedDockId: currentSelectedDockId } : {}),
677-
}).catch(() => {})
694+
const panelState = createDockPanelState(visible, open, currentSelectedDockId)
695+
void reportDockPanelState(rpc, panelState).catch(() => {})
678696
},
679697
{ immediate: true },
680698
)

‎packages/hub/src/client/host.ts‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import type {
1010
ClientScriptEntry,
1111
DevframeDockEntriesGrouped,
1212
DevframeDockEntry,
13+
DevframeDockPanelState,
1314
DevframeViewIframe,
1415
} from '../types/docks'
1516
import type { DevframeDocksUserSettings } from '../types/settings'
@@ -158,10 +159,7 @@ export async function createDevframeClientRuntime(
158159
}
159160

160161
const reportPanelState = (session: DockSessionStorage): void => {
161-
void reportDockPanelState(rpc, {
162-
state: session.open ? 'open' : 'closed',
163-
...(session.selectedDockId !== null ? { selectedDockId: session.selectedDockId } : {}),
164-
}).catch(() => {})
162+
void reportDockPanelState(rpc, createDockPanelState(session)).catch(() => {})
165163
}
166164
let panelStateReportPending = false
167165
const schedulePanelStateReport = (session: DockSessionStorage): void => {
@@ -618,6 +616,15 @@ function createPanelContext(
618616
}
619617
}
620618

619+
function createDockPanelState(session: DockSessionStorage): DevframeDockPanelState {
620+
const panelState: DevframeDockPanelState = {
621+
state: session.open ? 'open' : 'closed',
622+
}
623+
if (session.selectedDockId !== null)
624+
panelState.selectedDockId = session.selectedDockId
625+
return panelState
626+
}
627+
621628
function groupByCategory(entries: DevframeDockEntry[], categoryOrder: Record<string, number>): DevframeDockEntriesGrouped {
622629
// Index registered groups so a member whose `groupId` resolves takes its
623630
// OUTER bucket from the group's category, not its own (which becomes the

‎packages/hub/src/node/panel-state.ts‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,7 @@ export function updateDockPanelState(
2222
return
2323
}
2424

25-
const currentState: DevframeDockPanelState = {
26-
state: panelState.state,
27-
...(panelState.selectedDockId !== undefined ? { selectedDockId: panelState.selectedDockId } : {}),
28-
}
25+
const currentState: DevframeDockPanelState = { ...panelState }
2926
sessionStates.set(sessionId, currentState)
3027
const event: DevframeDockPanelStateEvent = previousState === undefined
3128
? { type: 'connected', sessionId, ...currentState }

0 commit comments

Comments
 (0)