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

ThreadFindBar.tsx

123 lines4.0 KB

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;
23​
24/**
25 * Room the open bar takes at the top of the chat canvas: its `h-9` height plus
26 * the shared 12px gap below it, so the details card starts on the same rhythm.
27 */
28export const THREAD_FIND_BAR_RESERVED_HEIGHT = 36 + THREAD_DETAILS_CARD_GAP;
29​
30export function ThreadFindBar(props: ThreadFindBarProps) {
31 const inputRef = useRef<HTMLInputElement | null>(null);
32​
33 useEffect(() => {
34 if (!props.open) return;
35 inputRef.current?.focus();
36 inputRef.current?.select();
37 }, [props.focusRequestId, props.open]);
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 <InputGroup
63 variant="popover"
64 size="lg"
65 onContextMenu={(event) => event.stopPropagation()}
66 role="search"
67 aria-label="Find in thread"
68 aria-busy={props.status === "loading" || props.counting === true}
69 className="absolute top-3 right-3 z-40 w-[min(24rem,calc(100%-1.5rem))]"
70 >
71 <InputGroupInput
72 ref={inputRef}
73 type="search"
74 size="sm"
75 value={props.query}
76 aria-label="Find in thread"
77 placeholder="Find in thread"
78 maxLength={200}
79 spellCheck={false}
80 autoComplete="off"
81 onChange={(event) => props.onQueryChange(event.target.value)}
82 onKeyDown={handleKeyDown}
83 />
84 <InputGroupAddon align="inline-end" inset="pill">
85 <span
86 aria-live="polite"
87 className={cn(
88 "min-w-10 text-center text-xs tabular-nums",
89 noResults ? "text-destructive" : "text-muted-foreground",
90 )}
91 >
92 {label}
93 </span>
94 {props.status === "error" ? (
95 <Button size="xs" variant="ghost" onClick={props.onRetry}>
96 Retry
97 </Button>
98 ) : null}
99 <Button
100 size="icon-xs"
101 variant="ghost"
102 aria-label="Previous match"
103 disabled={navigationDisabled}
104 onClick={props.onPrevious}
105 >
106 <ChevronUpIcon />
107 </Button>
108 <Button
109 size="icon-xs"
110 variant="ghost"
111 aria-label="Next match"
112 disabled={navigationDisabled}
113 onClick={props.onNext}
114 >
115 <ChevronDownIcon />
116 </Button>
117 <Button size="icon-xs" variant="ghost" aria-label="Close find" onClick={props.onClose}>
118 <XIcon />
119 </Button>
120 </InputGroupAddon>
121 </InputGroup>
122 );