-
Notifications
You must be signed in to change notification settings - Fork 173
Expand file tree
/
Copy pathChatHistorySidebar.tsx
More file actions
2247 lines (2175 loc) · 87.9 KB
/
Copy pathChatHistorySidebar.tsx
File metadata and controls
2247 lines (2175 loc) · 87.9 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
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { Tooltip } from "@base-ui/react";
import { useVirtualizer } from "@tanstack/react-virtual";
import { type CSSProperties, memo, useCallback, useEffect, useMemo, useRef, useState } from "react";
import iconSimpleUrl from "../../../src-tauri/icons/icon-simple.png";
import { useLocale } from "../../i18n";
import type { AppUpdateController } from "../../lib/appUpdates";
import {
DEFAULT_WORKSPACE_PROJECT_ID,
type WorkspaceProject,
workspaceProjectPathKey,
} from "../../lib/settings";
import { cn } from "../../lib/shared/utils";
import type {
SidebarBatchDeleteOptions,
SidebarBatchDeleteResult,
} from "../../lib/sidebar/batchDelete";
import { reconcileSidebarSelection, updateSidebarSelection } from "../../lib/sidebar/selection";
import type {
SidebarConversation,
SidebarListStatus,
SidebarMutationKind,
} from "../../lib/sidebar/types";
import { AppUpdateButton } from "../AppUpdateButton";
import {
Archive,
ArchiveRestore,
Blend,
Cable,
Check,
ChevronRight,
CirclePlus,
Edit3,
Folder,
FolderClosed,
FolderOpen,
FolderTree,
ListChecks,
Loader2,
MoreHorizontal,
PanelLeftClose,
Pin,
PinOff,
Plus,
Settings,
Share2,
Trash2,
X,
} from "../icons";
import { isMacOsTauri, MacOsTitleBarSpacer } from "../MacOsTitleBarSpacer";
import { Button } from "../ui/button";
import { useConfirmDialog } from "../ui/confirm-dialog";
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuSub,
DropdownMenuSubContent,
DropdownMenuSubTrigger,
DropdownMenuTrigger,
} from "../ui/dropdown-menu";
import { Input } from "../ui/input";
type ChatHistorySidebarProps = {
items: readonly SidebarConversation[];
currentConversationId: string;
runningConversationIds: ReadonlySet<string>;
// Rows with an in-flight mutation: only that row's controls are disabled.
busyConversationIds: ReadonlyMap<string, SidebarMutationKind>;
listStatus: SidebarListStatus;
// Identity of the current list scope (workspace/text mode). A change
// remounts the list content with a soft enter transition and resets scroll.
scopeKey?: string;
totalItems: number;
hasMore: boolean;
isLoadingMore: boolean;
// Localized error text (list or per-row mutation); rendered as a banner
// above the rows, never replacing them.
errorMessage: string | null;
errorDetail?: string | null;
onDismissError?: () => void;
renamingId: string | null;
renameDraft: string;
isOpen: boolean;
fontScale?: number;
activeView?: "chat" | "skills-hub" | "mcp-hub";
showProjects?: boolean;
// Pre-sorted by the container (activity/running/pinned) — rendered as-is.
projects?: WorkspaceProject[];
activeProjectId?: string;
missingProjectPathKeys?: ReadonlySet<string>;
runningProjectPathKeys?: ReadonlySet<string>;
projectRenamingId?: string | null;
projectRenameDraft?: string;
projectsCollapsed?: boolean;
recentCollapsed?: boolean;
onProjectsCollapsedChange?: (collapsed: boolean) => void;
onRecentCollapsedChange?: (collapsed: boolean) => void;
onCreateProject?: () => void;
onSelectProject?: (project: WorkspaceProject) => void;
onNewConversationForProject?: (project: WorkspaceProject) => void;
onBrowseProjectInFileTree?: (project: WorkspaceProject) => void;
onBrowseProjectInSystemFileManager?: (project: WorkspaceProject) => void;
onStartRenamingProject?: (project: WorkspaceProject) => void;
onProjectRenameDraftChange?: (value: string) => void;
onCommitProjectRename?: () => void;
onCancelProjectRename?: () => void;
onSetProjectPinned?: (project: WorkspaceProject, isPinned: boolean) => void;
onRemoveProject?: (project: WorkspaceProject) => void;
onArchiveProject?: (project: WorkspaceProject) => void;
onUnarchiveProject?: (project: WorkspaceProject) => void;
// Path keys of archived workspaces; those rows render disabled in a
// collapsed group at the end of the list.
archivedProjectPathKeys?: ReadonlySet<string>;
onNewConversation: () => void;
onSelectConversation: (id: string) => void;
onStartRenaming: (item: SidebarConversation) => void;
onRenameDraftChange: (value: string) => void;
onCommitRename: () => void;
onCancelRename: () => void;
onSetPinned: (id: string, isPinned: boolean) => void;
onMoveToWorkspace: (id: string, cwd: string) => void;
onMoveConversationsToWorkspace: (ids: readonly string[], cwd: string) => Promise<void>;
canShareConversations: boolean;
sharedConversationCount: number;
onShareConversation: (item: SidebarConversation) => void;
onOpenSharedConversations: () => void;
onDeleteConversation: (id: string) => void;
onDeleteConversations: (
ids: readonly string[],
options?: SidebarBatchDeleteOptions,
) => Promise<SidebarBatchDeleteResult>;
onLoadMore: () => void;
onCloseSidebar: () => void;
onOpenSettings: () => void;
appUpdate?: AppUpdateController;
onOpenSkillsHub?: () => void;
onOpenMcpHub?: () => void;
};
const HISTORY_ROW_ESTIMATED_HEIGHT = 30;
const HISTORY_ROW_GAP = 2;
const HISTORY_ROW_OVERSCAN_COUNT = 8;
const HISTORY_LOAD_MORE_THRESHOLD = 12;
const PROJECT_ICON_BUTTON_CLASS =
"h-7 w-7 rounded-lg !bg-transparent text-muted-foreground transition-colors hover:!bg-transparent hover:!text-foreground active:!bg-transparent focus-visible:!bg-transparent data-[state=open]:!bg-transparent data-[state=open]:text-foreground data-[popup-open]:!bg-transparent data-[popup-open]:text-foreground";
const SIDEBAR_SECTION_ROWS_TRANSITION_CLASS =
"transition-[grid-template-rows] duration-300 ease-out motion-reduce:transition-none";
const SIDEBAR_PROJECT_MIN_BODY_HEIGHT = 96;
const SIDEBAR_RECENT_MIN_BODY_HEIGHT = 160;
const PROJECT_LIST_COLLAPSED_MAX = 30;
const EMPTY_PROJECT_PATH_KEYS = new Set<string>();
const HISTORY_LOADING_SKELETON_ROWS = [
{ title: "w-36", meta: "w-20" },
{ title: "w-44", meta: "w-24" },
{ title: "w-32", meta: "w-16" },
{ title: "w-40", meta: "w-28" },
{ title: "w-28", meta: "w-20" },
] as const;
function clampSidebarSectionHeight(height: number, minHeight: number, maxHeight: number) {
return Math.round(Math.min(Math.max(height, minHeight), Math.max(minHeight, maxHeight)));
}
function useStableEvent<Args extends unknown[], Return>(
handler: (...args: Args) => Return,
): (...args: Args) => Return {
const handlerRef = useRef(handler);
handlerRef.current = handler;
return useCallback((...args: Args) => handlerRef.current(...args), []);
}
const HistoryRow = memo(function HistoryRow(props: {
item: SidebarConversation;
isActive: boolean;
isRunning: boolean;
isBusy: boolean;
isDeleteDisabled: boolean;
canShareConversation: boolean;
isRenaming: boolean;
isPendingDelete: boolean;
isSelectionMode: boolean;
isSelected: boolean;
isSelectionDisabled: boolean;
renameDraft: string;
onSelectConversation: (id: string) => void;
onStartRenaming: (item: SidebarConversation) => void;
onRenameDraftChange: (value: string) => void;
onCommitRename: () => void;
onCancelRename: () => void;
onSetPinned: (id: string, isPinned: boolean) => void;
onMoveToWorkspace: (id: string, cwd: string) => void;
moveWorkspaces: readonly WorkspaceProject[];
onShareConversation: (item: SidebarConversation) => void;
onDeleteConversation: (id: string) => void;
onSetPendingDelete: (id: string | null) => void;
onSelectForBulk: (id: string, modifiers: { shiftKey: boolean; toggleKey: boolean }) => void;
onEnterSelectionMode: (id: string) => void;
}) {
const {
item,
isActive,
isRunning,
isBusy,
isDeleteDisabled,
canShareConversation,
isRenaming,
isPendingDelete,
isSelectionMode,
isSelected,
isSelectionDisabled,
renameDraft,
onSelectConversation,
onStartRenaming,
onRenameDraftChange,
onCommitRename,
onCancelRename,
onSetPinned,
onMoveToWorkspace,
moveWorkspaces,
onShareConversation,
onDeleteConversation,
onSetPendingDelete,
onSelectForBulk,
onEnterSelectionMode,
} = props;
const { t } = useLocale();
const inputRef = useRef<HTMLInputElement | null>(null);
// Enter/Escape mark the blur as handled so the following input blur does
// not double-commit (symmetric with ProjectRow's guard).
const skipNextBlurCommitRef = useRef(false);
const [menuOpen, setMenuOpen] = useState(false);
const handleSelect = useCallback(
(event: React.MouseEvent<HTMLButtonElement>) => {
const usesSelectionModifier = event.shiftKey || event.ctrlKey || event.metaKey;
if (isSelectionMode || usesSelectionModifier) {
if (!isSelectionDisabled) {
onSelectForBulk(item.id, {
shiftKey: event.shiftKey,
toggleKey: event.ctrlKey || event.metaKey,
});
}
return;
}
onSelectConversation(item.id);
},
[isSelectionDisabled, isSelectionMode, item.id, onSelectConversation, onSelectForBulk],
);
const handleStartRenaming = useCallback(() => {
onStartRenaming(item);
}, [item, onStartRenaming]);
const handleRequestDelete = useCallback(() => {
onSetPendingDelete(item.id);
}, [item.id, onSetPendingDelete]);
const handleEnterSelectionMode = useCallback(() => {
onEnterSelectionMode(item.id);
}, [item.id, onEnterSelectionMode]);
const handleTogglePinned = useCallback(() => {
onSetPinned(item.id, item.isPinned !== true);
}, [item.id, item.isPinned, onSetPinned]);
const handleShare = useCallback(() => {
onShareConversation(item);
}, [item, onShareConversation]);
const handleConfirmDelete = useCallback(() => {
onSetPendingDelete(null);
onDeleteConversation(item.id);
}, [item.id, onDeleteConversation, onSetPendingDelete]);
const handleCancelDelete = useCallback(() => {
onSetPendingDelete(null);
}, [onSetPendingDelete]);
useEffect(() => {
if (!isRenaming) return;
skipNextBlurCommitRef.current = false;
inputRef.current?.focus();
inputRef.current?.select();
}, [isRenaming]);
if (isPendingDelete) {
return (
<div className="chat-history-row rounded-2xl border border-border/70 bg-background px-3 py-2.5 shadow-xs shadow-black/5">
<p className="truncate text-sm leading-5 text-foreground/80">
{t("chat.conversationDeleteConfirm").replace("{title}", item.title)}
</p>
<p className="mt-0.5 text-[calc(11px*var(--zone-font-scale,1))] leading-4 text-muted-foreground">
{t("chat.conversationDeleteWarning")}
</p>
<div className="mt-2 grid grid-cols-2 gap-1.5">
<Button
type="button"
variant="outline"
size="sm"
onClick={handleCancelDelete}
className="h-7 rounded-xl border-border/60 text-xs font-normal text-muted-foreground hover:text-foreground"
>
{t("chat.cancel")}
</Button>
<Button
type="button"
size="sm"
onClick={handleConfirmDelete}
disabled={isDeleteDisabled || isBusy}
className="h-7 rounded-xl bg-destructive text-xs font-medium text-destructive-foreground hover:bg-destructive/90"
>
{t("chat.delete")}
</Button>
</div>
</div>
);
}
return (
<div
className={cn(
"chat-history-row group/item grid h-[30px] grid-cols-[minmax(0,1fr)_auto] items-center rounded-lg pl-1 transition-colors",
isSelectionMode && isSelected
? "bg-primary/10 text-foreground hover:bg-primary/[0.14]"
: isActive
? "bg-foreground/[0.07] text-foreground hover:bg-foreground/[0.09]"
: "text-foreground/85 hover:bg-foreground/[0.05] hover:text-foreground",
isSelectionMode && isSelectionDisabled && "opacity-50",
)}
>
{isRenaming ? (
<div className="flex h-[30px] min-w-0 items-center px-2">
<Input
ref={inputRef}
value={renameDraft}
onChange={(e) => onRenameDraftChange(e.currentTarget.value)}
onBlur={() => {
if (skipNextBlurCommitRef.current) {
skipNextBlurCommitRef.current = false;
return;
}
onCommitRename();
}}
onKeyDown={(e) => {
if (e.key === "Enter") {
e.preventDefault();
skipNextBlurCommitRef.current = true;
onCommitRename();
}
if (e.key === "Escape") {
e.preventDefault();
skipNextBlurCommitRef.current = true;
onCancelRename();
}
}}
onClick={(e) => e.stopPropagation()}
className="h-7 min-w-0 flex-1 rounded-none border-0 bg-transparent p-0 text-[calc(14px*var(--zone-font-scale,1))] font-normal shadow-none outline-none focus-visible:border-0 focus-visible:bg-transparent"
disabled={isRunning || isBusy}
/>
</div>
) : (
<button
type="button"
onClick={handleSelect}
onMouseDown={(event) => {
if (event.shiftKey) event.preventDefault();
}}
onDoubleClick={(event) => {
event.preventDefault();
if (!isSelectionMode && !isRunning && !isBusy) {
handleStartRenaming();
}
}}
aria-pressed={isSelectionMode ? isSelected : undefined}
disabled={isSelectionMode && isSelectionDisabled}
className="flex h-[30px] min-w-0 items-center gap-2 rounded-md px-2 text-left outline-hidden transition-colors focus-visible:ring-2 focus-visible:ring-ring"
title={item.title}
>
{isSelectionMode ? (
<span
aria-hidden="true"
className={cn(
"flex h-4 w-4 shrink-0 items-center justify-center rounded-[4px] border transition-colors",
isSelected
? "border-primary bg-primary text-primary-foreground"
: "border-muted-foreground/45 bg-background/50",
)}
>
{isSelected ? <Check className="h-3 w-3" /> : null}
</span>
) : null}
<span className="sidebar-project-name-fade min-w-0 flex-1 overflow-hidden whitespace-nowrap text-[calc(14px*var(--zone-font-scale,1))] font-normal leading-5">
{item.title}
</span>
</button>
)}
{!isRenaming && !isSelectionMode ? (
<div
className={cn(
"relative flex items-center justify-end overflow-hidden transition-[max-width,opacity] duration-200 ease-out",
isRunning
? "max-w-7 opacity-100 group-hover/item:max-w-16 group-focus-within/item:max-w-16"
: "max-w-0 opacity-0 group-hover/item:max-w-16 group-hover/item:opacity-100 group-focus-within/item:max-w-16 group-focus-within/item:opacity-100",
menuOpen && "max-w-16 opacity-100",
)}
>
{isRunning ? (
<span
role="img"
aria-label={t("chat.statusRunningReply")}
title={t("chat.statusRunningReply")}
className={cn(
"pointer-events-none absolute right-1.5 flex h-4 w-4 items-center justify-center text-muted-foreground transition-opacity duration-200",
"opacity-100 group-hover/item:opacity-0 group-focus-within/item:opacity-0",
menuOpen && "opacity-0",
)}
>
<Loader2 className="h-4 w-4 animate-spin" />
</span>
) : null}
<div
className={cn(
"flex items-center gap-0.5 transition-opacity duration-200",
isRunning
? "opacity-0 group-hover/item:opacity-100 group-focus-within/item:opacity-100"
: "opacity-100",
menuOpen && "opacity-100",
)}
>
<Button
type="button"
variant="ghost"
size="icon"
className={PROJECT_ICON_BUTTON_CLASS}
title={item.isPinned ? t("chat.conversationUnpin") : t("chat.conversationPin")}
aria-label={item.isPinned ? t("chat.conversationUnpin") : t("chat.conversationPin")}
onClick={handleTogglePinned}
disabled={item.isPending || isBusy}
>
{item.isPinned ? <PinOff className="h-3.5 w-3.5" /> : <Pin className="h-3.5 w-3.5" />}
</Button>
<DropdownMenu open={menuOpen} onOpenChange={setMenuOpen}>
<DropdownMenuTrigger
render={
<Button
type="button"
variant="ghost"
size="icon"
className={PROJECT_ICON_BUTTON_CLASS}
title={t("chat.conversationMore")}
aria-label={t("chat.conversationMore")}
onPointerDown={(e: React.PointerEvent<HTMLButtonElement>) =>
e.stopPropagation()
}
onClick={(e: React.MouseEvent<HTMLButtonElement>) => e.stopPropagation()}
/>
}
>
<MoreHorizontal className="h-3.5 w-3.5" />
</DropdownMenuTrigger>
<DropdownMenuContent
side="right"
align="start"
sideOffset={8}
className="sidebar-context-menu min-w-[10rem] rounded-xl border-border/60 bg-background/95 backdrop-blur-xl"
>
{canShareConversation && !item.isPending ? (
<DropdownMenuItem onSelect={handleShare} className="gap-2">
<Share2 className="h-3.5 w-3.5" />
{t("chat.conversationShare")}
</DropdownMenuItem>
) : null}
<DropdownMenuItem
disabled={isRunning || isBusy}
onSelect={handleEnterSelectionMode}
className="gap-2"
>
<ListChecks className="h-3.5 w-3.5" />
{t("chat.conversationBulkSelect")}
</DropdownMenuItem>
<DropdownMenuItem
disabled={isRunning || isBusy}
onSelect={handleStartRenaming}
className="gap-2"
>
<Edit3 className="h-3.5 w-3.5" />
{t("chat.conversationRename")}
</DropdownMenuItem>
<DropdownMenuSub>
<DropdownMenuSubTrigger
disabled={isRunning || isBusy || moveWorkspaces.length === 0}
className="gap-2"
>
<Folder className="h-3.5 w-3.5" />
{t("chat.conversationMoveToWorkspace")}
</DropdownMenuSubTrigger>
<DropdownMenuSubContent className="sidebar-context-menu max-h-[18rem] min-w-[12rem] overflow-y-auto rounded-xl border-border/60 bg-background/95 backdrop-blur-xl">
{moveWorkspaces.map((workspace) => (
<DropdownMenuItem
key={workspace.id}
disabled={isRunning || isBusy || workspace.path === item.cwd}
onSelect={() => onMoveToWorkspace(item.id, workspace.path)}
className="gap-2"
>
<FolderClosed className="h-3.5 w-3.5 shrink-0" />
<span className="truncate">{workspace.path}</span>
</DropdownMenuItem>
))}
</DropdownMenuSubContent>
</DropdownMenuSub>
<DropdownMenuItem
disabled={isDeleteDisabled || isBusy}
onSelect={handleRequestDelete}
className="gap-2 text-destructive focus:bg-destructive/10 focus:text-destructive"
>
<Trash2 className="h-3.5 w-3.5" />
{t("chat.conversationDelete")}
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
</div>
</div>
) : null}
</div>
);
});
const ProjectRow = memo(function ProjectRow(props: {
project: WorkspaceProject;
isActive: boolean;
isMissing: boolean;
isRunning: boolean;
isRenaming: boolean;
isPendingRemove: boolean;
renameDraft: string;
onSelectProject: (project: WorkspaceProject) => void;
onBrowseProjectInFileTree?: (project: WorkspaceProject) => void;
onBrowseProjectInSystemFileManager?: (project: WorkspaceProject) => void;
onStartRenamingProject: (project: WorkspaceProject) => void;
onProjectRenameDraftChange: (value: string) => void;
onCommitProjectRename: () => void;
onCancelProjectRename: () => void;
onSetProjectPinned: (project: WorkspaceProject, isPinned: boolean) => void;
onRemoveProject: (project: WorkspaceProject) => void;
// Archived rows render disabled: no selection (so no new conversations),
// no pin — but rename/remove/browse stay available from the menu.
isArchived: boolean;
// Offered only while at least one other non-archived workspace remains.
canArchive: boolean;
onArchiveProject: (project: WorkspaceProject) => void;
onUnarchiveProject: (project: WorkspaceProject) => void;
onSetPendingRemove: (projectId: string | null) => void;
}) {
const {
project,
isActive,
isMissing,
isRunning,
isRenaming,
isPendingRemove,
renameDraft,
onSelectProject,
onBrowseProjectInFileTree,
onBrowseProjectInSystemFileManager,
onStartRenamingProject,
onProjectRenameDraftChange,
onCommitProjectRename,
onCancelProjectRename,
onSetProjectPinned,
onRemoveProject,
isArchived,
canArchive,
onArchiveProject,
onUnarchiveProject,
onSetPendingRemove,
} = props;
const { t } = useLocale();
const rowRef = useRef<HTMLDivElement | null>(null);
const inputRef = useRef<HTMLInputElement | null>(null);
const skipNextBlurCommitRef = useRef(false);
const [menuOpen, setMenuOpen] = useState(false);
const isDefaultProject = project.id === DEFAULT_WORKSPACE_PROJECT_ID;
const isPinned = project.isPinned === true;
const ProjectFolderIcon = isActive ? FolderOpen : FolderClosed;
useEffect(() => {
if (!isRenaming) return;
skipNextBlurCommitRef.current = false;
inputRef.current?.focus();
inputRef.current?.select();
}, [isRenaming]);
const handleRequestRemove = useCallback(() => {
onSetPendingRemove(project.id);
}, [onSetPendingRemove, project.id]);
const handleConfirmRemove = useCallback(() => {
onSetPendingRemove(null);
onRemoveProject(project);
}, [onRemoveProject, onSetPendingRemove, project]);
const handleCancelRemove = useCallback(() => {
onSetPendingRemove(null);
}, [onSetPendingRemove]);
const handleTogglePinned = useCallback(() => {
onSetProjectPinned(project, !isPinned);
}, [isPinned, onSetProjectPinned, project]);
const handleBrowseInFileTree = useCallback(() => {
onBrowseProjectInFileTree?.(project);
}, [onBrowseProjectInFileTree, project]);
const handleBrowseInSystemFileManager = useCallback(() => {
onBrowseProjectInSystemFileManager?.(project);
}, [onBrowseProjectInSystemFileManager, project]);
const handleArchive = useCallback(() => {
onArchiveProject(project);
}, [onArchiveProject, project]);
const handleUnarchive = useCallback(() => {
onUnarchiveProject(project);
}, [onUnarchiveProject, project]);
if (isPendingRemove) {
return (
<div className="rounded-lg border border-destructive/25 bg-destructive/5 px-3 py-2.5 text-sm text-destructive shadow-xs shadow-black/5">
<p className="truncate font-medium leading-5 text-destructive">
{t("chat.workspaceRemoveConfirm").replace("{name}", project.name)}
</p>
<p className="mt-0.5 text-[calc(11px*var(--zone-font-scale,1))] leading-4 text-destructive/75">
{isRunning ? t("chat.workspaceRemoveRunning") : t("chat.workspaceRemoveDescription")}
</p>
<div className="mt-2 grid grid-cols-2 gap-1.5">
<Button
type="button"
variant="outline"
size="sm"
onClick={handleCancelRemove}
className="h-7 rounded-xl border-border/60 bg-background text-xs font-normal text-muted-foreground hover:text-foreground"
>
{t("chat.cancel")}
</Button>
<Button
type="button"
size="sm"
onClick={handleConfirmRemove}
disabled={isRunning}
className="h-7 rounded-xl bg-destructive text-xs font-medium text-destructive-foreground hover:bg-destructive/90"
>
{t("chat.remove")}
</Button>
</div>
</div>
);
}
return (
<div
ref={rowRef}
className={cn(
"group/project grid h-[30px] grid-cols-[minmax(0,1fr)_auto] items-center rounded-lg pl-1 transition-colors",
isMissing
? "text-destructive hover:bg-destructive/10"
: isArchived
? "text-muted-foreground/60 hover:bg-foreground/[0.03]"
: isActive
? "bg-foreground/[0.07] text-foreground hover:bg-foreground/[0.09]"
: "text-foreground/85 hover:bg-foreground/[0.05] hover:text-foreground",
)}
>
{isRenaming ? (
<div className="flex h-[30px] min-w-0 items-center gap-3 rounded-md px-2 text-left">
<ProjectFolderIcon
className={cn(
"h-4 w-4 shrink-0 transition-colors",
isMissing
? "text-destructive"
: isArchived
? "text-muted-foreground/40"
: isActive
? "text-amber-500"
: "text-foreground/65",
)}
/>
<Input
ref={inputRef}
value={renameDraft}
onChange={(e) => onProjectRenameDraftChange(e.currentTarget.value)}
onBlur={() => {
if (skipNextBlurCommitRef.current) {
skipNextBlurCommitRef.current = false;
return;
}
onCommitProjectRename();
}}
onKeyDown={(e) => {
if (e.key === "Enter") {
e.preventDefault();
skipNextBlurCommitRef.current = true;
onCommitProjectRename();
}
if (e.key === "Escape") {
e.preventDefault();
skipNextBlurCommitRef.current = true;
onCancelProjectRename();
}
}}
onClick={(e) => e.stopPropagation()}
className="h-7 min-w-0 flex-1 rounded-none border-0 bg-transparent p-0 text-[calc(14px*var(--zone-font-scale,1))] font-normal shadow-none outline-none focus-visible:border-0 focus-visible:bg-transparent"
/>
</div>
) : (
<Tooltip.Root>
<Tooltip.Trigger
delay={0}
closeOnClick
render={
<button
type="button"
aria-disabled={isArchived || undefined}
className={cn(
"flex h-[30px] min-w-0 items-center gap-3 rounded-md px-2 text-left outline-hidden transition-colors focus-visible:ring-2 focus-visible:ring-ring",
isMissing
? "hover:text-destructive focus-visible:bg-destructive/10"
: isArchived
? "cursor-default"
: "hover:text-foreground focus-visible:bg-foreground/[0.06]",
)}
onClick={() => {
// Archived workspaces cannot be selected, so no new
// conversations can start in them.
if (!isArchived) {
onSelectProject(project);
}
}}
onDoubleClick={(event) => {
event.preventDefault();
if (!isDefaultProject) {
onStartRenamingProject(project);
}
}}
>
<ProjectFolderIcon
className={cn(
"h-4 w-4 shrink-0 transition-colors",
isMissing
? "text-destructive"
: isArchived
? "text-muted-foreground/40"
: isActive
? "text-amber-500"
: "text-foreground/65",
)}
/>
<span
className={cn(
"sidebar-project-name-fade min-w-0 flex-1 overflow-hidden whitespace-nowrap text-[calc(14px*var(--zone-font-scale,1))] font-normal leading-5",
isMissing ? "text-destructive" : undefined,
)}
>
{project.name}
</span>
</button>
}
/>
<Tooltip.Portal>
<Tooltip.Positioner
anchor={rowRef}
side="right"
align="center"
sideOffset={10}
collisionPadding={8}
className="z-[9999]"
>
<Tooltip.Popup className="w-64 rounded-xl border border-border/60 bg-popover px-3 py-2.5 text-popover-foreground shadow-lg outline-hidden data-[open]:animate-in data-[closed]:animate-out data-[closed]:fade-out-0 data-[open]:fade-in-0 data-[closed]:zoom-out-95 data-[open]:zoom-in-95">
<p className="truncate text-sm font-semibold leading-5">{project.name}</p>
<p className="mt-1 break-all text-xs leading-4 text-muted-foreground">
{project.path}
</p>
</Tooltip.Popup>
</Tooltip.Positioner>
</Tooltip.Portal>
</Tooltip.Root>
)}
{!isRenaming ? (
<div
className={cn(
"relative flex items-center justify-end overflow-hidden transition-[max-width,opacity] duration-200 ease-out",
isMissing
? "max-w-8 opacity-100"
: isRunning
? "max-w-7 opacity-100 group-hover/project:max-w-16 group-focus-within/project:max-w-16"
: "max-w-0 opacity-0 group-hover/project:max-w-16 group-hover/project:opacity-100 group-focus-within/project:max-w-16 group-focus-within/project:opacity-100",
menuOpen && "max-w-16 opacity-100",
)}
>
{isRunning && !isMissing ? (
<span
role="img"
aria-label={t("chat.statusRunningReply")}
title={t("chat.statusRunningReply")}
className={cn(
"pointer-events-none absolute right-1.5 flex h-4 w-4 items-center justify-center text-muted-foreground transition-opacity duration-200",
"opacity-100 group-hover/project:opacity-0 group-focus-within/project:opacity-0",
menuOpen && "opacity-0",
)}
>
<Loader2 className="h-4 w-4 animate-spin" />
</span>
) : null}
<div
className={cn(
"flex items-center gap-0.5 transition-opacity duration-200",
isRunning && !isMissing
? "opacity-0 group-hover/project:opacity-100 group-focus-within/project:opacity-100"
: "opacity-100",
menuOpen && "opacity-100",
)}
>
{isMissing && !isArchived ? (
!isDefaultProject ? (
<Button
type="button"
variant="ghost"
size="icon"
className={cn(
PROJECT_ICON_BUTTON_CLASS,
"text-destructive hover:!bg-transparent hover:text-destructive",
)}
title={t("chat.workspaceRemove")}
aria-label={t("chat.workspaceRemove")}
onClick={handleRequestRemove}
>
<Trash2 className="h-3.5 w-3.5" />
</Button>
) : null
) : (
<>
{!isArchived ? (
<Button
type="button"
variant="ghost"
size="icon"
className={PROJECT_ICON_BUTTON_CLASS}
title={isPinned ? t("chat.workspaceUnpin") : t("chat.workspacePin")}
aria-label={isPinned ? t("chat.workspaceUnpin") : t("chat.workspacePin")}
onClick={handleTogglePinned}
>
{isPinned ? (
<PinOff className="h-3.5 w-3.5" />
) : (
<Pin className="h-3.5 w-3.5" />
)}
</Button>
) : null}
<DropdownMenu open={menuOpen} onOpenChange={setMenuOpen}>
<DropdownMenuTrigger
render={
<Button
type="button"
variant="ghost"
size="icon"
className={PROJECT_ICON_BUTTON_CLASS}
title={t("chat.workspaceMore")}
aria-label={t("chat.workspaceMore")}
/>
}
>
<MoreHorizontal className="h-3.5 w-3.5" />
</DropdownMenuTrigger>
<DropdownMenuContent
side="right"
align="start"
sideOffset={6}
className="sidebar-context-menu"
>
{!isDefaultProject ? (
<>
<DropdownMenuItem
onSelect={() => onStartRenamingProject(project)}
className="gap-2"
>
<Edit3 className="h-3.5 w-3.5" />
{t("chat.workspaceRename")}
</DropdownMenuItem>
<DropdownMenuItem
onSelect={handleRequestRemove}
className="gap-2 text-destructive focus:bg-destructive/10 focus:text-destructive"
>
<Trash2 className="h-3.5 w-3.5" />
{t("chat.workspaceRemove")}
</DropdownMenuItem>
</>
) : null}
{!isArchived && canArchive ? (
<DropdownMenuItem onSelect={handleArchive} className="gap-2">
<Archive className="h-3.5 w-3.5" />
{t("chat.workspaceArchive")}
</DropdownMenuItem>
) : null}
{isArchived ? (
<DropdownMenuItem onSelect={handleUnarchive} className="gap-2">
<ArchiveRestore className="h-3.5 w-3.5" />
{t("chat.workspaceUnarchive")}
</DropdownMenuItem>
) : null}
{onBrowseProjectInFileTree ? (
<DropdownMenuItem onSelect={handleBrowseInFileTree} className="gap-2">
<FolderTree className="h-3.5 w-3.5" />
{t("chat.workspaceBrowseInFileTree")}
</DropdownMenuItem>
) : null}
{onBrowseProjectInSystemFileManager ? (
<DropdownMenuItem
onSelect={handleBrowseInSystemFileManager}
className="gap-2"
>
<FolderOpen className="h-3.5 w-3.5" />
{t("chat.workspaceBrowseInSystemFileManager")}
</DropdownMenuItem>
) : null}
</DropdownMenuContent>
</DropdownMenu>
</>
)}
</div>
</div>
) : null}
</div>
);
});
function HistoryListLoadingSkeleton() {
const { t } = useLocale();
return (
<div
className="space-y-1.5 pt-1"
role="status"
aria-live="polite"
aria-label={t("sidebar.readingHistory")}
>
<div className="flex items-center gap-2 px-2 pb-1 text-[calc(11px*var(--zone-font-scale,1))] font-medium text-muted-foreground/75">
<span className="relative flex h-2 w-2 shrink-0" aria-hidden="true">
<span className="absolute inline-flex h-full w-full animate-ping rounded-full bg-primary/35 opacity-75" />
<span className="relative inline-flex h-2 w-2 rounded-full bg-primary/70" />
</span>
<span>{t("sidebar.readingHistory")}</span>
</div>
{HISTORY_LOADING_SKELETON_ROWS.map((row) => (
<div key={`${row.title}-${row.meta}`} className="rounded-lg px-2 py-2.5">
<div className="flex items-start gap-2">
<div className="skills-skeleton-shimmer mt-1 h-3.5 w-3.5 shrink-0 rounded-md" />
<div className="min-w-0 flex-1 space-y-2">
<div className={cn("skills-skeleton-shimmer h-3.5 rounded", row.title)} />
<div className={cn("skills-skeleton-shimmer h-2.5 rounded", row.meta)} />
</div>
</div>
</div>
))}
</div>
);
}
function SidebarStateCard(props: {
title: string;
description?: string;
tone?: "default" | "error";
onDismiss?: () => void;
dismissLabel?: string;
}) {
const { title, description, tone = "default", onDismiss, dismissLabel } = props;
return (
<div
className={cn(
"rounded-2xl border px-3 py-3 text-sm",
tone === "error"
? "border-destructive/20 bg-destructive/5 text-destructive"
: "border-border/60 bg-background/70 text-muted-foreground",
)}
>
<div className="flex items-start justify-between gap-2">
<div
className={cn(
"min-w-0 font-medium",
tone === "error" ? "text-destructive" : "text-foreground/85",
)}
>
{title}
</div>
{onDismiss ? (
<button
type="button"
onClick={onDismiss}
aria-label={dismissLabel}