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