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

queries.ts

112 lines3.2 KB

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