Skip to content

Commit 82b027c

Browse files
committed
feat: persist tab store locally
1 parent 9ba1c73 commit 82b027c

File tree

1 file changed

+81
-67
lines changed

1 file changed

+81
-67
lines changed

src/renderer/stores/tabStore.ts

Lines changed: 81 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { TabState } from "@shared/types";
22
import { v4 as uuidv4 } from "uuid";
33
import { create } from "zustand";
4+
import { persist } from "zustand/middleware";
45

56
interface TabStore {
67
tabs: TabState[];
@@ -14,73 +15,86 @@ interface TabStore {
1415
}
1516

1617
// Create initial tabs
17-
const taskListTab: TabState = {
18-
id: uuidv4(),
19-
type: "task-list",
20-
title: "Tasks",
21-
};
18+
const createInitialTabs = (): [TabState, TabState] => {
19+
const taskListTab: TabState = {
20+
id: uuidv4(),
21+
type: "task-list",
22+
title: "Tasks",
23+
};
24+
25+
const workflowTab: TabState = {
26+
id: uuidv4(),
27+
type: "workflow",
28+
title: "Workflows",
29+
};
2230

23-
const workflowTab: TabState = {
24-
id: uuidv4(),
25-
type: "workflow",
26-
title: "Workflows",
31+
return [taskListTab, workflowTab];
2732
};
2833

29-
export const useTabStore = create<TabStore>((set, get) => ({
30-
tabs: [taskListTab, workflowTab],
31-
activeTabId: taskListTab.id,
32-
33-
createTab: (tabData) => {
34-
const newTab: TabState = {
35-
...tabData,
36-
id: uuidv4(),
37-
};
38-
39-
set((state) => ({
40-
tabs: [...state.tabs, newTab],
41-
activeTabId: newTab.id,
42-
}));
43-
},
44-
45-
closeTab: (tabId) => {
46-
const state = get();
47-
const tabIndex = state.tabs.findIndex((tab) => tab.id === tabId);
48-
49-
if (tabIndex === -1 || state.tabs.length === 1) return;
50-
51-
const newTabs = state.tabs.filter((tab) => tab.id !== tabId);
52-
let newActiveTabId = state.activeTabId;
53-
54-
if (state.activeTabId === tabId) {
55-
// Select the tab to the left, or the first tab if closing the leftmost
56-
const newIndex = Math.max(0, tabIndex - 1);
57-
newActiveTabId = newTabs[newIndex].id;
58-
}
59-
60-
set({
61-
tabs: newTabs,
62-
activeTabId: newActiveTabId,
63-
});
64-
},
65-
66-
setActiveTab: (tabId) => {
67-
set({ activeTabId: tabId });
68-
},
69-
70-
updateTabTitle: (tabId, title) => {
71-
set((state) => ({
72-
tabs: state.tabs.map((tab) =>
73-
tab.id === tabId ? { ...tab, title } : tab,
74-
),
75-
}));
76-
},
77-
78-
reorderTabs: (fromIndex, toIndex) => {
79-
set((state) => {
80-
const newTabs = [...state.tabs];
81-
const [movedTab] = newTabs.splice(fromIndex, 1);
82-
newTabs.splice(toIndex, 0, movedTab);
83-
return { tabs: newTabs };
84-
});
85-
},
86-
}));
34+
const [initialTaskListTab, initialWorkflowTab] = createInitialTabs();
35+
36+
export const useTabStore = create<TabStore>()(
37+
persist(
38+
(set, get) => ({
39+
tabs: [initialTaskListTab, initialWorkflowTab],
40+
activeTabId: initialTaskListTab.id,
41+
42+
createTab: (tabData) => {
43+
const newTab: TabState = {
44+
...tabData,
45+
id: uuidv4(),
46+
};
47+
48+
set((state) => ({
49+
tabs: [...state.tabs, newTab],
50+
activeTabId: newTab.id,
51+
}));
52+
},
53+
54+
closeTab: (tabId) => {
55+
const state = get();
56+
const tabIndex = state.tabs.findIndex((tab) => tab.id === tabId);
57+
58+
if (tabIndex === -1 || state.tabs.length === 1) return;
59+
60+
const newTabs = state.tabs.filter((tab) => tab.id !== tabId);
61+
let newActiveTabId = state.activeTabId;
62+
63+
if (state.activeTabId === tabId) {
64+
// Select the tab to the left, or the first tab if closing the leftmost
65+
const newIndex = Math.max(0, tabIndex - 1);
66+
newActiveTabId = newTabs[newIndex].id;
67+
}
68+
69+
set({
70+
tabs: newTabs,
71+
activeTabId: newActiveTabId,
72+
});
73+
},
74+
75+
setActiveTab: (tabId) => {
76+
set({ activeTabId: tabId });
77+
},
78+
79+
updateTabTitle: (tabId, title) => {
80+
set((state) => ({
81+
tabs: state.tabs.map((tab) =>
82+
tab.id === tabId ? { ...tab, title } : tab,
83+
),
84+
}));
85+
},
86+
87+
reorderTabs: (fromIndex, toIndex) => {
88+
set((state) => {
89+
const newTabs = [...state.tabs];
90+
const [movedTab] = newTabs.splice(fromIndex, 1);
91+
newTabs.splice(toIndex, 0, movedTab);
92+
return { tabs: newTabs };
93+
});
94+
},
95+
}),
96+
{
97+
name: "tab-store",
98+
},
99+
),
100+
);

0 commit comments

Comments
 (0)