-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseStore.ts
More file actions
346 lines (315 loc) · 11.6 KB
/
Copy pathuseStore.ts
File metadata and controls
346 lines (315 loc) · 11.6 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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
import { create } from 'zustand'
import { UnifiedSession, ToolFilter, StatusFilter, TerminalTab } from '../../shared/types'
import { Lang } from '../lib/i18n'
export interface ChatMessage {
role: 'user' | 'assistant'
type: 'text' | 'tool'
content: string
toolName?: string
timestamp: number | null
}
declare global {
interface Window {
api: {
getSessions: (filter: unknown) => Promise<UnifiedSession[]>
getMessagePreviews: (sessionId: string) => Promise<unknown[]>
getProjectNames: () => Promise<string[]>
getSessionCount: () => Promise<number>
getCounts: () => Promise<{ byTool: Record<string, number>; byProject: Record<string, number>; total: number; active: number; starred: number; pinned: number; archived: number }>
openInFinder: (projectName: string) => Promise<void>
copyProjectPath: (projectName: string) => Promise<void>
openInVscode: (projectName: string) => Promise<void>
listProjectFiles: (projectName: string) => Promise<Array<{ name: string; isDir: boolean }>>
toggleStar: (sessionId: string) => Promise<number>
toggleArchive: (sessionId: string) => Promise<number>
togglePin: (sessionId: string) => Promise<number>
deleteSession: (sessionId: string) => Promise<boolean>
updateTags: (sessionId: string, tags: string) => Promise<void>
refreshIndex: () => Promise<boolean>
getInstalledTerminals: () => Promise<string[]>
getSessionMessages: (session: UnifiedSession) => Promise<ChatMessage[]>
resumeSession: (session: UnifiedSession) => Promise<{ tabId: string; command: string }>
openSystemTerminal: (command: string, cwd?: string, terminalApp?: string) => Promise<boolean>
spawnTerminal: (cwd?: string) => Promise<string>
ptyWrite: (tabId: string, data: string) => void
ptyResize: (tabId: string, cols: number, rows: number) => void
ptyKill: (tabId: string) => Promise<void>
onPtyData: (cb: (data: { tabId: string; data: string }) => void) => () => void
onPtyExit: (cb: (data: { tabId: string; exitCode: number }) => void) => () => void
onIndexReady: (cb: () => void) => () => void
}
}
}
interface AppState {
sessions: UnifiedSession[]
projectNames: string[]
selectedTool: ToolFilter
selectedProject: string
selectedStatus: StatusFilter
searchQuery: string
detailSession: UnifiedSession | null
detailMessages: ChatMessage[]
detailLoading: boolean
counts: { byTool: Record<string, number>; byProject: Record<string, number>; total: number; active: number; starred: number; pinned: number; archived: number }
loading: boolean
_sessionReqId: number
sortBy: string
terminalTabs: TerminalTab[]
activeTabId: string | null
showTerminal: boolean
expandedProject: string | null
projectFiles: Record<string, Array<{ name: string; isDir: boolean }>>
pinnedProjects: string[]
terminalFullscreen: boolean
theme: 'dark' | 'light'
resumeAction: 'system' | 'builtin'
terminalApp: string
installedTerminals: string[]
showSettings: boolean
confirmDialog: { message: string; onConfirm: () => void } | null
language: Lang
loadSessions: () => Promise<void>
loadProjectNames: () => Promise<void>
setFilter: (key: string, value: string) => void
setSearch: (query: string) => void
openDetail: (session: UnifiedSession) => void
closeDetail: () => void
setSortBy: (sort: string) => void
toggleStar: (sessionId: string) => Promise<void>
toggleArchive: (sessionId: string) => Promise<void>
togglePin: (sessionId: string) => Promise<void>
deleteSession: (sessionId: string) => Promise<void>
resumeSession: (session: UnifiedSession) => Promise<void>
spawnTerminal: () => Promise<void>
closeTab: (tabId: string) => Promise<void>
setActiveTab: (tabId: string) => void
toggleProjectExpand: (projectName: string) => void
toggleProjectPin: (projectName: string) => void
setTerminalFullscreen: (v: boolean) => void
toggleTheme: () => void
setResumeAction: (v: 'system' | 'builtin') => void
setTerminalApp: (v: string) => void
loadInstalledTerminals: () => Promise<void>
setShowSettings: (v: boolean) => void
setConfirmDialog: (v: { message: string; onConfirm: () => void } | null) => void
confirm: (message: string) => Promise<boolean>
setLanguage: (v: Lang) => void
}
export const useStore = create<AppState>((set, get) => ({
sessions: [],
projectNames: [],
selectedTool: 'all',
selectedProject: 'all',
selectedStatus: 'all',
searchQuery: '',
detailSession: null,
detailMessages: [],
detailLoading: false,
counts: { byTool: {}, byProject: {}, total: 0, active: 0, starred: 0, pinned: 0, archived: 0 },
loading: false,
_sessionReqId: 0,
sortBy: 'updatedAt',
terminalTabs: [],
activeTabId: null,
showTerminal: false,
expandedProject: null,
projectFiles: {},
pinnedProjects: (() => { try { return JSON.parse(localStorage.getItem('pinnedProjects') || '[]') } catch { return [] } })(),
terminalFullscreen: false,
theme: (localStorage.getItem('theme') as 'dark' | 'light') || 'dark',
resumeAction: (localStorage.getItem('resumeAction') as 'system' | 'builtin') || 'system',
terminalApp: localStorage.getItem('terminalApp') || '',
installedTerminals: [],
showSettings: false,
confirmDialog: null,
language: (localStorage.getItem('language') as Lang) || 'en',
toggleTheme: () => {
const next = get().theme === 'dark' ? 'light' : 'dark'
set({ theme: next })
localStorage.setItem('theme', next)
document.documentElement.setAttribute('data-theme', next)
},
loadSessions: async () => {
const reqId = get()._sessionReqId + 1
set({ loading: true, _sessionReqId: reqId })
const { selectedTool, selectedProject, selectedStatus, searchQuery } = get()
const sessions = await window.api.getSessions({
tool: selectedTool,
projectName: selectedProject,
status: selectedStatus,
search: searchQuery || undefined
})
if (get()._sessionReqId !== reqId) return
set({ sessions, loading: false })
},
loadProjectNames: async () => {
const names = await window.api.getProjectNames()
const counts = await window.api.getCounts()
set({ projectNames: names, counts: { ...counts, pinned: counts.pinned || 0, archived: counts.archived || 0 } })
},
setFilter: async (key: string, value: string) => {
set({ [key]: value } as Partial<AppState>)
get().loadSessions()
},
setSearch: (query: string) => {
set({ searchQuery: query })
},
openDetail: async (session: UnifiedSession) => {
set({ detailSession: session, detailMessages: [], detailLoading: true })
try {
const messages = await window.api.getSessionMessages(session)
if (get().detailSession?.id !== session.id) return
set({ detailMessages: messages, detailLoading: false })
} catch {
set({ detailLoading: false })
}
},
closeDetail: () => {
set({ detailSession: null, detailMessages: [] })
},
setSortBy: (sort: string) => {
set({ sortBy: sort })
},
toggleStar: async (sessionId: string) => {
await window.api.toggleStar(sessionId)
const sessions = get().sessions.map(s =>
s.id === sessionId ? { ...s, starred: s.starred ? 0 : 1 } : s
)
const detailSession = get().detailSession
set({
sessions,
detailSession: detailSession?.id === sessionId
? { ...detailSession, starred: detailSession.starred ? 0 : 1 }
: detailSession
})
},
toggleArchive: async (sessionId: string) => {
await window.api.toggleArchive(sessionId)
await get().loadSessions()
await get().loadProjectNames()
if (get().detailSession?.id === sessionId) {
set({ detailSession: null, detailMessages: [] })
}
},
togglePin: async (sessionId: string) => {
await window.api.togglePin(sessionId)
const sessions = get().sessions.map(s =>
s.id === sessionId ? { ...s, pinned: s.pinned ? 0 : 1 } : s
)
const detailSession = get().detailSession
set({
sessions,
detailSession: detailSession?.id === sessionId
? { ...detailSession, pinned: detailSession.pinned ? 0 : 1 }
: detailSession
})
await get().loadProjectNames()
},
deleteSession: async (sessionId: string) => {
await window.api.deleteSession(sessionId)
await get().loadSessions()
await get().loadProjectNames()
if (get().detailSession?.id === sessionId) {
set({ detailSession: null, detailMessages: [] })
}
},
resumeSession: async (session: UnifiedSession) => {
const { terminalTabs } = get()
const result = await window.api.resumeSession(session)
const newTab: TerminalTab = {
id: result.tabId,
sessionId: session.id,
tool: session.tool,
projectPath: session.projectPath || '~',
title: session.title || 'Terminal',
active: true
}
const updatedTabs = terminalTabs.map(t => ({ ...t, active: false })).concat(newTab)
set({ terminalTabs: updatedTabs, activeTabId: newTab.id, showTerminal: true })
},
spawnTerminal: async () => {
const { terminalTabs } = get()
const tabId = await window.api.spawnTerminal()
const newTab: TerminalTab = {
id: tabId,
sessionId: '',
tool: '',
projectPath: '~',
title: 'Terminal',
active: true
}
const updatedTabs = terminalTabs.map(t => ({ ...t, active: false })).concat(newTab)
set({ terminalTabs: updatedTabs, activeTabId: newTab.id, showTerminal: true })
},
closeTab: async (tabId: string) => {
await window.api.ptyKill(tabId)
const { terminalTabs, activeTabId } = get()
const newTabs = terminalTabs.filter(t => t.id !== tabId)
const newActive = activeTabId === tabId
? (newTabs.length > 0 ? newTabs[newTabs.length - 1].id : null)
: activeTabId
set({ terminalTabs: newTabs, activeTabId: newActive, showTerminal: newTabs.length > 0 })
},
setActiveTab: (tabId: string) => {
const { terminalTabs } = get()
set({
activeTabId: tabId,
terminalTabs: terminalTabs.map(t => ({ ...t, active: t.id === tabId }))
})
},
toggleProjectExpand: async (projectName: string) => {
const { expandedProject, projectFiles } = get()
if (expandedProject === projectName) {
set({ expandedProject: null })
return
}
if (!projectFiles[projectName]) {
const files = await window.api.listProjectFiles(projectName)
set({ projectFiles: { ...projectFiles, [projectName]: files } })
}
set({ expandedProject: projectName })
},
toggleProjectPin: (projectName: string) => {
const pinned = get().pinnedProjects
const next = pinned.includes(projectName)
? pinned.filter(n => n !== projectName)
: [...pinned, projectName]
set({ pinnedProjects: next })
localStorage.setItem('pinnedProjects', JSON.stringify(next))
},
setTerminalFullscreen: (v: boolean) => {
set({ terminalFullscreen: v })
},
setResumeAction: (v: 'system' | 'builtin') => {
set({ resumeAction: v })
localStorage.setItem('resumeAction', v)
},
setTerminalApp: (v: string) => {
set({ terminalApp: v })
localStorage.setItem('terminalApp', v)
},
loadInstalledTerminals: async () => {
const terminals = await window.api.getInstalledTerminals()
set({ installedTerminals: terminals })
},
setShowSettings: (v: boolean) => {
set({ showSettings: v })
},
setConfirmDialog: (v) => {
set({ confirmDialog: v })
},
confirm: (message: string) => {
return new Promise<boolean>((resolve) => {
set({
confirmDialog: {
message,
onConfirm: () => { set({ confirmDialog: null }); resolve(true) }
}
})
})
},
setLanguage: (v: Lang) => {
set({ language: v })
localStorage.setItem('language', v)
}
}))