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