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