Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

SidebarProjectFolderHeader.tsx

121 lines4.8 KB

1import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/models";
2import { scopedThreadKey, scopeThreadRef } from "@t3tools/client-runtime/environment";
3import { ChevronDownIcon, SquarePenIcon } from "lucide-react";
4import { memo } from "react";
5​
6import { cn } from "~/lib/utils";
7import type { SidebarProjectSnapshot } from "../../sidebarProjectGrouping";
8import { useUiStateStore } from "../../uiStateStore";
9import { resolveSidebarFolderDot, resolveThreadLastVisitedAt } from "../Sidebar.logic";
10import { ProjectFavicon } from "../ProjectFavicon";
11import { Button } from "../ui/button";
12import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
13​
14const FOLDER_DOT_CLASS_NAME = {
15 attention: "bg-warning",
16 failed: "bg-error",
17 unread: "bg-success",
18 working: "bg-info",
19} as const;
20​
21const FOLDER_DOT_LABEL = {
22 attention: ", needs you",
23 failed: ", failed",
24 unread: ", done",
25 working: ", working",
26} as const;
27​
28/** The folder's sidebar tree row; its thread rows name it as their parent. */
29export function sidebarFolderRowId(projectKey: string) {
30 return `folder:${projectKey}`;
32​
33/**
34 * A project folder in the boring sidebar. It reads like a shelf header: a trailing
35 * count and chevron, with the count and a status dot only while it is closed, so a
36 * closed folder still says when a hidden thread needs you.
37 */
38export const SidebarProjectFolderHeader = memo(function SidebarProjectFolderHeader(props: {
39 group: SidebarProjectSnapshot;
40 collapsed: boolean;
41 hiddenThreads: readonly EnvironmentThreadShell[];
42 onToggle: (group: SidebarProjectSnapshot) => void;
43 onNewThread: (group: SidebarProjectSnapshot) => void;
44 onContextMenu: (group: SidebarProjectSnapshot, position: { x: number; y: number }) => void;
45}) {
46 const { group, collapsed, hiddenThreads } = props;
47 const rowId = sidebarFolderRowId(group.projectKey);
48 const localLastVisitedAtById = useUiStateStore((state) => state.threadLastVisitedAtById);
49 const dot = collapsed
50 ? resolveSidebarFolderDot(
51 hiddenThreads.map((thread) => ({
52 ...thread,
53 lastVisitedAt: resolveThreadLastVisitedAt(
54 thread.lastVisitedAt,
55 localLastVisitedAtById[
56 scopedThreadKey(scopeThreadRef(thread.environmentId, thread.id))
57 ],
58 ),
59 })),
60 )
61 : null;
62 return (
63 <li
64 data-testid="sidebar-project-folder"
65 className="group/project-folder relative mt-2 list-none first:mt-0"
66 >
67 <button
68 type="button"
69 data-sidebar-row={rowId}
70 aria-expanded={!collapsed}
71 onClick={() => props.onToggle(group)}
72 onContextMenu={(event) => {
73 event.preventDefault();
74 props.onContextMenu(group, { x: event.clientX, y: event.clientY });
75 }}
76 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"
77 >
78 <ProjectFavicon project={group} className="size-4 shrink-0" />
79 <span className="min-w-0 flex-1 truncate text-sm font-medium text-sidebar-foreground/90">
80 {group.displayName}
81 </span>
82 {dot ? (
83 <span className={cn("size-1.5 shrink-0 rounded-full", FOLDER_DOT_CLASS_NAME[dot])}>
84 <span className="sr-only">{FOLDER_DOT_LABEL[dot]}</span>
85 </span>
86 ) : null}
87 {collapsed && hiddenThreads.length > 0 ? (
88 <span className="shrink-0 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">
89 {hiddenThreads.length}
90 </span>
91 ) : null}
92 <ChevronDownIcon
93 aria-hidden
94 className={cn(
95 "size-3 shrink-0 text-sidebar-muted-foreground transition-transform",
96 collapsed && "-rotate-90",
97 )}
98 />
99 </button>
100 {/* Sits over the count, which hides on hover or focus, and left of the chevron. */}
101 <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">
102 <Tooltip>
103 <TooltipTrigger
104 render={
105 <Button
106 data-sidebar-action-for={rowId}
107 size="icon-xs"
108 variant="ghost-muted"
109 aria-label={`New thread in ${group.displayName}`}
110 onClick={() => props.onNewThread(group)}
111 />
112 }
113 >
114 <SquarePenIcon aria-hidden />
115 </TooltipTrigger>
116 <TooltipPopup>New thread in {group.displayName}</TooltipPopup>
117 </Tooltip>
118 </span>
119 </li>
120 );
121});