The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { useEffect, useRef, useState, type KeyboardEvent } from "react"; |
| 2 | import { T, useGT } from "gt-react"; |
| 3 | import { ChevronDownIcon, ChevronUpIcon, XIcon } from "lucide-react"; |
| 4 | import { Button } from "../ui/button"; |
| 5 | import { InputGroup, InputGroupAddon, InputGroupInput } from "../ui/input-group"; |
| 6 | import { cn } from "~/lib/utils"; |
| 7 | import { |
| 8 | formatThreadFindCount, |
| 9 | nextThreadFindAnnouncement, |
| 10 | type ThreadFindAnnouncement, |
| 11 | } from "./threadFind"; |
| 12 | import { THREAD_DETAILS_CARD_GAP } from "./threadDetailsCardLayout"; |
| 13 | |
| 14 | interface ThreadFindBarProps { |
| 15 | readonly open: boolean; |
| 16 | readonly query: string; |
| 17 | readonly matchCount: number; |
| 18 | readonly counting?: boolean; |
| 19 | readonly status: "loading" | "error" | null; |
| 20 | readonly onRetry: () => void; |
| 21 | readonly activeIndex: number; |
| 22 | /** Bumped by each user search or step; background refreshes keep it. */ |
| 23 | readonly navigationId: number; |
| 24 | /** Whether the label shows the outcome of `navigationId`: a count or a failure. */ |
| 25 | readonly settled: boolean; |
| 26 | readonly focusRequestId: number; |
| 27 | readonly onQueryChange: (query: string) => void; |
| 28 | readonly onNext: () => void; |
| 29 | readonly onPrevious: () => void; |
| 30 | readonly onClose: () => void; |
| 31 | } |
| 32 | |
| 33 | export const THREAD_FIND_BAR_RESERVED_HEIGHT = 36 + THREAD_DETAILS_CARD_GAP; |
| 34 | |
| 35 | export function ThreadFindBar(props: ThreadFindBarProps) { |
| 36 | const inputRef = useRef<HTMLInputElement | null>(null); |
| 37 | const gt = useGT(); |
| 38 | |
| 39 | // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- focuses and selects the input DOM node when the bar opens or a find shortcut fires |
| 40 | useEffect(() => { |
| 41 | if (!props.open) return; |
| 42 | inputRef.current?.focus(); |
| 43 | inputRef.current?.select(); |
| 44 | }, [ |
| 45 | // 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. |
| 46 | props.focusRequestId, |
| 47 | props.open, |
| 48 | ]); |
| 49 | |
| 50 | const hasQuery = props.query.trim().length > 0; |
| 51 | const noResults = hasQuery && !props.status && props.matchCount === 0; |
| 52 | let label = ""; |
| 53 | if (hasQuery) { |
| 54 | label = props.counting ? "1/…" : formatThreadFindCount(props.activeIndex, props.matchCount); |
| 55 | } |
| 56 | if (props.status === "loading") label = gt("Searching…"); |
| 57 | if (props.status === "error") label = gt("Search failed"); |
| 58 | const [storedAnnouncement, setAnnouncement] = useState<ThreadFindAnnouncement | null>(null); |
| 59 | const announcement = nextThreadFindAnnouncement(storedAnnouncement, { |
| 60 | navigationId: props.navigationId, |
| 61 | text: label, |
| 62 | settled: props.settled || props.status === "loading", |
| 63 | provisional: props.status !== null, |
| 64 | }); |
| 65 | if (announcement !== storedAnnouncement) setAnnouncement(announcement); |
| 66 | |
| 67 | if (!props.open) return null; |
| 68 | |
| 69 | const navigationDisabled = |
| 70 | props.matchCount === 0 || props.status === "loading" || props.counting === true; |
| 71 | const handleKeyDown = (event: KeyboardEvent<HTMLInputElement>) => { |
| 72 | if (event.nativeEvent.isComposing || event.nativeEvent.keyCode === 229) return; |
| 73 | if (event.key === "Enter") { |
| 74 | event.preventDefault(); |
| 75 | event.stopPropagation(); |
| 76 | if (event.shiftKey) props.onPrevious(); |
| 77 | else props.onNext(); |
| 78 | } |
| 79 | }; |
| 80 | |
| 81 | return ( |
| 82 | <div |
| 83 | data-thread-find-bar |
| 84 | 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" |
| 85 | > |
| 86 | <InputGroup |
| 87 | variant="popover" |
| 88 | size="lg" |
| 89 | onContextMenu={(event) => event.stopPropagation()} |
| 90 | role="search" |
| 91 | aria-label={gt("Find in thread")} |
| 92 | aria-busy={props.status === "loading" || props.counting === true} |
| 93 | className="w-full" |
| 94 | > |
| 95 | <InputGroupInput |
| 96 | ref={inputRef} |
| 97 | type="search" |
| 98 | size="sm" |
| 99 | value={props.query} |
| 100 | aria-label={gt("Find in thread")} |
| 101 | placeholder={gt("Find in thread")} |
| 102 | maxLength={200} |
| 103 | spellCheck={false} |
| 104 | autoComplete="off" |
| 105 | onChange={(event) => props.onQueryChange(event.target.value)} |
| 106 | onKeyDown={handleKeyDown} |
| 107 | /> |
| 108 | <InputGroupAddon align="inline-end" inset="pill"> |
| 109 | <span aria-live="polite" className="sr-only"> |
| 110 | {announcement.text} |
| 111 | </span> |
| 112 | <span |
| 113 | aria-hidden |
| 114 | className={cn( |
| 115 | "min-w-10 text-center text-xs tabular-nums", |
| 116 | noResults ? "text-destructive" : "text-muted-foreground", |
| 117 | )} |
| 118 | > |
| 119 | {label} |
| 120 | </span> |
| 121 | {props.status === "error" ? ( |
| 122 | <Button size="xs" variant="ghost" onClick={props.onRetry}> |
| 123 | <T>Retry</T> |
| 124 | </Button> |
| 125 | ) : null} |
| 126 | <Button |
| 127 | size="icon-xs" |
| 128 | variant="ghost" |
| 129 | aria-label={gt("Previous match")} |
| 130 | disabled={navigationDisabled} |
| 131 | onClick={props.onPrevious} |
| 132 | > |
| 133 | <ChevronUpIcon /> |
| 134 | </Button> |
| 135 | <Button |
| 136 | size="icon-xs" |
| 137 | variant="ghost" |
| 138 | aria-label={gt("Next match")} |
| 139 | disabled={navigationDisabled} |
| 140 | onClick={props.onNext} |
| 141 | > |
| 142 | <ChevronDownIcon /> |
| 143 | </Button> |
| 144 | <Button |
| 145 | size="icon-xs" |
| 146 | variant="ghost" |
| 147 | aria-label={gt("Close find")} |
| 148 | onClick={props.onClose} |
| 149 | > |
| 150 | <XIcon /> |
| 151 | </Button> |
| 152 | </InputGroupAddon> |
| 153 | </InputGroup> |
| 154 | </div> |
| 155 | ); |
| 156 | } |