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

ThreadLinkPicker.logic.ts

31 lines1.2 KB

1import type { EnvironmentId } from "@t3tools/contracts";
2​
3import { normalizeSearchText } from "~/lib/utils";
4​
5export function threadLinkMatcher(query: string, locale?: string) {
6 const localQuery = query.trim().toLocaleLowerCase(locale);
7 const foldedQuery = normalizeSearchText(query);
8 return (text: string) =>
9 text.toLocaleLowerCase(locale).includes(localQuery) ||
10 normalizeSearchText(text).includes(foldedQuery);
12​
13/** The link is stored on `environmentId`, so a thread on another environment is never a candidate. */
14export function threadLinkCandidates<
15 Thread extends {
16 readonly environmentId: EnvironmentId;
17 readonly archivedAt: string | null;
18 readonly updatedAt: string;
19 },
20>(threads: readonly Thread[], environmentId: EnvironmentId, matches: (thread: Thread) => boolean) {
21 const isActiveMatch = (thread: Thread) => thread.archivedAt === null && matches(thread);
22 const candidates = threads
23 .filter((thread) => thread.environmentId === environmentId && isActiveMatch(thread))
24 .toSorted((a, b) => b.updatedAt.localeCompare(a.updatedAt));
25 return {
26 candidates,
27 matchesElsewhere:
28 candidates.length === 0 &&
29 threads.some((thread) => thread.environmentId !== environmentId && isActiveMatch(thread)),
30 };