Open History to see the last commit
1import { useEffect, useRef, type KeyboardEvent } from "react";
2import { ChevronDownIcon, ChevronUpIcon, XIcon } from "lucide-react";
3import { Button } from "../ui/button";
4import { InputGroup, InputGroupAddon, InputGroupInput } from "../ui/input-group";
5import { cn } from "~/lib/utils";
6import { formatThreadFindCount } from "./threadFind";
7import { THREAD_DETAILS_CARD_GAP } from "./threadDetailsCardLayout";
8
9interface ThreadFindBarProps {
10 readonly open: boolean;
11 readonly query: string;
12 readonly matchCount: number;
13 readonly counting?: boolean;
14 readonly status: "loading" | "error" | null;
15 readonly onRetry: () => void;
16 readonly activeIndex: number;
17 readonly focusRequestId: number;
18 readonly onQueryChange: (query: string) => void;
19 readonly onNext: () => void;
20 readonly onPrevious: () => void;
21 readonly onClose: () => void;
22}
23
24export const THREAD_FIND_BAR_RESERVED_HEIGHT = 36 + THREAD_DETAILS_CARD_GAP;
25
26export function ThreadFindBar(props: ThreadFindBarProps) {
27 const inputRef = useRef<HTMLInputElement | null>(null);
28
29 useEffect(() => {
30 if (!props.open) return;
31 inputRef.current?.focus();
32 inputRef.current?.select();
33 }, [
34 // oxlint-disable-next-line react/exhaustive-effect-dependencies -- Each find shortcut bumps the request id, so an already-open bar refocuses and reselects its query.
35 props.focusRequestId,
36 props.open,
37 ]);
38
39 if (!props.open) return null;
40
41 const hasQuery = props.query.trim().length > 0;
42 const noResults = hasQuery && !props.status && props.matchCount === 0;
43 let label = "";
44 if (hasQuery) {
45 label = props.counting ? "1/…" : formatThreadFindCount(props.activeIndex, props.matchCount);
46 }
47 if (props.status === "loading") label = "Searching…";
48 if (props.status === "error") label = "Search failed";
49 const navigationDisabled =
50 props.matchCount === 0 || props.status === "loading" || props.counting === true;
51 const handleKeyDown = (event: KeyboardEvent<HTMLInputElement>) => {
52 if (event.nativeEvent.isComposing || event.nativeEvent.keyCode === 229) return;
53 if (event.key === "Enter") {
54 event.preventDefault();
55 event.stopPropagation();
56 if (event.shiftKey) props.onPrevious();
57 else props.onNext();
58 }
59 };
60
61 return (
62 <div
63 data-thread-find-bar
64 className="absolute top-3 right-3 z-40 w-[min(var(--thread-details-panel-width),calc(100%-1.5rem))] transition-opacity duration-150 ease-out starting:opacity-0 motion-reduce:transition-none"
65 >
66 <InputGroup
67 variant="popover"
68 size="lg"
69 onContextMenu={(event) => event.stopPropagation()}
70 role="search"
71 aria-label="Find in thread"
72 aria-busy={props.status === "loading" || props.counting === true}
73 className="w-full"
74 >
75 <InputGroupInput
76 ref={inputRef}
77 type="search"
78 size="sm"
79 value={props.query}
80 aria-label="Find in thread"
81 placeholder="Find in thread"
82 maxLength={200}
83 spellCheck={false}
84 autoComplete="off"
85 onChange={(event) => props.onQueryChange(event.target.value)}
86 onKeyDown={handleKeyDown}
87 />
88 <InputGroupAddon align="inline-end" inset="pill">
89 <span
90 aria-live="polite"
91 className={cn(
92 "min-w-10 text-center text-xs tabular-nums",
93 noResults ? "text-destructive" : "text-muted-foreground",
94 )}
95 >
96 {label}
97 </span>
98 {props.status === "error" ? (
99 <Button size="xs" variant="ghost" onClick={props.onRetry}>
100 Retry
101 </Button>
102 ) : null}
103 <Button
104 size="icon-xs"
105 variant="ghost"
106 aria-label="Previous match"
107 disabled={navigationDisabled}
108 onClick={props.onPrevious}
109 >
110 <ChevronUpIcon />
111 </Button>
112 <Button
113 size="icon-xs"
114 variant="ghost"
115 aria-label="Next match"
116 disabled={navigationDisabled}
117 onClick={props.onNext}
118 >
119 <ChevronDownIcon />
120 </Button>
121 <Button size="icon-xs" variant="ghost" aria-label="Close find" onClick={props.onClose}>
122 <XIcon />
123 </Button>
124 </InputGroupAddon>
125 </InputGroup>
126 </div>
127 );
128}