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 // 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 ? `New thread (${newThreadShortcutLabel})`
59 : "New thread";
60
61 return (
62 <div className="flex items-center gap-1">
63 <div
64 ref={searchFieldRef}
65 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"
66 >
67 <SearchIcon className="size-4 shrink-0 text-(--sidebar-icon-color)" />
68 <SidebarInput
69 ref={searchInputRef}
70 nativeInput
71 type="search"
72 value={searchQuery}
73 onChange={(event) => onSearchQueryChange(event.currentTarget.value)}
74 onKeyDown={onSearchKeyDown}
75 placeholder="Search"
76 aria-label="Search threads"
77 role="combobox"
78 aria-autocomplete="list"
79 aria-expanded={resultsVisible}
80 aria-controls={resultsVisible ? "sidebar-thread-search-results" : undefined}
81 aria-activedescendant={
82 activeResultExists
83 ? `sidebar-thread-search-result-${activeSearchResultIndex}`
84 : undefined
85 }
86 className="min-w-0 flex-1"
87 />
88 {isSearching ? (
89 <Button
90 type="button"
91 size="icon-micro"
92 variant="ghost-muted"
93 className="shrink-0"
94 aria-label="Clear thread search"
95 onClick={() => {
96 onClearSearch();
97 searchInputRef.current?.focus();
98 }}
99 >
100 <XIcon className="size-3" />
101 </Button>
102 ) : null}
103 </div>
104 <div className="flex shrink-0 items-center">
105 {hasProjects ? (
106 <>
107 {projectScope}
108 <SidebarHeaderIconButton label="Add project" onClick={onNewProject}>
109 <FolderPlusIcon />
110 </SidebarHeaderIconButton>
111 </>
112 ) : null}
113 <SidebarHeaderIconButton
114 label="New thread"
115 tooltip={
116 showNewThreadInProjectHint ? (
117 <span className="flex flex-col gap-0.5">
118 <span>{newThreadLabel}</span>
119 <span className="text-muted-foreground">
120 New thread in current project: Shift+click
121 {newThreadInProjectShortcutLabel ? ` (${newThreadInProjectShortcutLabel})` : ""}
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
138export 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}