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

SidebarThreadHeader.tsx

162 lines5.2 KB

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 { SharedTooltipTrigger } from "../ui/shared-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;
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: 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({ context: "search field placeholder" })`Search`}
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: newThreadInProjectShortcutLabel }})`
110 : gt`New thread in current project: Shift+click`}
111 </span>
112 </span>
113 ) : (
114 newThreadLabel
115 )
116 }
117 disabled={newThreadDisabled}
118 onClick={onNewThread}
119 >
120 <SquarePenIcon />
121 </SidebarHeaderIconButton>
122 </div>
123 </div>
124 );
126​
127export function SidebarHeaderIconButton({
128 label,
129 tooltip = label,
130 className,
131 children,
132 ...rest
133}: {
134 label: string;
135 tooltip?: ReactNode;
136 className?: string | undefined;
137 children?: ReactNode;
138} & Omit<
139 ComponentProps<typeof SidebarMenuButton>,
140 "children" | "className" | "tooltip" | "isActive" | "aria-label"
141>) {
142 return (
143 <SharedTooltipTrigger
144 render={
145 <SidebarMenuButton
146 size="icon"
147 type="button"
148 aria-label={label}
149 {...rest}
150 className={cn("relative size-7 shrink-0", className)}
151 />
152 }
153 label={tooltip}
154 >
155 {children}
156 <span
157 aria-hidden
158 className="pointer-events-none absolute left-1/2 top-1/2 size-[max(100%,3rem)] -translate-1/2 pointer-fine:hidden"
159 />
160 </SharedTooltipTrigger>
161 );