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

threadFindActionBus.ts

80 lines3.3 KB

1import type { OrchestrationThreadSearchMatch } from "@t3tools/contracts";
2import {
3 findSearchTokenRanges,
4 messagePhrase,
5 messageSearchTokens,
6} from "@t3tools/shared/paletteSearch";
7import type { ThreadFindStart } from "./threadFind";
8​
9const THREAD_FIND_OPEN_EVENT = "t3:thread-find-open";
10​
11export function requestThreadFindOpen(): void {
12 window.dispatchEvent(new Event(THREAD_FIND_OPEN_EVENT));
14​
15export function subscribeThreadFindOpen(listener: () => void): () => void {
16 window.addEventListener(THREAD_FIND_OPEN_EVENT, listener);
17 return () => window.removeEventListener(THREAD_FIND_OPEN_EVENT, listener);
19​
20const THREAD_FIND_QUERY_EVENT = "t3:thread-find-query";
21interface PendingFind {
22 readonly query: string;
23 /** Where find starts, such as the message a palette result came from. */
24 readonly start?: ThreadFindStart;
26// A find a palette message result asked for, until that thread's view takes
27// it. One the view never took (the navigation failed) lets go after a while,
28// so a later visit does not open an old find.
29const PENDING_FIND_MS = 30_000;
30let pendingFind: (PendingFind & { readonly threadKey: string; readonly at: number }) | null = null;
31​
32/** Opens find on `query` in the thread's view, now or once it mounts. */
33export function requestThreadFindQuery(threadKey: string, find: PendingFind): void {
34 pendingFind = { threadKey, at: Date.now(), ...find };
35 window.dispatchEvent(new Event(THREAD_FIND_QUERY_EVENT));
37​
38/** The find waiting for this thread's view; answers once. */
39export function takeThreadFindQuery(threadKey: string): PendingFind | null {
40 if (pendingFind?.threadKey !== threadKey) return null;
41 const { at, threadKey: _threadKey, ...find } = pendingFind;
42 pendingFind = null;
43 return Date.now() - at < PENDING_FIND_MS ? find : null;
45​
46/**
47 * The find a message search result opens, at the matched message: the whole
48 * query when the snippet holds it as typed ("vp run dev"), else its longest
49 * word, as the most telling ("sidebar" in "fix sidebar"), written as the
50 * snippet writes it ("Café" for "cafe", since find matches accents).
51 */
52export function findForMessageMatch(
53 match: Pick<OrchestrationThreadSearchMatch, "snippet" | "messageId">,
54 query: string,
55): PendingFind {
56 const start = match.messageId ? { start: { entryId: match.messageId, occurrence: 0 } } : {};
57 // Apostrophes are straight in the query; straightening keeps offsets.
58 const together = messagePhrase(query)?.exec(match.snippet.replace(/[\u2018\u2019]/g, "'"));
59 if (together) {
60 return {
61 query: match.snippet.slice(together.index, together.index + together[0].length),
62 ...start,
63 };
64 }
65 const word =
66 messageSearchTokens(query).long.reduce(
67 (longest, token) => (token.length > longest.length ? token : longest),
68 "",
69 ) || query;
70 const ranges = findSearchTokenRanges(match.snippet, word);
71 // A whole word first, as search ranked it: "Café", not "Cafe" in "Cafeteria".
72 const shown =
73 ranges.find((range) => !/[\p{L}\p{N}]/u.test(match.snippet[range.end] ?? "")) ?? ranges[0];
74 return { query: shown ? match.snippet.slice(shown.start, shown.end) : word, ...start };
76​
77export function subscribeThreadFindQuery(listener: () => void): () => void {
78 window.addEventListener(THREAD_FIND_QUERY_EVENT, listener);
79 return () => window.removeEventListener(THREAD_FIND_QUERY_EVENT, listener);