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

SidebarThreadHeader.tsx

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