The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { useGT } from "gt-react"; |
| 2 | import { useEffect, useRef, useState, type KeyboardEvent } from "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 { previewBridge } from "./previewBridge"; |
| 8 | |
| 9 | interface PreviewFindBarProps { |
| 10 | readonly tabId: string; |
| 11 | readonly focusRequestId: number; |
| 12 | readonly onClose: () => void; |
| 13 | } |
| 14 | |
| 15 | export function PreviewFindBar({ tabId, focusRequestId, onClose }: PreviewFindBarProps) { |
| 16 | const gt = useGT(); |
| 17 | const inputRef = useRef<HTMLInputElement | null>(null); |
| 18 | const [query, setQuery] = useState(""); |
| 19 | const [result, setResult] = useState<{ active: number; matches: number } | null>(null); |
| 20 | |
| 21 | // 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 |
| 22 | useEffect(() => { |
| 23 | inputRef.current?.focus(); |
| 24 | inputRef.current?.select(); |
| 25 | // 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. |
| 26 | }, [focusRequestId]); |
| 27 | |
| 28 | // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- subscribes to Chromium find results for this tab and stops the search on unmount |
| 29 | useEffect(() => { |
| 30 | if (!previewBridge) return; |
| 31 | const unsubscribe = previewBridge.onFindResult((event) => { |
| 32 | if (event.tabId !== tabId) return; |
| 33 | // Chromium reports -1 for a field an update leaves unchanged. |
| 34 | setResult((previous) => ({ |
| 35 | active: event.activeMatchOrdinal >= 0 ? event.activeMatchOrdinal : (previous?.active ?? 0), |
| 36 | matches: event.matches >= 0 ? event.matches : (previous?.matches ?? 0), |
| 37 | })); |
| 38 | }); |
| 39 | return () => { |
| 40 | unsubscribe(); |
| 41 | void previewBridge?.stopFindInPage(tabId).catch(() => undefined); |
| 42 | }; |
| 43 | }, [tabId]); |
| 44 | |
| 45 | const search = (text: string, options: { forward: boolean; newSession: boolean }) => { |
| 46 | if (!previewBridge) return; |
| 47 | void previewBridge.findInPage(tabId, text, options).catch(() => undefined); |
| 48 | }; |
| 49 | |
| 50 | const handleQueryChange = (next: string) => { |
| 51 | setQuery(next); |
| 52 | if (next.length === 0) { |
| 53 | setResult(null); |
| 54 | void previewBridge?.stopFindInPage(tabId).catch(() => undefined); |
| 55 | return; |
| 56 | } |
| 57 | search(next, { forward: true, newSession: true }); |
| 58 | }; |
| 59 | |
| 60 | const step = (forward: boolean) => { |
| 61 | if (query.length === 0) return; |
| 62 | search(query, { forward, newSession: false }); |
| 63 | }; |
| 64 | |
| 65 | const handleKeyDown = (event: KeyboardEvent<HTMLInputElement>) => { |
| 66 | if (event.nativeEvent.isComposing || event.nativeEvent.keyCode === 229) return; |
| 67 | if (event.key === "Enter") { |
| 68 | event.preventDefault(); |
| 69 | event.stopPropagation(); |
| 70 | step(!event.shiftKey); |
| 71 | } else if (event.key === "Escape") { |
| 72 | event.preventDefault(); |
| 73 | event.stopPropagation(); |
| 74 | onClose(); |
| 75 | } |
| 76 | }; |
| 77 | |
| 78 | const hasQuery = query.length > 0; |
| 79 | const noResults = hasQuery && result !== null && result.matches === 0; |
| 80 | const label = |
| 81 | hasQuery && result ? `${result.matches === 0 ? 0 : result.active}/${result.matches}` : ""; |
| 82 | const navigationDisabled = !result || result.matches === 0; |
| 83 | |
| 84 | return ( |
| 85 | <div className="absolute top-3 right-3 z-40 w-[min(22rem,calc(100%-1.5rem))]"> |
| 86 | <InputGroup |
| 87 | variant="popover" |
| 88 | size="lg" |
| 89 | role="search" |
| 90 | aria-label={gt("Find in page")} |
| 91 | className="w-full" |
| 92 | > |
| 93 | <InputGroupInput |
| 94 | ref={inputRef} |
| 95 | type="search" |
| 96 | size="sm" |
| 97 | value={query} |
| 98 | aria-label={gt("Find in page")} |
| 99 | placeholder={gt("Find in page")} |
| 100 | maxLength={200} |
| 101 | spellCheck={false} |
| 102 | autoComplete="off" |
| 103 | onChange={(event) => handleQueryChange(event.target.value)} |
| 104 | onKeyDown={handleKeyDown} |
| 105 | /> |
| 106 | <InputGroupAddon align="inline-end" inset="pill"> |
| 107 | <span |
| 108 | aria-live="polite" |
| 109 | className={cn( |
| 110 | "min-w-10 text-center text-xs tabular-nums", |
| 111 | noResults ? "text-destructive" : "text-muted-foreground", |
| 112 | )} |
| 113 | > |
| 114 | {label} |
| 115 | </span> |
| 116 | <Button |
| 117 | size="icon-xs" |
| 118 | variant="ghost" |
| 119 | aria-label={gt("Previous match")} |
| 120 | disabled={navigationDisabled} |
| 121 | onClick={() => step(false)} |
| 122 | > |
| 123 | <ChevronUpIcon /> |
| 124 | </Button> |
| 125 | <Button |
| 126 | size="icon-xs" |
| 127 | variant="ghost" |
| 128 | aria-label={gt("Next match")} |
| 129 | disabled={navigationDisabled} |
| 130 | onClick={() => step(true)} |
| 131 | > |
| 132 | <ChevronDownIcon /> |
| 133 | </Button> |
| 134 | <Button size="icon-xs" variant="ghost" aria-label={gt("Close find")} onClick={onClose}> |
| 135 | <XIcon /> |
| 136 | </Button> |
| 137 | </InputGroupAddon> |
| 138 | </InputGroup> |
| 139 | </div> |
| 140 | ); |
| 141 | } |