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

queries.ts

134 lines3.9 KB

1import type { EnvironmentId, OrchestrationThread, ThreadId } from "@t3tools/contracts";
2import * as Option from "effect/Option";
3import { useEffect, useMemo, useState } from "react";
4​
5import { orchestrationEnvironment } from "./orchestration";
6import { projectEnvironment } from "./projects";
7import { useEnvironmentQuery } from "./query";
8import { useEnvironmentThread } from "./threads";
9import { vcsEnvironment } from "./vcs";
10import {
11 buildCheckpointDiffTargets,
12 normalizeComposerPathSearchQuery,
13 type CheckpointDiffTarget,
14} from "./queryTargets";
15​
16const COMPOSER_PATH_SEARCH_DEBOUNCE_MS = 200;
17const COMPOSER_PATH_SEARCH_LIMIT = 20;
18const VCS_REF_LIST_LIMIT = 100;
19​
20export interface ThreadDetailView {
21 readonly data: OrchestrationThread | null;
22 readonly error: string | null;
23 readonly isPending: boolean;
24 readonly isDeleted: boolean;
26​
27export interface ComposerPathSearchTarget {
28 readonly environmentId: EnvironmentId | null;
29 readonly cwd: string | null;
30 readonly query: string | null;
32​
33function useDebouncedValue<A>(value: A, delayMs: number): A {
34 const [debounced, setDebounced] = useState(value);
35​
36 useEffect(() => {
37 const timer = setTimeout(() => {
38 setDebounced(value);
39 }, delayMs);
40 return () => {
41 clearTimeout(timer);
42 };
43 }, [delayMs, value]);
44​
45 return debounced;
47​
48export function useThreadDetail(
49 environmentId: EnvironmentId | null,
50 threadId: ThreadId | null,
51): ThreadDetailView {
52 const state = useEnvironmentThread(environmentId, threadId);
53 return {
54 data: Option.getOrNull(state.data),
55 error: Option.getOrNull(state.error),
56 isPending: state.status === "synchronizing",
57 isDeleted: state.status === "deleted",
58 };
60​
61export function useBranches(input: {
62 readonly environmentId: EnvironmentId | null;
63 readonly cwd: string | null;
64 readonly query?: string | null;
65}) {
66 const query = input.query?.trim() ?? "";
67 return useEnvironmentQuery(
68 input.environmentId !== null && input.cwd !== null
69 ? vcsEnvironment.listRefs({
70 environmentId: input.environmentId,
71 input: {
72 cwd: input.cwd,
73 ...(query.length > 0 ? { query } : {}),
74 limit: VCS_REF_LIST_LIMIT,
75 },
76 })
77 : null,
78 );
80​
81export function useComposerPathSearch(target: ComposerPathSearchTarget) {
82 const normalizedTarget = useMemo(
83 () => ({
84 environmentId: target.environmentId,
85 cwd: target.cwd,
86 query: normalizeComposerPathSearchQuery(target.query),
87 }),
88 [target.cwd, target.environmentId, target.query],
89 );
90 const debouncedTarget = useDebouncedValue(normalizedTarget, COMPOSER_PATH_SEARCH_DEBOUNCE_MS);
91 const result = useEnvironmentQuery(
92 debouncedTarget.environmentId !== null &&
93 debouncedTarget.cwd !== null &&
94 debouncedTarget.query.length > 0
95 ? projectEnvironment.searchEntries({
96 environmentId: debouncedTarget.environmentId,
97 input: {
98 cwd: debouncedTarget.cwd,
99 query: debouncedTarget.query,
100 limit: COMPOSER_PATH_SEARCH_LIMIT,
101 },
102 })
103 : null,
104 );
105​
106 return {
107 entries: result.data?.entries ?? [],
108 error: result.error,
109 isPending: normalizedTarget.query !== debouncedTarget.query || result.isPending,
110 refresh: result.refresh,
111 };
113​
114export function useCheckpointDiff(target: CheckpointDiffTarget) {
115 const targets = useMemo(
116 () => buildCheckpointDiffTargets(target),
117 [
118 target.environmentId,
119 target.fromTurnCount,
120 target.ignoreWhitespace,
121 target.threadId,
122 target.toTurnCount,
123 ],
124 );
125 const fullThread = useEnvironmentQuery(
126 targets.fullThread === null
127 ? null
128 : orchestrationEnvironment.fullThreadDiff(targets.fullThread),
129 );
130 const turn = useEnvironmentQuery(
131 targets.turn === null ? null : orchestrationEnvironment.turnDiff(targets.turn),
132 );
133 return targets.fullThread === null ? turn : fullThread;