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

queries.ts

181 lines5.7 KB

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 { useEnvironmentThread } from "./threads";
16import { vcsEnvironment } from "./vcs";
17import {
18 buildCheckpointDiffTargets,
19 normalizeComposerPathSearchQuery,
20 type CheckpointDiffTarget,
21} from "./queryTargets";
22​
23const COMPOSER_PATH_SEARCH_DEBOUNCE_MS = 200;
24const COMPOSER_PATH_SEARCH_LIMIT = 20;
25const THREAD_SEARCH_DEBOUNCE_MS = 200;
26const VCS_REF_LIST_LIMIT = 100;
27const EMPTY_THREAD_SEARCH_MATCHES: ReadonlyArray<EnvironmentThreadSearchMatch> = Object.freeze([]);
28const EMPTY_THREAD_SEARCH_ATOM = Atom.make({
29 matches: EMPTY_THREAD_SEARCH_MATCHES,
30 isLoading: false,
31}).pipe(Atom.withLabel("mobile:thread-search:empty"));
32​
33const threadSearchResultsAtom = createThreadSearchResultsAtomFamily({
34 getSearchAtom: (environmentId, query) =>
35 orchestrationEnvironment.threadSearch({
36 environmentId,
37 input: { query },
38 }),
39 labelPrefix: "mobile:thread-search",
40});
41​
42export interface ThreadDetailView {
43 readonly data: OrchestrationThread | null;
44 readonly error: string | null;
45 readonly isPending: boolean;
46 readonly isDeleted: boolean;
48​
49export interface ComposerPathSearchTarget {
50 readonly environmentId: EnvironmentId | null;
51 readonly cwd: string | null;
52 readonly query: string | null;
54​
55function useDebouncedValue<A>(value: A, delayMs: number): A {
56 const [debounced, setDebounced] = useState(value);
57​
58 useEffect(() => {
59 const timer = setTimeout(() => {
60 setDebounced(value);
61 }, delayMs);
62 return () => {
63 clearTimeout(timer);
64 };
65 }, [delayMs, value]);
66​
67 return debounced;
69​
70export function useThreadSearch(
71 environmentIds: ReadonlyArray<EnvironmentId>,
72 query: string,
73): {
74 readonly matches: ReadonlyArray<EnvironmentThreadSearchMatch>;
75 readonly isPending: boolean;
76} {
77 const normalizedQuery = query.trim();
78 const debouncedQuery = useDebouncedValue(normalizedQuery, THREAD_SEARCH_DEBOUNCE_MS);
79 const canSearch = environmentIds.length > 0 && normalizedQuery.length >= 2;
80 const settledQuery = canSearch && normalizedQuery === debouncedQuery ? debouncedQuery : null;
81 const searchKey = useMemo(
82 () => (settledQuery === null ? null : makeThreadSearchKey(environmentIds, settledQuery)),
83 [environmentIds, settledQuery],
84 );
85 const result = useAtomValue(
86 searchKey === null ? EMPTY_THREAD_SEARCH_ATOM : threadSearchResultsAtom(searchKey),
87 );
88 const isDebouncing = canSearch && normalizedQuery !== debouncedQuery;
89 return {
90 matches: isDebouncing ? EMPTY_THREAD_SEARCH_MATCHES : result.matches,
91 isPending: canSearch && (isDebouncing || result.isLoading),
92 };
94​
95export function useThreadDetail(
96 environmentId: EnvironmentId | null,
97 threadId: ThreadId | null,
98): ThreadDetailView {
99 const state = useEnvironmentThread(environmentId, threadId);
100 return {
101 data: Option.getOrNull(state.data),
102 error: Option.getOrNull(state.error),
103 isPending: state.status === "synchronizing",
104 isDeleted: state.status === "deleted",
105 };
107​
108export function useBranches(input: {
109 readonly environmentId: EnvironmentId | null;
110 readonly cwd: string | null;
111 readonly query?: string | null;
112}) {
113 const query = input.query?.trim() ?? "";
114 return useEnvironmentQuery(
115 input.environmentId !== null && input.cwd !== null
116 ? vcsEnvironment.listRefs({
117 environmentId: input.environmentId,
118 input: {
119 cwd: input.cwd,
120 ...(query.length > 0 ? { query } : {}),
121 limit: VCS_REF_LIST_LIMIT,
122 },
123 })
124 : null,
125 );
127​
128export function useComposerPathSearch(target: ComposerPathSearchTarget) {
129 const normalizedTarget = useMemo(
130 () => ({
131 environmentId: target.environmentId,
132 cwd: target.cwd,
133 query: normalizeComposerPathSearchQuery(target.query),
134 }),
135 [target.cwd, target.environmentId, target.query],
136 );
137 const debouncedTarget = useDebouncedValue(normalizedTarget, COMPOSER_PATH_SEARCH_DEBOUNCE_MS);
138 const result = useEnvironmentQuery(
139 debouncedTarget.environmentId !== null &&
140 debouncedTarget.cwd !== null &&
141 debouncedTarget.query.length > 0
142 ? projectEnvironment.searchEntries({
143 environmentId: debouncedTarget.environmentId,
144 input: {
145 cwd: debouncedTarget.cwd,
146 query: debouncedTarget.query,
147 limit: COMPOSER_PATH_SEARCH_LIMIT,
148 },
149 })
150 : null,
151 );
152​
153 return {
154 entries: result.data?.entries ?? [],
155 error: result.error,
156 isPending: normalizedTarget.query !== debouncedTarget.query || result.isPending,
157 refresh: result.refresh,
158 };
160​
161export function useCheckpointDiff(target: CheckpointDiffTarget) {
162 const targets = useMemo(
163 () => buildCheckpointDiffTargets(target),
164 [
165 target.environmentId,
166 target.fromTurnCount,
167 target.ignoreWhitespace,
168 target.threadId,
169 target.toTurnCount,
170 ],
171 );
172 const fullThread = useEnvironmentQuery(
173 targets.fullThread === null
174 ? null
175 : orchestrationEnvironment.fullThreadDiff(targets.fullThread),
176 );
177 const turn = useEnvironmentQuery(
178 targets.turn === null ? null : orchestrationEnvironment.turnDiff(targets.turn),
179 );
180 return targets.fullThread === null ? turn : fullThread;