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

SidebarProjectFolderHeader.tsx

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