Open History to see the last commit
1import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/models";
2import { scopedThreadKey, scopeThreadRef } from "@t3tools/client-runtime/environment";
3import { groupIconFallbacks } from "@t3tools/client-runtime/state/project-grouping";
4import type { ContextMenuSource } from "@t3tools/contracts";
5import { ChevronDownIcon, PlusIcon } from "lucide-react";
6import { msg, T, useGT, useMessages, Var } from "gt-react";
7import { memo } from "react";
8
9import { cn } from "~/lib/utils";
10import { contextMenuSourceOf } from "../../contextMenuSource";
11import type { SidebarProjectSnapshot } from "../../sidebarProjectGrouping";
12import { useUiStateStore } from "../../uiStateStore";
13import { resolveSidebarFolderDot, resolveThreadLastVisitedAt } from "../Sidebar.logic";
14import { ProjectFavicon } from "../ProjectFavicon";
15import { Button } from "../ui/button";
16import { SharedTooltipTrigger } from "../ui/shared-tooltip";
17
18const FOLDER_DOT_CLASS_NAME = {
19 attention: "bg-warning",
20 failed: "bg-error",
21 unread: "bg-success",
22 working: "bg-info",
23} as const;
24
25const FOLDER_DOT_LABEL = {
26 attention: msg({ context: "project folder status, read after its name" })`, needs you`,
27 failed: msg({ context: "project folder status, read after its name" })`, failed`,
28 unread: msg({ context: "project folder status, read after its name" })`, done`,
29 working: msg({ context: "project folder status, read after its name" })`, working`,
30} as const;
31
32/** The folder's sidebar tree row; its thread rows name it as their parent. */
33export function sidebarFolderRowId(projectKey: string) {
34 return `folder:${projectKey}`;
35}
36
37/**
38 * A project folder in the boring sidebar. It reads like a shelf header: a trailing
39 * count and chevron, with the count only while closed and a status dot whenever hidden
40 * threads need you.
41 */
42export const SidebarProjectFolderHeader = memo(function SidebarProjectFolderHeader(props: {
43 group: SidebarProjectSnapshot;
44 collapsed: boolean;
45 hiddenThreads: readonly EnvironmentThreadShell[];
46 onToggle: (group: SidebarProjectSnapshot) => void;
47 onNewThread: (group: SidebarProjectSnapshot) => void;
48 onContextMenu: (
49 group: SidebarProjectSnapshot,
50 position: { x: number; y: number },
51 source: ContextMenuSource,
52 ) => void;
53}) {
54 const gt = useGT();
55 const m = useMessages();
56 const { group, collapsed, hiddenThreads } = props;
57 const rowId = sidebarFolderRowId(group.projectKey);
58 const localLastVisitedAtById = useUiStateStore((state) => state.threadLastVisitedAtById);
59 const hasHiddenThreads = hiddenThreads.length > 0;
60 const dot = hasHiddenThreads
61 ? resolveSidebarFolderDot(
62 hiddenThreads.map((thread) => ({
63 ...thread,
64 lastVisitedAt: resolveThreadLastVisitedAt(
65 thread.lastVisitedAt,
66 localLastVisitedAtById[
67 scopedThreadKey(scopeThreadRef(thread.environmentId, thread.id))
68 ],
69 ),
70 })),
71 )
72 : null;
73 const hasCount = collapsed && hiddenThreads.length > 0;
74 return (
75 <li
76 data-testid="sidebar-project-folder"
77 className="group/project-folder relative mt-2 list-none first:mt-0"
78 >
79 <button
80 type="button"
81 data-sidebar-row={rowId}
82 aria-expanded={!collapsed}
83 onClick={() => props.onToggle(group)}
84 onContextMenu={(event) => {
85 event.preventDefault();
86 props.onContextMenu(
87 group,
88 { x: event.clientX, y: event.clientY },
89 contextMenuSourceOf(event),
90 );
91 }}
92 className="flex h-8 w-full cursor-pointer items-center gap-2.5 rounded-(--control-radius) px-(--sidebar-row-content-inset) text-left outline-none hover:bg-sidebar-row-hover focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring"
93 >
94 <ProjectFavicon
95 project={group}
96 fallbackProjects={groupIconFallbacks(group.memberProjects, group)}
97 className="size-4 shrink-0"
98 />
99 <span className="min-w-0 flex-1 truncate text-sm font-medium text-sidebar-foreground/90">
100 {group.displayName}
101 </span>
102 {dot ? (
103 <span className={cn("size-1.5 shrink-0 rounded-full", FOLDER_DOT_CLASS_NAME[dot])}>
104 <span className="sr-only">{m(FOLDER_DOT_LABEL[dot])}</span>
105 </span>
106 ) : null}
107 {/* Widens to the new-thread button while it shows; empty, it also gives back its flex gap. */}
108 <span
109 className={cn(
110 "flex min-w-0 shrink-0 justify-end group-hover/project-folder:min-w-4 group-has-[[data-sidebar-action-for]:focus]/project-folder:min-w-4 pointer-coarse:min-w-4",
111 !hasCount &&
112 "-ml-2.5 group-hover/project-folder:ml-0 group-has-[[data-sidebar-action-for]:focus]/project-folder:ml-0 pointer-coarse:ml-0",
113 )}
114 >
115 {hasCount ? (
116 <span className="text-xs tabular-nums text-sidebar-muted-foreground group-hover/project-folder:invisible group-has-[[data-sidebar-action-for]:focus]/project-folder:invisible pointer-coarse:invisible">
117 {hiddenThreads.length}
118 </span>
119 ) : null}
120 </span>
121 <ChevronDownIcon
122 aria-hidden
123 className={cn(
124 "size-3 shrink-0 text-sidebar-muted-foreground transition-transform",
125 collapsed && "-rotate-90",
126 )}
127 />
128 </button>
129 {/* Sits over the count, which hides on hover or focus, and left of the chevron. */}
130 <span className="absolute inset-y-0 right-6 flex items-center opacity-0 focus-within:opacity-100 group-hover/project-folder:opacity-100 pointer-coarse:opacity-100">
131 <SharedTooltipTrigger
132 render={
133 <Button
134 data-sidebar-action-for={rowId}
135 size="icon-xs"
136 variant="ghost-muted"
137 aria-label={gt`New thread in ${{ project: group.displayName }}`}
138 onClick={() => props.onNewThread(group)}
139 />
140 }
141 label={
142 <>
143 <T>
144 New thread in <Var>{group.displayName}</Var>
145 </T>
146 </>
147 }
148 >
149 <PlusIcon aria-hidden />
150 </SharedTooltipTrigger>
151 </span>
152 </li>
153 );
154});