Open History to see the last commit
1import type { EnvironmentId, OrchestrationThread, ThreadId } from "@t3tools/contracts";
2import {
3 createThreadSearchResultsAtomFamily,
4 makeThreadSearchKey,
5 type EnvironmentThreadSearchMatch,
6} from "@t3tools/client-runtime/state/thread-search";
7import { useAtomValue } from "@effect/atom-react";
8import * as Option from "effect/Option";
9import { Atom } from "effect/unstable/reactivity";
10import { useEffect, useMemo, useState } from "react";
11
12import { orchestrationEnvironment } from "./orchestration";
13import { projectEnvironment } from "./projects";
14import { useEnvironmentQuery } from "./query";
15import { vcsEnvironment } from "./vcs";
16import {
17 buildCheckpointDiffTargets,
18 normalizeComposerPathSearchQuery,
19 type CheckpointDiffTarget,
20} from "./queryTargets";
21
22const COMPOSER_PATH_SEARCH_DEBOUNCE_MS = 200;
23const COMPOSER_PATH_SEARCH_LIMIT = 20;
24const THREAD_SEARCH_DEBOUNCE_MS = 200;
25const VCS_REF_LIST_LIMIT = 100;
26const EMPTY_THREAD_SEARCH_MATCHES: ReadonlyArray<EnvironmentThreadSearchMatch> = Object.freeze([]);
27const EMPTY_THREAD_SEARCH_ATOM = Atom.make({
28 matches: EMPTY_THREAD_SEARCH_MATCHES,
29 isLoading: false,
30}).pipe(Atom.withLabel("mobile:thread-search:empty"));
31
32const threadSearchResultsAtom = createThreadSearchResultsAtomFamily({
33 getSearchAtom: (environmentId, query) =>
34 orchestrationEnvironment.threadSearch({
35 environmentId,
36 input: { query },
37 }),
38 labelPrefix: "mobile:thread-search",
39});
40
41export interface ComposerPathSearchTarget {
42 readonly environmentId: EnvironmentId | null;
43 readonly cwd: string | null;
44 readonly query: string | null;
45}
46
47function useDebouncedValue<A>(value: A, delayMs: number): A {
48 const [debounced, setDebounced] = useState(value);
49
50 useEffect(() => {
51 const timer = setTimeout(() => {
52 setDebounced(value);
53 }, delayMs);
54 return () => {
55 clearTimeout(timer);
56 };
57 }, [delayMs, value]);
58
59 return debounced;
60}
61
62export function useThreadSearch(
63 environmentIds: ReadonlyArray<EnvironmentId>,
64 query: string,
65): {
66 readonly matches: ReadonlyArray<EnvironmentThreadSearchMatch>;
67 readonly isPending: boolean;
68} {
69 const normalizedQuery = query.trim();
70 const debouncedQuery = useDebouncedValue(normalizedQuery, THREAD_SEARCH_DEBOUNCE_MS);
71 const canSearch = environmentIds.length > 0 && normalizedQuery.length >= 2;
72 const settledQuery = canSearch && normalizedQuery === debouncedQuery ? debouncedQuery : null;
73 const searchKey = useMemo(
74 () => (settledQuery === null ? null : makeThreadSearchKey(environmentIds, settledQuery)),
75 [environmentIds, settledQuery],
76 );
77 const result = useAtomValue(
78 searchKey === null ? EMPTY_THREAD_SEARCH_ATOM : threadSearchResultsAtom(searchKey),
79 );
80 const isDebouncing = canSearch && normalizedQuery !== debouncedQuery;
81 return {
82 matches: isDebouncing ? EMPTY_THREAD_SEARCH_MATCHES : result.matches,
83 isPending: canSearch && (isDebouncing || result.isLoading),
84 };
85}
86
87export function useBranches(input: {
88 readonly environmentId: EnvironmentId | null;
89 readonly cwd: string | null;
90 readonly query?: string | null;
91}) {
92 const query = input.query?.trim() ?? "";
93 return useEnvironmentQuery(
94 input.environmentId !== null && input.cwd !== null
95 ? vcsEnvironment.listRefs({
96 environmentId: input.environmentId,
97 input: {
98 cwd: input.cwd,
99 ...(query.length > 0 ? { query } : {}),
100 limit: VCS_REF_LIST_LIMIT,
101 },
102 })
103 : null,
104 );
105}
106
107export function useComposerPathSearch(target: ComposerPathSearchTarget) {
108 const normalizedTarget = useMemo(
109 () => ({
110 environmentId: target.environmentId,
111 cwd: target.cwd,
112 query: normalizeComposerPathSearchQuery(target.query),
113 }),
114 [target.cwd, target.environmentId, target.query],
115 );
116 const debouncedTarget = useDebouncedValue(normalizedTarget, COMPOSER_PATH_SEARCH_DEBOUNCE_MS);
117 const result = useEnvironmentQuery(
118 debouncedTarget.environmentId !== null &&
119 debouncedTarget.cwd !== null &&
120 debouncedTarget.query.length > 0
121 ? projectEnvironment.searchEntries({
122 environmentId: debouncedTarget.environmentId,
123 input: {
124 cwd: debouncedTarget.cwd,
125 query: debouncedTarget.query,
126 limit: COMPOSER_PATH_SEARCH_LIMIT,
127 },
128 })
129 : null,
130 );
131
132 return {
133 entries: result.data?.entries ?? [],
134 error: result.error,
135 isPending: normalizedTarget.query !== debouncedTarget.query || result.isPending,
136 refresh: result.refresh,
137 };
138}
139
140export function useCheckpointDiff(target: CheckpointDiffTarget) {
141 const targets = useMemo(
142 () => buildCheckpointDiffTargets(target),
143 [
144 target.environmentId,
145 target.fromTurnCount,
146 target.ignoreWhitespace,
147 target.threadId,
148 target.toTurnCount,
149 ],
150 );
151 const fullThread = useEnvironmentQuery(
152 targets.fullThread === null
153 ? null
154 : orchestrationEnvironment.fullThreadDiff(targets.fullThread),
155 );
156 const turn = useEnvironmentQuery(
157 targets.turn === null ? null : orchestrationEnvironment.turnDiff(targets.turn),
158 );
159 return targets.fullThread === null ? turn : fullThread;
160}