-
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathrightPanelStore.ts
More file actions
69 lines (58 loc) · 1.67 KB
/
rightPanelStore.ts
File metadata and controls
69 lines (58 loc) · 1.67 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
import { create } from "zustand";
export type RightPanelTab = "workspace" | "diffs";
interface RightPanelState {
isOpen: boolean;
activeTab: RightPanelTab;
open: (tab?: RightPanelTab) => void;
close: () => void;
setActiveTab: (tab: RightPanelTab, open?: boolean) => void;
}
const STORAGE_KEY = "okcode:right-panel-tab:v1";
const VALID_TABS: readonly RightPanelTab[] = ["workspace", "diffs"];
export function normalizeRightPanelTab(value: string | null | undefined): RightPanelTab | null {
if (value === "files" || value === "editor") {
return "workspace";
}
if ((VALID_TABS as readonly string[]).includes(value ?? "")) {
return value as RightPanelTab;
}
return null;
}
function readPersistedTab(): RightPanelTab {
if (typeof window === "undefined") return "workspace";
try {
const raw = window.localStorage.getItem(STORAGE_KEY);
const normalized = normalizeRightPanelTab(raw);
if (normalized) {
return normalized;
}
} catch {
// ignore storage errors
}
return "workspace";
}
function persistTab(tab: RightPanelTab): void {
if (typeof window === "undefined") return;
try {
window.localStorage.setItem(STORAGE_KEY, tab);
} catch {
// ignore storage errors
}
}
export const useRightPanelStore = create<RightPanelState>((set) => ({
isOpen: false,
activeTab: readPersistedTab(),
open: (tab) => {
if (tab) {
persistTab(tab);
set({ isOpen: true, activeTab: tab });
} else {
set({ isOpen: true });
}
},
close: () => set({ isOpen: false }),
setActiveTab: (tab, open = true) => {
persistTab(tab);
set((state) => ({ activeTab: tab, isOpen: open ? true : state.isOpen }));
},
}));