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

SidebarThreadHeader.tsx

231 lines8.2 KB

1/**
2 * The sidebar header: one row holding search, project scope and new thread.
3 *
4 * Search owns the row's text and spans it. Project scope collapses to an icon
5 * that sits with new-project and new-thread as a segmented group at the end.
6 * The scope icon swaps to the project favicon while a project is selected,
7 * so the header still names the scope after the row that showed it is gone.
8 *
9 * The scope picker itself is passed in: its combobox state lives with the rest
10 * of the sidebar's scope logic. `searchFieldRef` lands on the search field so
11 * the picker's popup can anchor to that width rather than to its 28px trigger.
12 */
13import { FolderPlusIcon, SearchIcon, SquarePenIcon, XIcon } from "lucide-react";
14import {
15 type ComponentProps,
16 type KeyboardEvent as ReactKeyboardEvent,
17 type MouseEvent as ReactMouseEvent,
18 type ReactNode,
19 type RefObject,
20} from "react";
21​
22import { cn } from "~/lib/utils";
23import { useCompactSidebarEnabled } from "../../hooks/useSettings";
24import { Button } from "../ui/button";
25import { Input } from "../ui/input";
26import { SidebarMenuButton, useSidebar } from "../ui/sidebar";
27import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
28​
29export interface SidebarThreadHeaderProps {
30 /** Lands on the search field so a popup can anchor to its width. */
31 searchFieldRef?: RefObject<HTMLDivElement | null>;
32 /** Without projects there is nothing to scope, so those controls stay out. */
33 hasProjects: boolean;
34 /** The project scope combobox, rendered as the first icon of the group. */
35 projectScope: ReactNode;
36 onNewProject: () => void;
37 /** Receives the click so Shift+click can skip the project picker. */
38 onNewThread: (event: ReactMouseEvent) => void;
39 newThreadDisabled: boolean;
40 newThreadShortcutLabel: string | null | undefined;
41 newThreadInProjectShortcutLabel: string | null | undefined;
42 /** Shift+click only matters once there is more than one project to pick. */
43 showNewThreadInProjectHint: boolean;
44 searchInputRef: RefObject<HTMLInputElement | null>;
45 searchQuery: string;
46 onSearchQueryChange: (value: string) => void;
47 onSearchKeyDown: (event: ReactKeyboardEvent<HTMLInputElement>) => void;
48 isSearching: boolean;
49 searchResultCount: number;
50 activeSearchResultIndex: number;
51 onClearSearch: () => void;
53​
54export function SidebarThreadHeader({
55 searchFieldRef,
56 hasProjects,
57 projectScope,
58 onNewProject,
59 onNewThread,
60 newThreadDisabled,
61 newThreadShortcutLabel,
62 newThreadInProjectShortcutLabel,
63 showNewThreadInProjectHint,
64 searchInputRef,
65 searchQuery,
66 onSearchQueryChange,
67 onSearchKeyDown,
68 isSearching,
69 searchResultCount,
70 activeSearchResultIndex,
71 onClearSearch,
72}: SidebarThreadHeaderProps) {
73 const compactEnabled = useCompactSidebarEnabled();
74 const { state, isMobile, setOpen } = useSidebar();
75 const compact = compactEnabled && state === "collapsed" && !isMobile;
76 const resultsVisible = isSearching && searchResultCount > 0;
77 // Results shrink as the query narrows, so the active index can outrun the
78 // list; pointing aria-activedescendant at a removed option strands the
79 // screen reader on nothing.
80 const activeResultExists = resultsVisible && activeSearchResultIndex < searchResultCount;
81 const newThreadLabel = newThreadShortcutLabel
82 ? `New thread (${newThreadShortcutLabel})`
83 : "New thread";
84​
85 return (
86 <div className={cn("flex items-center gap-1", compact && "flex-col")}>
87 {compact ? (
88 <SidebarHeaderIconButton
89 label="Search threads"
90 onClick={() => {
91 setOpen(true);
92 requestAnimationFrame(() => searchInputRef.current?.focus());
93 }}
94 >
95 <SearchIcon />
96 </SidebarHeaderIconButton>
97 ) : null}
98 <div
99 ref={searchFieldRef}
100 className={cn(
101 "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",
102 compact && "hidden",
103 )}
104 >
105 <SearchIcon className="size-4 shrink-0 text-[var(--sidebar-icon-color)]" />
106 <Input
107 ref={searchInputRef}
108 nativeInput
109 unstyled
110 type="search"
111 value={searchQuery}
112 onChange={(event) => onSearchQueryChange(event.currentTarget.value)}
113 onKeyDown={onSearchKeyDown}
114 placeholder="Search"
115 aria-label="Search threads"
116 role="combobox"
117 aria-autocomplete="list"
118 aria-expanded={resultsVisible}
119 aria-controls={resultsVisible ? "sidebar-thread-search-results" : undefined}
120 aria-activedescendant={
121 activeResultExists
122 ? `sidebar-thread-search-result-${activeSearchResultIndex}`
123 : undefined
124 }
125 className="min-w-0 flex-1 [&_[data-slot=input]]:h-auto [&_[data-slot=input]]:p-0 [&_[data-slot=input]]:leading-normal [&_[data-slot=input]]:text-sm [&_[data-slot=input]]:font-medium [&_[data-slot=input]]:text-sidebar-foreground [&_[data-slot=input]]:placeholder:text-[var(--sidebar-icon-color)]"
126 />
127 {isSearching ? (
128 <Button
129 type="button"
130 size="icon-micro"
131 variant="ghost"
132 className="shrink-0 text-sidebar-muted-foreground hover:bg-sidebar-control-surface hover:text-sidebar-foreground"
133 aria-label="Clear thread search"
134 onClick={() => {
135 onClearSearch();
136 searchInputRef.current?.focus();
137 }}
138 >
139 <XIcon className="size-3" />
140 </Button>
141 ) : null}
142 </div>
143 {/* Segmented well: the icons read as one control instead of three loose
144 buttons competing with the search field beside them. */}
145 <div
146 className={cn(
147 "flex shrink-0 items-center rounded-md bg-sidebar-control-surface/60 p-px",
148 compact && "flex-col",
149 )}
150 >
151 {hasProjects ? (
152 <>
153 {projectScope}
154 <SidebarHeaderIconButton label="New project" onClick={onNewProject}>
155 <FolderPlusIcon />
156 </SidebarHeaderIconButton>
157 </>
158 ) : null}
159 <SidebarHeaderIconButton
160 label="New thread"
161 tooltip={
162 showNewThreadInProjectHint ? (
163 <span className="flex flex-col gap-0.5">
164 <span>{newThreadLabel}</span>
165 <span className="text-muted-foreground">
166 New thread in current project: Shift+click
167 {newThreadInProjectShortcutLabel ? ` (${newThreadInProjectShortcutLabel})` : ""}
168 </span>
169 </span>
170 ) : (
171 newThreadLabel
172 )
173 }
174 disabled={newThreadDisabled}
175 onClick={onNewThread}
176 >
177 <SquarePenIcon />
178 </SidebarHeaderIconButton>
179 </div>
180 </div>
181 );
183​
184/**
185 * Icon button with a tooltip, sized for the header's segmented pair. Spreads
186 * unknown props through so it can serve as a popup trigger's render target,
187 * which injects its own handlers, ref and aria state.
188 */
189export function SidebarHeaderIconButton({
190 label,
191 tooltip = label,
192 className,
193 children,
194 ...rest
195}: {
196 /** Accessible name; also the tooltip unless `tooltip` says more. */
197 label: string;
198 tooltip?: ReactNode;
199 className?: string | undefined;
200 children?: ReactNode;
201} & Omit<
202 ComponentProps<typeof SidebarMenuButton>,
203 "children" | "className" | "tooltip" | "isActive" | "aria-label"
204>) {
205 return (
206 <Tooltip>
207 <TooltipTrigger
208 render={
209 <SidebarMenuButton
210 size="icon"
211 type="button"
212 aria-label={label}
213 {...rest}
214 className={cn(
215 "relative size-7 shrink-0 focus-visible:ring-offset-2 focus-visible:ring-offset-sidebar",
216 className,
217 )}
218 />
219 }
220 >
221 {children}
222 {/* Coarse-pointer hit area, matching the rest of the sidebar chrome. */}
223 <span
224 aria-hidden
225 className="pointer-events-none absolute left-1/2 top-1/2 size-[max(100%,3rem)] -translate-1/2 pointer-fine:hidden"
226 />
227 </TooltipTrigger>
228 <TooltipPopup side="top">{tooltip}</TooltipPopup>
229 </Tooltip>
230 );