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

projectFilesQueryState.ts

199 lines6.1 KB

1import { useAtomRefresh, useAtomValue } from "@effect/atom-react";
2import type {
3 EnvironmentId,
4 ProjectListEntriesResult,
5 ProjectReadFileResult,
6} from "@t3tools/contracts";
7import * as Cause from "effect/Cause";
8import * as Option from "effect/Option";
9import { AsyncResult, Atom } from "effect/unstable/reactivity";
10import { useCallback } from "react";
11​
12import { appAtomRegistry } from "~/rpc/atomRegistry";
13import { projectEnvironment } from "~/state/projects";
14import { useProjectPathSearch } from "~/state/queries";
15import { executeAtomQuery } from "@t3tools/client-runtime/state/runtime";
16​
17const EMPTY_PROJECT_FILE_PATH = "";
18const EMPTY_PROJECT_FILE_QUERY_ATOM = Atom.make(
19 AsyncResult.initial<ProjectReadFileResult, never>(false),
20).pipe(Atom.withLabel("project-file-query:empty"));
21function optimisticFileAtom(environmentId: EnvironmentId, cwd: string, relativePath: string) {
22 return projectEnvironment.optimisticFile({ environmentId, cwd, relativePath });
24​
25interface ProjectQueryState<A> {
26 readonly data: A | null;
27 readonly error: string | null;
28 readonly isPending: boolean;
29 readonly refresh: () => void;
31​
32export function getProjectEntriesQueryAtom(environmentId: EnvironmentId, cwd: string) {
33 return projectEnvironment.listEntries({ environmentId, input: { cwd } });
35​
36export function getProjectFileQueryAtom(
37 environmentId: EnvironmentId,
38 cwd: string,
39 relativePath: string | null,
40) {
41 return projectEnvironment.readFile({
42 environmentId,
43 input: { cwd, relativePath: relativePath ?? EMPTY_PROJECT_FILE_PATH },
44 });
46​
47export function setProjectFileQueryData(
48 environmentId: EnvironmentId,
49 cwd: string,
50 relativePath: string,
51 contents: string,
52): void {
53 appAtomRegistry.set(optimisticFileAtom(environmentId, cwd, relativePath), {
54 confirmedAgainst: undefined,
55 data: {
56 relativePath,
57 contents,
58 byteLength: new TextEncoder().encode(contents).byteLength,
59 truncated: false,
60 },
61 });
63​
64export function getOptimisticProjectFileQueryData(
65 environmentId: EnvironmentId,
66 cwd: string,
67 relativePath: string,
68): ProjectReadFileResult | null {
69 return appAtomRegistry.get(optimisticFileAtom(environmentId, cwd, relativePath))?.data ?? null;
71​
72export function confirmProjectFileQueryData(
73 environmentId: EnvironmentId,
74 cwd: string,
75 relativePath: string,
76 contents: string,
77): boolean {
78 const atom = optimisticFileAtom(environmentId, cwd, relativePath);
79 const optimisticFile = appAtomRegistry.get(atom);
80 if (optimisticFile?.data.contents !== contents) return false;
81​
82 const queryAtom = getProjectFileQueryAtom(environmentId, cwd, relativePath);
83 const confirmed = {
84 ...optimisticFile,
85 confirmedAgainst: appAtomRegistry.get(queryAtom),
86 };
87 appAtomRegistry.set(atom, confirmed);
88 appAtomRegistry.refresh(queryAtom);
89 void executeAtomQuery(appAtomRegistry, queryAtom, {
90 reportDefect: false,
91 reportFailure: false,
92 }).then((result) => {
93 if (result._tag === "Success" && appAtomRegistry.get(atom) === confirmed) {
94 appAtomRegistry.set(atom, null);
95 }
96 });
97 return true;
99​
100export function resolveProjectFileQueryData(
101 environmentId: EnvironmentId,
102 cwd: string,
103 relativePath: string | null,
104 data: ProjectReadFileResult | null,
105): ProjectReadFileResult | null {
106 if (relativePath === null) return data;
107 return appAtomRegistry.get(optimisticFileAtom(environmentId, cwd, relativePath))?.data ?? data;
109​
110export function clearProjectFileQueryData(
111 environmentId: EnvironmentId,
112 cwd: string,
113 relativePath: string,
114): void {
115 appAtomRegistry.set(optimisticFileAtom(environmentId, cwd, relativePath), null);
117​
118function errorMessage<A>(result: AsyncResult.AsyncResult<A, unknown>): string | null {
119 if (result._tag !== "Failure") return null;
120 const cause = Cause.squash(result.cause);
121 return cause instanceof Error ? cause.message : "Workspace query failed.";
123​
124export function useProjectEntriesQuery(
125 environmentId: EnvironmentId,
126 cwd: string,
127): ProjectQueryState<ProjectListEntriesResult> {
128 const atom = getProjectEntriesQueryAtom(environmentId, cwd);
129 const result = useAtomValue(atom);
130 const refreshAtom = useAtomRefresh(atom);
131 const refresh = useCallback(() => refreshAtom(), [refreshAtom]);
132 return {
133 data: Option.getOrNull(AsyncResult.value(result)),
134 error: errorMessage(result),
135 isPending: result.waiting,
136 refresh,
137 };
139​
140/**
141 * Backing query for the project file picker: a debounced, bounded, file-only
142 * server search. An empty query is a valid request — the index answers it
143 * with frecency-ordered files, so the picker's initial view is recent files
144 * without transferring the full workspace listing. `matchedQuery` is the
145 * query the returned entries were computed for, so the caller can highlight
146 * against results instead of half-typed input.
147 */
148export function useProjectFilePickerQuery(
149 environmentId: EnvironmentId,
150 cwd: string,
151 query: string,
152 limit: number,
153 options?: { readonly imageOnly?: boolean },
154) {
155 const search = useProjectPathSearch(
156 {
157 environmentId,
158 cwd,
159 query,
160 kind: "file",
161 ...(options?.imageOnly ? { imageOnly: true } : {}),
162 },
163 limit,
164 { allowEmptyQuery: true },
165 );
166​
167 return {
168 entries: search.isPending ? [] : search.entries,
169 error: search.error,
170 isPending: search.isPending,
171 matchedQuery: search.searchedQuery,
172 };
174​
175export function useProjectFileQuery(
176 environmentId: EnvironmentId,
177 cwd: string,
178 relativePath: string | null,
179 enabled = true,
180): ProjectQueryState<ProjectReadFileResult> {
181 const atom = enabled
182 ? getProjectFileQueryAtom(environmentId, cwd, relativePath)
183 : EMPTY_PROJECT_FILE_QUERY_ATOM;
184 const result = useAtomValue(atom);
185 const refreshAtom = useAtomRefresh(atom);
186 const refresh = useCallback(() => refreshAtom(), [refreshAtom]);
187 const data = Option.getOrNull(AsyncResult.value(result));
188 const optimisticResult = useAtomValue(
189 optimisticFileAtom(environmentId, cwd, relativePath ?? EMPTY_PROJECT_FILE_PATH),
190 );
191 const optimisticFile = relativePath === null ? null : optimisticResult;
192​
193 return {
194 data: optimisticFile?.data ?? data,
195 error: errorMessage(result),
196 isPending: result.waiting,
197 refresh,
198 };