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

ThreadFindBar.tsx

124 lines4.1 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​
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 }, [props.focusRequestId, props.open]);
34​
35 if (!props.open) return null;
36​
37 const hasQuery = props.query.trim().length > 0;
38 const noResults = hasQuery && !props.status && props.matchCount === 0;
39 let label = "";
40 if (hasQuery) {
41 label = props.counting ? "1/…" : formatThreadFindCount(props.activeIndex, props.matchCount);
42 }
43 if (props.status === "loading") label = "Searching…";
44 if (props.status === "error") label = "Search failed";
45 const navigationDisabled =
46 props.matchCount === 0 || props.status === "loading" || props.counting === true;
47 const handleKeyDown = (event: KeyboardEvent<HTMLInputElement>) => {
48 if (event.nativeEvent.isComposing || event.nativeEvent.keyCode === 229) return;
49 if (event.key === "Enter") {
50 event.preventDefault();
51 event.stopPropagation();
52 if (event.shiftKey) props.onPrevious();
53 else props.onNext();
54 }
55 };
56​
57 return (
58 <div
59 data-thread-find-bar
60 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"
61 >
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="w-full"
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 </div>
123 );