The best way to control Claude Code, Codex and more from any computer or phone

main
tsx 175 lines 5.67 KB
Raw
1 import { useGT } from "gt-react";
2 import { FolderPlusIcon, SearchIcon, SquarePenIcon, XIcon } from "lucide-react";
3 import {
4 type ComponentProps,
5 type KeyboardEvent as ReactKeyboardEvent,
6 type MouseEvent as ReactMouseEvent,
7 type ReactNode,
8 type RefObject,
9 } from "react";
10
11 import { cn } from "~/lib/utils";
12 import { Button } from "../ui/button";
13 import { SidebarInput, SidebarMenuButton } from "../ui/sidebar";
14 import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
15
16 export interface SidebarThreadHeaderProps {
17 hasProjects: boolean;
18 projectScope: ReactNode;
19 onNewProject: () => void;
20 onNewThread: (event: ReactMouseEvent) => void;
21 newThreadDisabled: boolean;
22 newThreadShortcutLabel: string | null | undefined;
23 newThreadInProjectShortcutLabel: string | null | undefined;
24 showNewThreadInProjectHint: boolean;
25 searchInputRef: RefObject<HTMLInputElement | null>;
26 searchQuery: string;
27 onSearchQueryChange: (value: string) => void;
28 onSearchKeyDown: (event: ReactKeyboardEvent<HTMLInputElement>) => void;
29 isSearching: boolean;
30 searchResultCount: number;
31 activeSearchResultIndex: number;
32 onClearSearch: () => void;
33 }
34
35 export function SidebarThreadHeader({
36 hasProjects,
37 projectScope,
38 onNewProject,
39 onNewThread,
40 newThreadDisabled,
41 newThreadShortcutLabel,
42 newThreadInProjectShortcutLabel,
43 showNewThreadInProjectHint,
44 searchInputRef,
45 searchQuery,
46 onSearchQueryChange,
47 onSearchKeyDown,
48 isSearching,
49 searchResultCount,
50 activeSearchResultIndex,
51 onClearSearch,
52 }: SidebarThreadHeaderProps) {
53 const gt = useGT();
54 const resultsVisible = isSearching && searchResultCount > 0;
55 // Shrinking results can remove the active option; never leave aria-activedescendant pointing at it.
56 const activeResultExists = resultsVisible && activeSearchResultIndex < searchResultCount;
57 const newThreadLabel = newThreadShortcutLabel
58 ? gt("New thread ({shortcut})", { shortcut: newThreadShortcutLabel })
59 : gt("New thread");
60
61 return (
62 <div className="flex items-center gap-1">
63 <div className="flex h-8 min-w-0 flex-1 items-center gap-2 rounded-md px-2 py-1.5 text-sm font-medium text-sidebar-muted-foreground hover:bg-sidebar-row-hover hover:text-sidebar-foreground focus-within:bg-sidebar-row-hover focus-within:text-sidebar-foreground focus-within:ring-1 focus-within:ring-inset focus-within:ring-ring/70">
64 <SearchIcon className="size-4 shrink-0 text-(--sidebar-icon-color)" />
65 <SidebarInput
66 ref={searchInputRef}
67 nativeInput
68 type="search"
69 value={searchQuery}
70 onChange={(event) => onSearchQueryChange(event.currentTarget.value)}
71 onKeyDown={onSearchKeyDown}
72 placeholder={gt("Search", { $context: "search field placeholder" })}
73 aria-label={gt("Search threads")}
74 role="combobox"
75 aria-autocomplete="list"
76 aria-expanded={resultsVisible}
77 aria-controls={resultsVisible ? "sidebar-thread-search-results" : undefined}
78 aria-activedescendant={
79 activeResultExists
80 ? `sidebar-thread-search-result-${activeSearchResultIndex}`
81 : undefined
82 }
83 className="min-w-0 flex-1"
84 />
85 {isSearching ? (
86 <Button
87 type="button"
88 size="icon-micro"
89 variant="ghost-muted"
90 className="shrink-0"
91 aria-label={gt("Clear thread search")}
92 onClick={() => {
93 onClearSearch();
94 searchInputRef.current?.focus();
95 }}
96 >
97 <XIcon className="size-3" />
98 </Button>
99 ) : null}
100 </div>
101 <div className="flex shrink-0 items-center">
102 {hasProjects ? (
103 <>
104 {projectScope}
105 <SidebarHeaderIconButton label={gt("Add project")} onClick={onNewProject}>
106 <FolderPlusIcon />
107 </SidebarHeaderIconButton>
108 </>
109 ) : null}
110 <SidebarHeaderIconButton
111 label={gt("New thread")}
112 tooltip={
113 showNewThreadInProjectHint ? (
114 <span className="flex flex-col gap-0.5">
115 <span>{newThreadLabel}</span>
116 <span className="text-muted-foreground">
117 {newThreadInProjectShortcutLabel
118 ? gt("New thread in current project: Shift+click ({shortcut})", {
119 shortcut: newThreadInProjectShortcutLabel,
120 })
121 : gt("New thread in current project: Shift+click")}
122 </span>
123 </span>
124 ) : (
125 newThreadLabel
126 )
127 }
128 disabled={newThreadDisabled}
129 onClick={onNewThread}
130 >
131 <SquarePenIcon />
132 </SidebarHeaderIconButton>
133 </div>
134 </div>
135 );
136 }
137
138 export function SidebarHeaderIconButton({
139 label,
140 tooltip = label,
141 className,
142 children,
143 ...rest
144 }: {
145 label: string;
146 tooltip?: ReactNode;
147 className?: string | undefined;
148 children?: ReactNode;
149 } & Omit<
150 ComponentProps<typeof SidebarMenuButton>,
151 "children" | "className" | "tooltip" | "isActive" | "aria-label"
152 >) {
153 return (
154 <Tooltip>
155 <TooltipTrigger
156 render={
157 <SidebarMenuButton
158 size="icon"
159 type="button"
160 aria-label={label}
161 {...rest}
162 className={cn("relative size-7 shrink-0", className)}
163 />
164 }
165 >
166 {children}
167 <span
168 aria-hidden
169 className="pointer-events-none absolute left-1/2 top-1/2 size-[max(100%,3rem)] -translate-1/2 pointer-fine:hidden"
170 />
171 </TooltipTrigger>
172 <TooltipPopup side="top">{tooltip}</TooltipPopup>
173 </Tooltip>
174 );
175 }