-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatHistorySidebar.tsx
More file actions
620 lines (588 loc) · 20.8 KB
/
Copy pathChatHistorySidebar.tsx
File metadata and controls
620 lines (588 loc) · 20.8 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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
import { useState, useCallback, useEffect } from 'react';
import { useTranslation } from 'react-i18next';
import {
Plus,
MessageSquare,
Trash2,
Pencil,
Check,
X,
MoreVertical,
Loader2,
CheckSquare,
Archive,
ArchiveRestore
} from 'lucide-react';
import { Button } from './ui/button';
import { Input } from './ui/input';
import { ScrollArea } from './ui/scroll-area';
import { Checkbox } from './ui/checkbox';
import { Tooltip, TooltipContent, TooltipTrigger } from './ui/tooltip';
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuTrigger
} from './ui/dropdown-menu';
import {
AlertDialog,
AlertDialogAction,
AlertDialogCancel,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogTitle
} from './ui/alert-dialog';
import { cn } from '../lib/utils';
import type { InsightsSessionSummary } from '../../shared/types';
interface ChatHistorySidebarProps {
sessions: InsightsSessionSummary[];
currentSessionId: string | null;
isLoading: boolean;
onNewSession: () => void;
onSelectSession: (sessionId: string) => void;
onDeleteSession: (sessionId: string) => Promise<boolean>;
onRenameSession: (sessionId: string, newTitle: string) => Promise<boolean>;
onArchiveSession?: (sessionId: string) => Promise<void>;
onUnarchiveSession?: (sessionId: string) => Promise<void>;
onDeleteSessions?: (sessionIds: string[]) => Promise<void>;
onArchiveSessions?: (sessionIds: string[]) => Promise<void>;
showArchived?: boolean;
onToggleShowArchived?: () => void;
}
export function ChatHistorySidebar({
sessions,
currentSessionId,
isLoading,
onNewSession,
onSelectSession,
onDeleteSession,
onRenameSession,
onArchiveSession,
onUnarchiveSession,
onDeleteSessions,
onArchiveSessions,
showArchived = false,
onToggleShowArchived
}: ChatHistorySidebarProps) {
const { t } = useTranslation('common');
const [editingId, setEditingId] = useState<string | null>(null);
const [editTitle, setEditTitle] = useState('');
const [deleteSessionId, setDeleteSessionId] = useState<string | null>(null);
const [isSelectionMode, setIsSelectionMode] = useState(false);
const [selectedIds, setSelectedIds] = useState<Set<string>>(new Set());
const [bulkDeleteOpen, setBulkDeleteOpen] = useState(false);
const [bulkArchiveOpen, setBulkArchiveOpen] = useState(false);
// Clear selection when exiting selection mode
const handleToggleSelectionMode = useCallback(() => {
setIsSelectionMode((prev) => {
if (prev) {
setSelectedIds(new Set());
}
return !prev;
});
}, []);
// Prune selectedIds when sessions change - removes IDs for sessions no longer displayed
// Also resets when showArchived toggles
// biome-ignore lint/correctness/useExhaustiveDependencies: showArchived is intentionally a dependency to reset selection on filter change
useEffect(() => {
setSelectedIds((prev) => {
if (prev.size === 0) return prev;
const validIds = new Set(sessions.map((s) => s.id));
const pruned = new Set([...prev].filter((id) => validIds.has(id)));
return pruned.size === prev.size ? prev : pruned;
});
}, [sessions, showArchived]);
const handleToggleSelect = useCallback((sessionId: string) => {
setSelectedIds((prev) => {
const next = new Set(prev);
if (next.has(sessionId)) {
next.delete(sessionId);
} else {
next.add(sessionId);
}
return next;
});
}, []);
const handleSelectAll = useCallback(() => {
setSelectedIds(new Set(sessions.map((s) => s.id)));
}, [sessions]);
const handleClearSelection = useCallback(() => {
setSelectedIds(new Set());
}, []);
const handleStartEdit = (session: InsightsSessionSummary) => {
setEditingId(session.id);
setEditTitle(session.title);
};
const handleSaveEdit = async () => {
if (editingId && editTitle.trim()) {
await onRenameSession(editingId, editTitle.trim());
}
setEditingId(null);
setEditTitle('');
};
const handleCancelEdit = () => {
setEditingId(null);
setEditTitle('');
};
const handleDelete = async () => {
if (deleteSessionId) {
await onDeleteSession(deleteSessionId);
setDeleteSessionId(null);
}
};
const handleBulkDelete = async () => {
if (selectedIds.size > 0 && onDeleteSessions) {
try {
await onDeleteSessions(Array.from(selectedIds));
setSelectedIds(new Set());
} catch (error) {
console.error('Failed to delete sessions:', error);
} finally {
setBulkDeleteOpen(false);
}
}
};
const handleBulkArchive = () => {
if (selectedIds.size > 0 && onArchiveSessions) {
setBulkArchiveOpen(true);
}
};
const handleBulkArchiveConfirmed = async () => {
if (selectedIds.size > 0 && onArchiveSessions) {
try {
await onArchiveSessions(Array.from(selectedIds));
setSelectedIds(new Set());
} catch (error) {
console.error('Failed to archive sessions:', error);
} finally {
setBulkArchiveOpen(false);
}
}
};
const formatDate = (date: Date) => {
const now = new Date();
const d = new Date(date);
const diffMs = now.getTime() - d.getTime();
const diffDays = Math.floor(diffMs / (1000 * 60 * 60 * 24));
if (diffDays === 0) {
return t('insights.today');
} else if (diffDays === 1) {
return t('insights.yesterday');
} else if (diffDays < 7) {
return t('insights.daysAgo', { count: diffDays });
} else {
return d.toLocaleDateString(undefined, { month: 'short', day: 'numeric' });
}
};
// Group sessions by date
const groupedSessions = sessions.reduce((groups, session) => {
const dateLabel = formatDate(session.updatedAt);
if (!groups[dateLabel]) {
groups[dateLabel] = [];
}
groups[dateLabel].push(session);
return groups;
}, {} as Record<string, InsightsSessionSummary[]>);
// Sessions selected for bulk delete preview
const sessionsToDelete = sessions.filter((s) => selectedIds.has(s.id));
return (
<div className="flex h-full w-64 flex-col border-r border-border bg-muted/30">
{/* Header */}
<div className="flex items-center justify-between border-b border-border px-3 py-3">
<h3 className="text-sm font-medium text-foreground">{t('insights.chatHistory')}</h3>
<div className="flex items-center gap-1">
{/* Selection mode toggle */}
<Tooltip>
<TooltipTrigger asChild>
<Button
variant={isSelectionMode ? 'secondary' : 'ghost'}
size="icon"
className="h-7 w-7"
onClick={handleToggleSelectionMode}
aria-label={isSelectionMode ? t('insights.exitSelectMode') : t('insights.selectMode')}
>
<CheckSquare className="h-4 w-4" />
</Button>
</TooltipTrigger>
<TooltipContent>
{isSelectionMode ? t('insights.exitSelectMode') : t('insights.selectMode')}
</TooltipContent>
</Tooltip>
{/* Show archived toggle */}
{onToggleShowArchived && (
<Tooltip>
<TooltipTrigger asChild>
<Button
variant={showArchived ? 'secondary' : 'ghost'}
size="icon"
className="h-7 w-7"
onClick={onToggleShowArchived}
aria-label={showArchived ? t('insights.hideArchived') : t('insights.showArchived')}
>
<Archive className={cn('h-4 w-4', showArchived && 'text-primary')} />
</Button>
</TooltipTrigger>
<TooltipContent>
{showArchived ? t('insights.hideArchived') : t('insights.showArchived')}
</TooltipContent>
</Tooltip>
)}
<Tooltip>
<TooltipTrigger asChild>
<Button
variant="ghost"
size="icon"
className="h-7 w-7"
onClick={onNewSession}
aria-label={t('accessibility.newConversationAriaLabel')}
>
<Plus className="h-4 w-4" />
</Button>
</TooltipTrigger>
<TooltipContent>{t('accessibility.newConversationAriaLabel')}</TooltipContent>
</Tooltip>
</div>
</div>
{/* Select All / Clear links */}
{isSelectionMode && sessions.length > 0 && (
<div className="flex items-center justify-between border-b border-border px-3 py-1.5">
<button
type="button"
className="text-xs text-primary hover:underline"
onClick={handleSelectAll}
>
{t('accessibility.selectAllAriaLabel')}
</button>
<button
type="button"
className="text-xs text-muted-foreground hover:underline"
onClick={handleClearSelection}
>
{t('accessibility.clearSelectionAriaLabel')}
</button>
</div>
)}
{/* Session list */}
<ScrollArea className="flex-1">
{isLoading ? (
<div className="flex items-center justify-center py-8">
<Loader2 className="h-5 w-5 animate-spin text-muted-foreground" />
</div>
) : sessions.length === 0 ? (
<div className="px-3 py-8 text-center text-sm text-muted-foreground">
{t('insights.noConversations')}
</div>
) : (
<div className="py-2">
{Object.entries(groupedSessions).map(([dateLabel, dateSessions]) => (
<div key={dateLabel} className="mb-2">
<div className="px-3 py-1 text-[11px] font-medium uppercase tracking-wide text-muted-foreground">
{dateLabel}
</div>
{dateSessions.map((session) => (
<SessionItem
key={session.id}
session={session}
isActive={session.id === currentSessionId}
isEditing={editingId === session.id}
editTitle={editTitle}
onSelect={() => onSelectSession(session.id)}
onStartEdit={() => handleStartEdit(session)}
onSaveEdit={handleSaveEdit}
onCancelEdit={handleCancelEdit}
onEditTitleChange={setEditTitle}
onDelete={() => setDeleteSessionId(session.id)}
onArchive={onArchiveSession ? () => onArchiveSession(session.id) : undefined}
onUnarchive={
onUnarchiveSession ? () => onUnarchiveSession(session.id) : undefined
}
isArchived={!!session.archivedAt}
isSelectionMode={isSelectionMode}
isSelected={selectedIds.has(session.id)}
onToggleSelect={() => handleToggleSelect(session.id)}
/>
))}
</div>
))}
</div>
)}
</ScrollArea>
{/* Bulk action toolbar */}
{isSelectionMode && selectedIds.size > 0 && (
<div className="flex items-center gap-2 border-t border-border px-3 py-2">
<Button
variant="destructive"
size="sm"
className="flex-1 text-xs"
onClick={() => setBulkDeleteOpen(true)}
>
<Trash2 className="mr-1.5 h-3.5 w-3.5" />
{t('selection.deleteSelected')} ({selectedIds.size})
</Button>
{onArchiveSessions && (
<Button
variant="secondary"
size="sm"
className="flex-1 text-xs"
onClick={handleBulkArchive}
>
<Archive className="mr-1.5 h-3.5 w-3.5" />
{t('insights.archiveSelected')} ({selectedIds.size})
</Button>
)}
</div>
)}
{/* Single delete confirmation dialog */}
<AlertDialog open={!!deleteSessionId} onOpenChange={() => setDeleteSessionId(null)}>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>{t('insights.deleteTitle')}</AlertDialogTitle>
<AlertDialogDescription>
{t('insights.deleteDescription')}
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel>{t('buttons.cancel')}</AlertDialogCancel>
<AlertDialogAction onClick={handleDelete}>{t('accessibility.deleteAriaLabel')}</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
{/* Bulk delete confirmation dialog */}
<AlertDialog open={bulkDeleteOpen} onOpenChange={setBulkDeleteOpen}>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>{t('insights.bulkDeleteTitle')}</AlertDialogTitle>
<AlertDialogDescription>
{t('insights.bulkDeleteDescription', { count: selectedIds.size })}
</AlertDialogDescription>
</AlertDialogHeader>
{sessionsToDelete.length > 0 && (
<div className="max-h-32 overflow-y-auto rounded border border-border p-2">
<p className="mb-1 text-xs font-medium text-muted-foreground">
{t('insights.conversationsToDelete')}:
</p>
<ul className="space-y-0.5">
{sessionsToDelete.map((s) => (
<li key={s.id} className="truncate text-xs text-foreground/80">
{s.title}
</li>
))}
</ul>
</div>
)}
<AlertDialogFooter>
<AlertDialogCancel>{t('buttons.cancel')}</AlertDialogCancel>
<AlertDialogAction onClick={handleBulkDelete}>
{t('insights.bulkDeleteConfirm', { count: selectedIds.size })}
</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
{/* Bulk archive confirmation dialog */}
<AlertDialog open={bulkArchiveOpen} onOpenChange={setBulkArchiveOpen}>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>{t('insights.archiveConfirmTitle')}</AlertDialogTitle>
<AlertDialogDescription>
{t('insights.archiveConfirmDescription')}
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel>{t('buttons.cancel')}</AlertDialogCancel>
<AlertDialogAction onClick={handleBulkArchiveConfirmed}>
{t('insights.archiveConfirmButton', { count: selectedIds.size })}
</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
</div>
);
}
interface SessionItemProps {
session: InsightsSessionSummary;
isActive: boolean;
isEditing: boolean;
editTitle: string;
onSelect: () => void;
onStartEdit: () => void;
onSaveEdit: () => void;
onCancelEdit: () => void;
onEditTitleChange: (title: string) => void;
onDelete: () => void;
onArchive?: () => Promise<void>;
onUnarchive?: () => Promise<void>;
isArchived: boolean;
isSelectionMode: boolean;
isSelected: boolean;
onToggleSelect: () => void;
}
function SessionItem({
session,
isActive,
isEditing,
editTitle,
onSelect,
onStartEdit,
onSaveEdit,
onCancelEdit,
onEditTitleChange,
onDelete,
onArchive,
onUnarchive,
isArchived,
isSelectionMode,
isSelected,
onToggleSelect
}: SessionItemProps) {
const { t } = useTranslation('common');
const handleKeyDown = (e: React.KeyboardEvent) => {
if (e.key === 'Enter') {
e.preventDefault();
onSaveEdit();
} else if (e.key === 'Escape') {
onCancelEdit();
}
};
if (isEditing) {
return (
<div className="group flex items-center gap-1 px-2 py-1">
<Input
value={editTitle}
onChange={(e) => onEditTitleChange(e.target.value)}
onKeyDown={handleKeyDown}
className="h-7 text-sm"
autoFocus
/>
<Button
variant="ghost"
size="icon"
className="h-7 w-7 shrink-0"
onClick={onSaveEdit}
aria-label={t('accessibility.saveEditAriaLabel')}
>
<Check className="h-3.5 w-3.5 text-success" />
</Button>
<Button
variant="ghost"
size="icon"
className="h-7 w-7 shrink-0"
onClick={onCancelEdit}
aria-label={t('accessibility.cancelEditAriaLabel')}
>
<X className="h-3.5 w-3.5 text-muted-foreground" />
</Button>
</div>
);
}
return (
// biome-ignore lint/a11y/useSemanticElements: div with role for complex styling
// biome-ignore lint/a11y/noNoninteractiveElementInteractions: interactive via role+tabIndex+keyDown
// biome-ignore lint/a11y/noStaticElementInteractions: interactive via role+tabIndex+keyDown
// biome-ignore lint/a11y/useAriaPropsSupportedByRole: aria-checked valid when role=checkbox
<div
role={isSelectionMode ? 'checkbox' : 'button'}
aria-checked={isSelectionMode ? isSelected : undefined}
tabIndex={0}
className={cn(
'group relative cursor-pointer px-2 py-2 transition-colors hover:bg-muted',
'focus:outline-none focus:ring-2 focus:ring-primary focus:ring-offset-1',
isActive && 'bg-primary/10 hover:bg-primary/15',
isArchived && 'opacity-50'
)}
onClick={isSelectionMode ? onToggleSelect : onSelect}
onKeyDown={(e) => {
if (e.key === 'Enter' || e.key === ' ') {
e.preventDefault();
isSelectionMode ? onToggleSelect() : onSelect();
}
}}
>
{/* Content with reserved space for the menu button */}
<div className="flex items-center gap-1.5 pr-7">
{isSelectionMode ? (
<div className="shrink-0">
<Checkbox
checked={isSelected}
className="h-4 w-4"
aria-hidden
tabIndex={-1}
/>
</div>
) : (
<MessageSquare
className={cn(
'h-4 w-4 shrink-0',
isActive ? 'text-primary' : 'text-muted-foreground'
)}
/>
)}
<div className="min-w-0 flex-1">
<div className="flex items-center gap-1">
<p
className={cn(
'line-clamp-2 text-sm leading-tight break-words',
isActive ? 'font-medium text-foreground' : 'text-foreground/80'
)}
>
{session.title}
</p>
{isArchived && (
<span className="inline-flex items-center gap-0.5 rounded bg-muted px-1 py-0.5 text-[9px] font-medium text-muted-foreground">
<Archive className="h-2.5 w-2.5" />
{t('insights.archived')}
</span>
)}
</div>
<p className="text-[11px] text-muted-foreground mt-0.5">
{t('insights.messageCount', { count: session.messageCount })}
</p>
</div>
</div>
{/* Absolutely positioned menu button - hidden in selection mode */}
{!isSelectionMode && (
<DropdownMenu modal={false}>
<DropdownMenuTrigger asChild onClick={(e) => e.stopPropagation()}>
<Button
variant="ghost"
size="icon"
className="absolute right-1 top-1/2 -translate-y-1/2 h-6 w-6 opacity-0 group-hover:opacity-100 data-[state=open]:opacity-100 hover:bg-muted-foreground/20 transition-opacity"
aria-label={t('accessibility.moreOptionsAriaLabel')}
>
<MoreVertical className="h-3.5 w-3.5" />
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent align="end" sideOffset={5} className="w-36 z-[100]">
<DropdownMenuItem onSelect={onStartEdit}>
<Pencil className="mr-2 h-3.5 w-3.5" />
{t('accessibility.renameAriaLabel')}
</DropdownMenuItem>
{isArchived ? (
onUnarchive && (
<DropdownMenuItem onSelect={onUnarchive}>
<ArchiveRestore className="mr-2 h-3.5 w-3.5" />
{t('insights.unarchive')}
</DropdownMenuItem>
)
) : (
onArchive && (
<DropdownMenuItem onSelect={onArchive}>
<Archive className="mr-2 h-3.5 w-3.5" />
{t('insights.archive')}
</DropdownMenuItem>
)
)}
<DropdownMenuItem
onSelect={onDelete}
className="text-destructive focus:text-destructive"
>
<Trash2 className="mr-2 h-3.5 w-3.5" />
{t('accessibility.deleteAriaLabel')}
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
)}
</div>
);
}