Line data Source code
1 : /**
2 : * Tab State Management - Per-tab workflow pointers in sessionStorage.
3 : *
4 : * Uses api.clientId to scope pointers per browser tab.
5 : * Includes workspaceId for validation to prevent cross-workspace contamination.
6 : */
7 :
8 : import type {
9 : ActivePathPointer,
10 : OpenPathsPointer
11 : } from '@/platform/workflow/persistence/base/draftTypes'
12 : import { getWorkspaceId } from '@/platform/workflow/persistence/base/storageKeys'
13 : import {
14 : clearActivePath,
15 : readActivePath,
16 : readOpenPaths,
17 : writeActivePath,
18 : writeOpenPaths
19 : } from '@/platform/workflow/persistence/base/storageIO'
20 : import { api } from '@/scripts/api'
21 :
22 : /**
23 : * Gets the current client ID for browser tab identification.
24 : * Falls back to initialClientId if clientId is not yet set.
25 : */
26 14264 : function getClientId(): string | null {
27 14264 : return api.clientId ?? api.initialClientId ?? null
28 : }
29 :
30 : /**
31 : * Composable for managing per-tab workflow state in sessionStorage.
32 : */
33 2355 : export function useWorkflowTabState() {
34 : /**
35 : * Gets the active workflow path for the current tab.
36 : * Returns null if no pointer exists or workspaceId doesn't match.
37 : */
38 2299 : function getActivePath(): string | null {
39 2299 : const clientId = getClientId()
40 2299 : const workspaceId = getWorkspaceId()
41 :
42 : const pointer = readActivePath(clientId, workspaceId, api.initialClientId)
43 2299 : return pointer?.path ?? null
44 : }
45 :
46 : /**
47 : * Sets the active workflow path for the current tab.
48 : */
49 3976 : function setActivePath(path: string): void {
50 3976 : const clientId = getClientId()
51 3976 : const workspaceId = getWorkspaceId()
52 3976 : if (!clientId) return
53 :
54 : const pointer: ActivePathPointer = {
55 : workspaceId,
56 : path
57 : }
58 1718 : writeActivePath(clientId, pointer)
59 : }
60 :
61 : /** Forgets the active workflow, so nothing is restored on the next boot. */
62 3 : function clearActivePathPointer(): void {
63 3 : const clientId = getClientId()
64 3 : if (!clientId) return
65 :
66 0 : clearActivePath(clientId, getWorkspaceId())
67 : }
68 :
69 : /**
70 : * Gets the open workflow paths for the current tab.
71 : * Returns null if no pointer exists or workspaceId doesn't match.
72 : */
73 4708 : function getOpenPaths(): { paths: string[]; activeIndex: number } | null {
74 4708 : const clientId = getClientId()
75 4708 : const workspaceId = getWorkspaceId()
76 :
77 4708 : const pointer = readOpenPaths(clientId, workspaceId, api.initialClientId)
78 4708 : if (!pointer) return null
79 :
80 110 : return { paths: pointer.paths, activeIndex: pointer.activeIndex }
81 : }
82 :
83 : /**
84 : * Sets the open workflow paths for the current tab.
85 : */
86 3278 : function setOpenPaths(paths: string[], activeIndex: number): void {
87 3278 : const clientId = getClientId()
88 3278 : const workspaceId = getWorkspaceId()
89 3278 : if (!clientId) return
90 :
91 : const pointer: OpenPathsPointer = {
92 : workspaceId,
93 : paths,
94 : activeIndex
95 : }
96 3274 : writeOpenPaths(clientId, pointer)
97 : }
98 :
99 2355 : return {
100 : getActivePath,
101 : setActivePath,
102 : clearActivePathPointer,
103 : getOpenPaths,
104 : setOpenPaths
105 : }
106 : }
|