-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSessionList.tsx
More file actions
412 lines (380 loc) · 16.2 KB
/
Copy pathSessionList.tsx
File metadata and controls
412 lines (380 loc) · 16.2 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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
import { memo, useMemo, useState, useCallback, useEffect, useRef } from 'react'
import { createPortal } from 'react-dom'
import { motion, AnimatePresence } from 'framer-motion'
import { GroupedVirtuoso } from 'react-virtuoso'
import { Pin, Star, Archive, Trash2, Play, Search, Flame, Eye } from 'lucide-react'
import { useStore } from '../stores/useStore'
import { ToolIcon } from './Sidebar'
import { cn } from '../lib/utils'
import { translate } from '../lib/i18n'
const SORT_KEYS = ['updatedAt', 'createdAt', 'messageCount', 'tokensTotal', 'cost'] as const
const SORT_LABEL_KEYS: Record<string, string> = {
updatedAt: 'list.sort.updated',
createdAt: 'list.sort.created',
messageCount: 'list.sort.rounds',
tokensTotal: 'list.sort.tokens',
cost: 'list.sort.cost',
}
const GROUP_KEYS = ['today', 'week', 'month', 'older'] as const
const GROUP_LABEL_KEYS: Record<string, string> = {
today: 'list.group.today',
week: 'list.group.week',
month: 'list.group.month',
older: 'list.group.older',
}
function formatTokens(n: number): string {
if (n >= 1000000) return (n / 1000000).toFixed(1) + 'M'
if (n >= 1000) return (n / 1000).toFixed(1) + 'k'
return String(n)
}
function formatRelativeTime(ts: number): string {
const now = Date.now()
const date = new Date(ts * 1000)
const diffMs = now - date.getTime()
const diffMin = Math.floor(diffMs / 60000)
const diffH = Math.floor(diffMs / 3600000)
const diffD = Math.floor(diffMs / 86400000)
if (diffMin < 1) return 'just now'
if (diffMin < 60) return `${diffMin}m ago`
if (diffH < 24) return `${diffH}h ago`
if (diffD < 30) return `${diffD}d ago`
const y = date.getFullYear() % 100
const m = String(date.getMonth() + 1).padStart(2, '0')
const d = String(date.getDate()).padStart(2, '0')
return `${y}${m}${d}`
}
function formatCost(n: number): string {
if (n === 0) return ''
if (n < 0.01) return '<$0.01'
return '$' + n.toFixed(2)
}
type CtxMenuState = { x: number; y: number; sessionId: string; session: any } | null
const ContextMenu = memo(function ContextMenu({ ctx, onClose, togglePin, toggleStar, toggleArchive, deleteSession, openDetail }: {
ctx: CtxMenuState
onClose: () => void
togglePin: (id: string) => void
toggleStar: (id: string) => void
toggleArchive: (id: string) => void
deleteSession: (id: string) => void
openDetail: (s: any) => void
}) {
const lang = useStore(s => s.language)
const ref = useRef<HTMLDivElement>(null)
useEffect(() => {
if (!ctx) return
const close = (e: MouseEvent) => {
if (ref.current && ref.current.contains(e.target as Node)) return
e.stopImmediatePropagation()
e.preventDefault()
onClose()
}
const closeCtx = (e: MouseEvent) => {
if (ref.current && ref.current.contains(e.target as Node)) return
e.preventDefault()
onClose()
}
const closeKey = (e: KeyboardEvent) => { if (e.key === 'Escape') onClose() }
const closeWheel = () => onClose()
document.addEventListener('click', close, true)
document.addEventListener('contextmenu', closeCtx, true)
document.addEventListener('keydown', closeKey)
window.addEventListener('wheel', closeWheel, { passive: true })
return () => {
document.removeEventListener('click', close, true)
document.removeEventListener('contextmenu', closeCtx, true)
document.removeEventListener('keydown', closeKey)
window.removeEventListener('wheel', closeWheel)
}
}, [ctx, onClose])
useEffect(() => {
if (!ctx || !ref.current) return
const el = ref.current
const raf = requestAnimationFrame(() => {
const rect = el.getBoundingClientRect()
let x = parseFloat(el.style.left), y = parseFloat(el.style.top)
if (x + rect.width > window.innerWidth) x = window.innerWidth - rect.width - 8
if (y + rect.height > window.innerHeight) y = window.innerHeight - rect.height - 8
if (x < 4) x = 4
if (y < 4) y = 4
el.style.left = x + 'px'
el.style.top = y + 'px'
})
return () => cancelAnimationFrame(raf)
}, [ctx])
if (!ctx) return null
const s = ctx.session
const items = [
{ icon: Eye, label: translate('ctx.viewDetail', lang), action: () => { onClose(); openDetail(s) } },
{ icon: Pin, label: s.pinned ? translate('ctx.unpin', lang) : translate('ctx.pin', lang), action: () => togglePin(ctx.sessionId) },
{ icon: Star, label: s.starred ? translate('ctx.unstar', lang) : translate('ctx.star', lang), action: () => toggleStar(ctx.sessionId) },
{ icon: Archive, label: translate('ctx.archive', lang), action: () => { onClose(); useStore.getState().confirm(translate('confirm.archive', lang)).then(ok => { if (ok) toggleArchive(ctx.sessionId) }) } },
{ icon: Trash2, label: translate('ctx.delete', lang), danger: true, action: () => { onClose(); useStore.getState().confirm(translate('confirm.delete', lang)).then(ok => { if (ok) deleteSession(ctx.sessionId) }) } },
]
return createPortal(
<AnimatePresence>
<motion.div
ref={ref}
initial={{ scale: 0.92, opacity: 0 }}
animate={{ scale: 1, opacity: 1 }}
exit={{ scale: 0.92, opacity: 0 }}
transition={{ duration: 0.15, ease: [0.16, 1, 0.3, 1] }}
style={{ position: 'fixed', zIndex: 9999, left: ctx.x, top: ctx.y, minWidth: 150 }}
className="rounded-lg border border-border bg-card/80 p-1 shadow-2xl backdrop-blur-xl"
>
{items.map(({ icon: Icon, label, danger, action }) => (
<div
key={label}
onClick={() => { action(); onClose() }}
className={cn(
'flex cursor-pointer items-center gap-2 rounded-md px-3 py-1.5 text-xs transition-colors',
danger ? 'text-danger hover:bg-danger/10' : 'text-foreground hover:bg-hover'
)}
style={{ fontFamily: 'var(--font-mono)' }}
>
<Icon size={13} />
{label}
</div>
))}
</motion.div>
</AnimatePresence>,
document.body
)
})
function getTimeGroup(ts: number | null): string {
if (!ts) return 'older'
const now = Date.now() / 1000
const diff = now - ts
if (diff < 86400) return 'today'
if (diff < 604800) return 'week'
if (diff < 2592000) return 'month'
return 'older'
}
export function SessionList() {
const sessions = useStore(s => s.sessions)
const loading = useStore(s => s.loading)
const searchQuery = useStore(s => s.searchQuery)
const setSearch = useStore(s => s.setSearch)
const sortBy = useStore(s => s.sortBy)
const setSortBy = useStore(s => s.setSortBy)
const openDetail = useStore(s => s.openDetail)
const togglePin = useStore(s => s.togglePin)
const toggleStar = useStore(s => s.toggleStar)
const toggleArchive = useStore(s => s.toggleArchive)
const deleteSession = useStore(s => s.deleteSession)
const lang = useStore(s => s.language)
const [ctxMenu, setCtxMenu] = useState<CtxMenuState>(null)
const closeCtx = useCallback(() => setCtxMenu(null), [])
const [selectedIndex, setSelectedIndex] = useState(0)
const sortedSessions = useMemo(() => {
let list = [...(sessions || [])]
if (searchQuery) {
const q = searchQuery.toLowerCase()
list = list.filter(s =>
(s.title || '').toLowerCase().includes(q) ||
(s.projectName || '').toLowerCase().includes(q) ||
(s.model || '').toLowerCase().includes(q)
)
}
list.sort((a, b) => {
if (a.pinned !== b.pinned) return (b.pinned || 0) - (a.pinned || 0)
const aVal = (a as any)[sortBy] || 0
const bVal = (b as any)[sortBy] || 0
return (bVal as number) - (aVal as number)
})
return list
}, [sessions, searchQuery, sortBy])
const visibleGroups = useMemo(() => {
const groups: Record<string, typeof sortedSessions> = { today: [], week: [], month: [], older: [] }
for (const s of sortedSessions) {
const g = getTimeGroup(s.updatedAt)
groups[g].push(s)
}
return GROUP_KEYS
.filter(key => groups[key] && groups[key].length > 0)
.map(key => ({ key, label: translate(GROUP_LABEL_KEYS[key] as any, lang), items: groups[key] }))
}, [sortedSessions, lang])
const groupCounts = useMemo(() => visibleGroups.map(g => g.items.length), [visibleGroups])
const itemData = useMemo(() => {
const flat: { session: any; groupIndex: number }[] = []
visibleGroups.forEach((g, gi) => {
for (const session of g.items) {
flat.push({ session, groupIndex: gi })
}
})
return flat
}, [visibleGroups])
useEffect(() => {
setSelectedIndex(0)
}, [sortedSessions])
useEffect(() => {
const handler = (e: KeyboardEvent) => {
if (e.target instanceof HTMLInputElement) return
if (e.key === 'ArrowDown') {
e.preventDefault()
setSelectedIndex(i => Math.min(i + 1, itemData.length - 1))
} else if (e.key === 'ArrowUp') {
e.preventDefault()
setSelectedIndex(i => Math.max(i - 1, 0))
} else if ((e.key === 'Enter' || e.key === 'ArrowRight') && itemData[selectedIndex]) {
e.preventDefault()
openDetail(itemData[selectedIndex].session)
}
}
window.addEventListener('keydown', handler)
return () => window.removeEventListener('keydown', handler)
}, [itemData, selectedIndex, openDetail])
return (
<div className="flex flex-1 flex-col overflow-hidden">
{/* Search + sort bar — top portion is draggable */}
<div
className="flex shrink-0 flex-col gap-3 border-b border-border px-6 py-4"
style={{ WebkitAppRegion: 'drag' } as React.CSSProperties}
>
<div className="relative" style={{ WebkitAppRegion: 'no-drag' } as React.CSSProperties}>
<Search size={13} className="absolute left-2.5 top-1/2 -translate-y-1/2 text-foreground-muted" />
<input
type="text"
placeholder={translate('list.search', lang)}
value={searchQuery}
onChange={e => setSearch(e.target.value)}
className="w-full rounded-lg border border-border bg-background py-2.5 pr-3 pl-9 text-xs text-foreground transition-colors focus:border-primary"
/>
</div>
<div className="flex gap-1" style={{ WebkitAppRegion: 'no-drag' } as React.CSSProperties}>
{SORT_KEYS.map(key => (
<button
key={key}
onClick={() => setSortBy(key)}
className={cn(
'cursor-pointer rounded px-2 py-0.5 text-[10px] transition-colors',
sortBy === key
? 'bg-hover font-semibold text-foreground'
: 'text-foreground-muted hover:text-foreground-secondary'
)}
>
{translate(SORT_LABEL_KEYS[key] as any, lang)}
</button>
))}
</div>
</div>
{/* Session list */}
<div className="flex-1 overflow-hidden">
{loading && sortedSessions.length === 0 ? (
<div className="flex h-full flex-col items-center justify-center gap-3 text-xs text-foreground-muted">
<div className="h-5 w-5 animate-spin rounded-full border-2 border-border border-t-primary" />
{translate('list.loading', lang)}
</div>
) : sortedSessions.length === 0 ? (
<div className="px-5 py-10 text-center text-xs text-foreground-muted">
{translate('list.noSessions', lang)}
</div>
) : (
<GroupedVirtuoso
groupCounts={groupCounts}
groupContent={(index) => {
const g = visibleGroups[index]
return (
<div className="sticky top-0 z-10 flex items-center gap-2 border-b border-border bg-background-secondary/80 px-6 py-2.5 text-[10px] font-semibold uppercase tracking-wider text-foreground-muted backdrop-blur-sm">
<span>{g.label}</span>
<span className="text-[9px] opacity-60">{g.items.length}</span>
<div className="h-px flex-1 bg-border/50" />
</div>
)
}}
itemContent={(index) => {
const { session } = itemData[index]
return (
<SessionCard
session={session}
openDetail={openDetail}
setCtxMenu={setCtxMenu}
selected={index === selectedIndex}
/>
)
}}
/>
)}
</div>
<ContextMenu ctx={ctxMenu} onClose={closeCtx} togglePin={togglePin} toggleStar={toggleStar} toggleArchive={toggleArchive} deleteSession={deleteSession} openDetail={openDetail} />
</div>
)
}
const SessionCard = memo(function SessionCard({ session, openDetail, setCtxMenu, selected }: { session: any; openDetail: (s: any) => void; setCtxMenu: (v: CtxMenuState) => void; selected?: boolean }) {
const resumeAction = useStore(s => s.resumeAction)
const terminalApp = useStore(s => s.terminalApp)
const lang = useStore(s => s.language)
const tokensStr = session.tokensTotal > 0 ? formatTokens(session.tokensTotal) : ''
const costStr = formatCost(session.cost)
const isHot = session.tokensTotal > 50000
const isEmpty = session.messageCount === 0
const handleResume = (s: any) => {
const cmd = s.tool === 'opencode'
? `opencode --session ${s.originalId} "${s.projectPath || ''}"`
: s.tool === 'claude'
? `claude --resume ${s.originalId}`
: `codex --resume ${s.originalId}`
if (resumeAction === 'system') {
window.api.openSystemTerminal(cmd, s.projectPath || undefined, terminalApp || undefined)
} else {
useStore.getState().resumeSession(s)
}
}
const handleContextMenu = (e: React.MouseEvent) => {
e.preventDefault()
e.stopPropagation()
setCtxMenu({ x: e.clientX, y: e.clientY, sessionId: session.id, session })
}
return (
<div
onClick={() => openDetail(session)}
onContextMenu={handleContextMenu}
className={cn(
'glass-card mx-5 my-2 cursor-pointer rounded-xl border px-5 py-4 transition-all duration-200 hover:border-primary/40 hover:shadow-lg hover:shadow-primary/5',
selected ? 'border-primary/50 bg-primary/10 shadow-md shadow-primary/10' : 'border-border/50',
isEmpty && 'opacity-45'
)}
>
<div className={cn('flex items-start gap-3.5', isEmpty && 'opacity-45')}>
<div className="pt-1">
<ToolIcon tool={session.tool} size={20} />
</div>
<div className="min-w-0 flex-1 overflow-hidden">
<div className="flex items-center gap-1.5 overflow-hidden text-[13px] font-medium text-foreground">
{session.pinned && <Pin size={12} fill="currentColor" className="shrink-0 text-primary" />}
<span className="truncate">{session.title || translate('list.untitled', lang)}</span>
{isEmpty && (
<span className="ml-1.5 italic text-foreground-muted">{translate('list.empty', lang)}</span>
)}
</div>
<div className="mt-2 flex flex-wrap items-center gap-2 text-[11px] text-foreground-muted">
{session.projectName && (
<span className="max-w-[160px] truncate rounded-md border border-border/50 bg-hover/50 px-2 py-0.5 text-[10px] text-foreground-secondary">
{session.projectName}
</span>
)}
{session.updatedAt && (
<span>{formatRelativeTime(session.updatedAt)}</span>
)}
{session.messageCount > 0 && (
<span>{session.messageCount} {translate('list.rounds', lang)}</span>
)}
{tokensStr && (
<span className={cn('flex items-center gap-0.5', isHot ? 'text-danger' : 'text-primary')}>
{isHot && <Flame size={9} />}
{tokensStr} {translate('list.tok', lang)}
</span>
)}
{costStr && <span className="text-orange">{costStr}</span>}
</div>
</div>
<button
onClick={e => { e.stopPropagation(); handleResume(session) }}
className="mt-1 flex shrink-0 cursor-pointer items-center gap-1.5 rounded-lg border border-border/50 bg-hover/50 px-3 py-2 text-[11px] text-foreground-secondary transition-all duration-150 hover:border-primary hover:bg-primary hover:text-white hover:shadow-md hover:shadow-primary/25"
>
<Play size={9} />
{translate('list.resume', lang)}
</button>
</div>
</div>
)
})