-
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathuseLayoutActions.ts
More file actions
273 lines (245 loc) · 10 KB
/
useLayoutActions.ts
File metadata and controls
273 lines (245 loc) · 10 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/**
* Hook that coordinates across all panel/terminal stores to capture
* the current layout state and apply a saved layout.
*
* Sidebar widths are read/written via the same localStorage keys the
* `<SidebarRail>` component uses, so persisted widths survive page
* reloads. Panel open/close states apply immediately via Zustand;
* sidebar widths take effect on next panel toggle or page load (the
* SidebarRail reads them from localStorage on mount).
*/
import { useCallback } from "react";
import { Schema } from "effect";
import type { ProjectId, ThreadId } from "@okcode/contracts";
import { getLocalStorageItem, setLocalStorageItem } from "./useLocalStorage";
import { useCodeViewerStore } from "../codeViewerStore";
import { useDiffViewerStore } from "../diffViewerStore";
import { useSimulationViewerStore } from "../simulationViewerStore";
import { usePreviewStateStore } from "../previewStateStore";
import { selectThreadTerminalState, useTerminalStateStore } from "../terminalStateStore";
import {
useLayoutStore,
type LayoutPanel,
type LayoutSidebarWidths,
type SavedLayout,
} from "../layoutStore";
// ─── Sidebar width localStorage keys ───────────────────────────────
// These must match the storageKey values used by the <Sidebar> components
// in _chat.tsx and _chat.$threadId.tsx.
const SIDEBAR_WIDTH_KEYS = {
threadSidebar: "chat_thread_sidebar_width",
codeViewer: "chat_code_viewer_sidebar_width",
diffViewer: "chat_diff_viewer_sidebar_width",
simulation: "chat_simulation_sidebar_width",
} as const satisfies Record<keyof LayoutSidebarWidths, string>;
// ─── Helpers ────────────────────────────────────────────────────────
function generateLayoutId(): string {
if (typeof crypto !== "undefined" && typeof crypto.randomUUID === "function") {
return crypto.randomUUID();
}
// Fallback for older environments.
return `layout-${Date.now()}-${Math.random().toString(36).slice(2, 10)}`;
}
function readSidebarWidths(): LayoutSidebarWidths {
return {
threadSidebar: getLocalStorageItem(SIDEBAR_WIDTH_KEYS.threadSidebar, Schema.Finite),
codeViewer: getLocalStorageItem(SIDEBAR_WIDTH_KEYS.codeViewer, Schema.Finite),
diffViewer: getLocalStorageItem(SIDEBAR_WIDTH_KEYS.diffViewer, Schema.Finite),
simulation: getLocalStorageItem(SIDEBAR_WIDTH_KEYS.simulation, Schema.Finite),
};
}
function writeSidebarWidths(widths: LayoutSidebarWidths): void {
for (const [field, storageKey] of Object.entries(SIDEBAR_WIDTH_KEYS) as Array<
[keyof LayoutSidebarWidths, string]
>) {
const value = widths[field];
if (value !== null) {
setLocalStorageItem(storageKey, value, Schema.Finite);
}
}
}
/**
* Determine which panel is currently active based on all panel store states.
*/
function resolveActivePanel(
codeViewerOpen: boolean,
diffViewerOpen: boolean,
previewOpen: boolean,
simulationOpen: boolean,
): LayoutPanel {
if (codeViewerOpen) return "code-viewer";
if (diffViewerOpen) return "diff-viewer";
if (previewOpen) return "preview";
if (simulationOpen) return "simulation";
return "none";
}
// ─── Hook ───────────────────────────────────────────────────────────
export interface UseLayoutActionsResult {
/**
* Snapshot the current panel arrangement into a `SavedLayout` object.
* Does not persist it — call `saveCurrentAsLayout` for that.
*/
captureCurrentLayout: (
name: string,
threadId: ThreadId | null,
projectId: ProjectId | null,
) => SavedLayout;
/**
* Apply a saved layout by setting all panel stores to the saved state.
* Sidebar widths are written to localStorage and will take effect on
* next panel toggle or page load.
*/
applyLayout: (
layout: SavedLayout,
threadId: ThreadId | null,
projectId: ProjectId | null,
) => void;
/**
* Capture the current layout and save it to the layout store in one step.
* Returns the ID of the newly saved layout.
*/
saveCurrentAsLayout: (
name: string,
threadId: ThreadId | null,
projectId: ProjectId | null,
) => string;
/**
* Overwrite a saved layout with the current panel arrangement,
* preserving its name and ID.
*/
updateLayoutFromCurrent: (
layoutId: string,
threadId: ThreadId | null,
projectId: ProjectId | null,
) => void;
}
export function useLayoutActions(): UseLayoutActionsResult {
const saveLayoutToStore = useLayoutStore((s) => s.saveLayout);
const updateLayoutInStore = useLayoutStore((s) => s.updateLayout);
const captureCurrentLayout = useCallback(
(name: string, threadId: ThreadId | null, projectId: ProjectId | null): SavedLayout => {
const codeViewerOpen = useCodeViewerStore.getState().isOpen;
const diffState = useDiffViewerStore.getState();
const diffViewerOpen = diffState.isOpen;
const simulationOpen = useSimulationViewerStore.getState().isOpen;
const previewState = usePreviewStateStore.getState();
const previewOpen = projectId
? (previewState.openByProjectId[projectId] ?? false)
: false;
const terminalStoreState = useTerminalStateStore.getState();
const threadTerminal = threadId
? selectThreadTerminalState(terminalStoreState.terminalStateByThreadId, threadId)
: null;
const previewDock = projectId
? (previewState.dockByProjectId[projectId] ?? null)
: null;
const previewSize = projectId
? (previewState.sizeByProjectId[projectId] ?? null)
: null;
const now = Date.now();
return {
id: generateLayoutId(),
name: name.trim().slice(0, 128) || "Untitled Layout",
createdAt: now,
updatedAt: now,
activePanel: resolveActivePanel(codeViewerOpen, diffViewerOpen, previewOpen, simulationOpen),
terminalOpen: threadTerminal?.terminalOpen ?? false,
terminalHeight: threadTerminal?.terminalHeight ?? null,
sidebarWidths: readSidebarWidths(),
previewDock,
previewSize,
};
},
[],
);
const applyLayout = useCallback(
(layout: SavedLayout, threadId: ThreadId | null, projectId: ProjectId | null) => {
// ── 1. Close all right-side panels first ────────────────────
const codeViewerStore = useCodeViewerStore.getState();
const diffViewerStore = useDiffViewerStore.getState();
const simulationStore = useSimulationViewerStore.getState();
const previewStore = usePreviewStateStore.getState();
if (codeViewerStore.isOpen) codeViewerStore.close();
if (diffViewerStore.isOpen) diffViewerStore.close();
if (simulationStore.isOpen) simulationStore.close();
if (projectId && previewStore.openByProjectId[projectId]) {
previewStore.setProjectOpen(projectId, false);
}
// ── 2. Open the target panel ───────────────────────────────
switch (layout.activePanel) {
case "code-viewer":
useCodeViewerStore.getState().open();
break;
case "diff-viewer":
if (threadId) {
useDiffViewerStore.getState().openConversation(threadId);
}
break;
case "preview":
if (projectId) {
usePreviewStateStore.getState().setProjectOpen(projectId, true);
}
break;
case "simulation":
useSimulationViewerStore.getState().open();
break;
case "none":
default:
break;
}
// ── 3. Apply preview dock & size ───────────────────────────
if (projectId) {
if (layout.previewDock !== null) {
usePreviewStateStore.getState().setProjectDock(projectId, layout.previewDock);
}
if (layout.previewSize !== null) {
usePreviewStateStore.getState().setProjectSize(projectId, layout.previewSize);
}
}
// ── 4. Apply terminal state ────────────────────────────────
if (threadId) {
const terminalStore = useTerminalStateStore.getState();
terminalStore.setTerminalOpen(threadId, layout.terminalOpen);
if (layout.terminalHeight !== null) {
terminalStore.setTerminalHeight(threadId, layout.terminalHeight);
}
}
// ── 5. Apply sidebar widths to localStorage ────────────────
writeSidebarWidths(layout.sidebarWidths);
// ── 6. Mark this layout as active ──────────────────────────
useLayoutStore.getState().setActiveLayoutId(layout.id);
},
[],
);
const saveCurrentAsLayout = useCallback(
(name: string, threadId: ThreadId | null, projectId: ProjectId | null): string => {
const layout = captureCurrentLayout(name, threadId, projectId);
saveLayoutToStore(layout);
return layout.id;
},
[captureCurrentLayout, saveLayoutToStore],
);
const updateLayoutFromCurrent = useCallback(
(layoutId: string, threadId: ThreadId | null, projectId: ProjectId | null) => {
const layouts = useLayoutStore.getState().savedLayouts;
const existing = layouts.find((l) => l.id === layoutId);
if (!existing) return;
const snapshot = captureCurrentLayout(existing.name, threadId, projectId);
updateLayoutInStore(layoutId, {
activePanel: snapshot.activePanel,
terminalOpen: snapshot.terminalOpen,
terminalHeight: snapshot.terminalHeight,
sidebarWidths: snapshot.sidebarWidths,
previewDock: snapshot.previewDock,
previewSize: snapshot.previewSize,
});
},
[captureCurrentLayout, updateLayoutInStore],
);
return {
captureCurrentLayout,
applyLayout,
saveCurrentAsLayout,
updateLayoutFromCurrent,
};
}