The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type { |
| 2 | OrchestrationThreadSearchMatch, |
| 3 | OrchestrationV2SearchThreadResult, |
| 4 | ScopedThreadRef, |
| 5 | } from "@t3tools/contracts"; |
| 6 | import { scopedThreadKey } from "@t3tools/client-runtime/environment"; |
| 7 | import { |
| 8 | findSearchTokenRanges, |
| 9 | messagePhrase, |
| 10 | messageSearchTokens, |
| 11 | } from "@t3tools/shared/paletteSearch"; |
| 12 | import { create } from "zustand"; |
| 13 | import type { ThemeAppearance } from "~/themePalette"; |
| 14 | import type { ThreadFindPositionReader, ThreadFindStart } from "./threadFind"; |
| 15 | |
| 16 | export interface ThreadFindLocalSelection { |
| 17 | readonly theme: ThemeAppearance; |
| 18 | readonly base: OrchestrationV2SearchThreadResult; |
| 19 | readonly baseNavigationId: number; |
| 20 | readonly activeIndex: number; |
| 21 | readonly match: NonNullable<OrchestrationV2SearchThreadResult["match"]>; |
| 22 | readonly navigationId: number; |
| 23 | } |
| 24 | |
| 25 | /** Find is open in at most one thread: the one the chat view shows. */ |
| 26 | export interface ThreadFindState { |
| 27 | readonly threadKey: string | null; |
| 28 | readonly query: string; |
| 29 | readonly offset: number; |
| 30 | readonly start: ThreadFindStart | undefined; |
| 31 | readonly focusRequestId: number; |
| 32 | readonly navigationId: number; |
| 33 | readonly requestId: number; |
| 34 | readonly localSelection: ThreadFindLocalSelection | null; |
| 35 | } |
| 36 | |
| 37 | export const CLOSED_THREAD_FIND: ThreadFindState = { |
| 38 | threadKey: null, |
| 39 | query: "", |
| 40 | offset: 0, |
| 41 | start: undefined, |
| 42 | focusRequestId: 0, |
| 43 | navigationId: 0, |
| 44 | requestId: 0, |
| 45 | localSelection: null, |
| 46 | }; |
| 47 | |
| 48 | export const useThreadFindStore = create<ThreadFindState>(() => CLOSED_THREAD_FIND); |
| 49 | |
| 50 | /** What find needs from the timeline that shows a thread. */ |
| 51 | interface ThreadFindTimeline { |
| 52 | readonly threadKey: string; |
| 53 | readonly readPosition: ThreadFindPositionReader; |
| 54 | /** Opening find is manual navigation: it releases live follow. */ |
| 55 | readonly navigate: () => void; |
| 56 | } |
| 57 | |
| 58 | let attachedTimeline: ThreadFindTimeline | null = null; |
| 59 | |
| 60 | function timelineFor(threadKey: string): ThreadFindTimeline | null { |
| 61 | return attachedTimeline?.threadKey === threadKey ? attachedTimeline : null; |
| 62 | } |
| 63 | |
| 64 | /** Attaches the timeline showing a thread; returns the detach. */ |
| 65 | export function attachThreadFindTimeline(timeline: ThreadFindTimeline): () => void { |
| 66 | attachedTimeline = timeline; |
| 67 | return () => { |
| 68 | if (attachedTimeline === timeline) attachedTimeline = null; |
| 69 | }; |
| 70 | } |
| 71 | |
| 72 | function readThreadFindPosition(threadKey: string, query: string): ThreadFindStart | undefined { |
| 73 | return timelineFor(threadKey)?.readPosition(query); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Navigates the timeline showing the thread when find newly opens there. A |
| 78 | * timeline that starts showing the thread later (a search result opened find |
| 79 | * before the route switched or the thread painted) releases follow itself as |
| 80 | * it opens on the thread. |
| 81 | */ |
| 82 | function announceOpening(previous: ThreadFindState, threadKey: string): void { |
| 83 | if (previous.threadKey !== threadKey) timelineFor(threadKey)?.navigate(); |
| 84 | } |
| 85 | |
| 86 | /** Opens find, or refocuses it and restarts its search from the viewport when already open. */ |
| 87 | export function openThreadFind(threadKey: string): void { |
| 88 | const previous = useThreadFindStore.getState(); |
| 89 | const current = previous.threadKey === threadKey ? previous : CLOSED_THREAD_FIND; |
| 90 | const query = current.query.trim(); |
| 91 | const start = query ? readThreadFindPosition(threadKey, query) : undefined; |
| 92 | announceOpening(previous, threadKey); |
| 93 | useThreadFindStore.setState({ |
| 94 | ...current, |
| 95 | threadKey, |
| 96 | focusRequestId: previous.focusRequestId + 1, |
| 97 | ...(query |
| 98 | ? { |
| 99 | start, |
| 100 | offset: 0, |
| 101 | localSelection: null, |
| 102 | navigationId: current.navigationId + 1, |
| 103 | requestId: current.navigationId + 1, |
| 104 | } |
| 105 | : {}), |
| 106 | }); |
| 107 | } |
| 108 | |
| 109 | export interface PendingThreadFind { |
| 110 | readonly query: string; |
| 111 | /** Where find starts, such as the message a palette result came from. */ |
| 112 | readonly start?: ThreadFindStart; |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Opens find on `query` in the thread, such as at the message a search result |
| 117 | * came from. The thread's view shows it once it is on screen. |
| 118 | */ |
| 119 | export function openThreadFindOnQuery(threadKey: string, find: PendingThreadFind): void { |
| 120 | const previous = useThreadFindStore.getState(); |
| 121 | announceOpening(previous, threadKey); |
| 122 | useThreadFindStore.setState({ |
| 123 | ...(previous.threadKey === threadKey ? previous : CLOSED_THREAD_FIND), |
| 124 | threadKey, |
| 125 | query: find.query, |
| 126 | offset: 0, |
| 127 | start: find.start, |
| 128 | localSelection: null, |
| 129 | focusRequestId: previous.focusRequestId + 1, |
| 130 | navigationId: previous.navigationId + 1, |
| 131 | requestId: previous.navigationId + 1, |
| 132 | }); |
| 133 | } |
| 134 | |
| 135 | export function setThreadFindQuery(threadKey: string, query: string): void { |
| 136 | const start = readThreadFindPosition(threadKey, query.trim()); |
| 137 | useThreadFindStore.setState((previous) => ({ |
| 138 | query, |
| 139 | offset: 0, |
| 140 | start, |
| 141 | localSelection: null, |
| 142 | navigationId: previous.navigationId + 1, |
| 143 | requestId: previous.navigationId + 1, |
| 144 | })); |
| 145 | } |
| 146 | |
| 147 | export function closeThreadFind(): void { |
| 148 | useThreadFindStore.setState(CLOSED_THREAD_FIND); |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Find belongs to the thread it was opened in: a route showing anything else |
| 153 | * closes it. Settings and Usage draw over the thread, so they keep it. |
| 154 | */ |
| 155 | export function retainThreadFindForRoute(route: { |
| 156 | readonly modalPage: boolean; |
| 157 | readonly threadRef: ScopedThreadRef | null; |
| 158 | }): void { |
| 159 | if (route.modalPage) return; |
| 160 | const { threadKey } = useThreadFindStore.getState(); |
| 161 | const shownKey = route.threadRef ? scopedThreadKey(route.threadRef) : null; |
| 162 | if (threadKey !== null && threadKey !== shownKey) closeThreadFind(); |
| 163 | } |
| 164 | |
| 165 | /** Whether find is open in `thread`, as long as find is available there. */ |
| 166 | export function useThreadFindOpen(thread: ScopedThreadRef | null, enabled: boolean): boolean { |
| 167 | const openThreadKey = useThreadFindStore((state) => state.threadKey); |
| 168 | return enabled && thread !== null && openThreadKey === scopedThreadKey(thread); |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * The find a message search result opens, at the matched message: the whole |
| 173 | * query when the snippet holds it as typed ("vp run dev"), else its longest |
| 174 | * word, as the most telling ("sidebar" in "fix sidebar"), written as the |
| 175 | * snippet writes it ("Café" for "cafe", since find matches accents). |
| 176 | */ |
| 177 | export function findForMessageMatch( |
| 178 | match: Pick<OrchestrationThreadSearchMatch, "snippet" | "messageId">, |
| 179 | query: string, |
| 180 | ): PendingThreadFind { |
| 181 | const start = match.messageId ? { start: { entryId: match.messageId, occurrence: 0 } } : {}; |
| 182 | // Apostrophes are straight in the query; straightening keeps offsets. |
| 183 | const together = messagePhrase(query)?.exec(match.snippet.replace(/[\u2018\u2019]/g, "'")); |
| 184 | if (together) { |
| 185 | return { |
| 186 | query: match.snippet.slice(together.index, together.index + together[0].length), |
| 187 | ...start, |
| 188 | }; |
| 189 | } |
| 190 | const word = |
| 191 | messageSearchTokens(query).long.reduce( |
| 192 | (longest, token) => (token.length > longest.length ? token : longest), |
| 193 | "", |
| 194 | ) || query; |
| 195 | const ranges = findSearchTokenRanges(match.snippet, word); |
| 196 | // A whole word first, as search ranked it: "Café", not "Cafe" in "Cafeteria". |
| 197 | const shown = |
| 198 | ranges.find((range) => !/[\p{L}\p{N}]/u.test(match.snippet[range.end] ?? "")) ?? ranges[0]; |
| 199 | return { query: shown ? match.snippet.slice(shown.start, shown.end) : word, ...start }; |
| 200 | } |