Open History to see the last commit
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 { executeAtomQuery } from "@t3tools/client-runtime/state/runtime";
15
16const EMPTY_PROJECT_FILE_PATH = "";
17const EMPTY_PROJECT_FILE_QUERY_ATOM = Atom.make(
18 AsyncResult.initial<ProjectReadFileResult, never>(false),
19).pipe(Atom.withLabel("project-file-query:empty"));
20function optimisticFileAtom(environmentId: EnvironmentId, cwd: string, relativePath: string) {
21 return projectEnvironment.optimisticFile({ environmentId, cwd, relativePath });
22}
23
24interface ProjectQueryState<A> {
25 readonly data: A | null;
26 readonly error: string | null;
27 readonly isPending: boolean;
28 readonly refresh: () => void;
29}
30
31export function getProjectEntriesQueryAtom(environmentId: EnvironmentId, cwd: string) {
32 return projectEnvironment.listEntries({ environmentId, input: { cwd } });
33}
34
35export function getProjectFileQueryAtom(
36 environmentId: EnvironmentId,
37 cwd: string,
38 relativePath: string | null,
39) {
40 return projectEnvironment.readFile({
41 environmentId,
42 input: { cwd, relativePath: relativePath ?? EMPTY_PROJECT_FILE_PATH },
43 });
44}
45
46export function setProjectFileQueryData(
47 environmentId: EnvironmentId,
48 cwd: string,
49 relativePath: string,
50 contents: string,
51): void {
52 appAtomRegistry.set(optimisticFileAtom(environmentId, cwd, relativePath), {
53 confirmedAgainst: undefined,
54 data: {
55 relativePath,
56 contents,
57 byteLength: new TextEncoder().encode(contents).byteLength,
58 truncated: false,
59 },
60 });
61}
62
63export function getOptimisticProjectFileQueryData(
64 environmentId: EnvironmentId,
65 cwd: string,
66 relativePath: string,
67): ProjectReadFileResult | null {
68 return appAtomRegistry.get(optimisticFileAtom(environmentId, cwd, relativePath))?.data ?? null;
69}
70
71export function confirmProjectFileQueryData(
72 environmentId: EnvironmentId,
73 cwd: string,
74 relativePath: string,
75 contents: string,
76): boolean {
77 const atom = optimisticFileAtom(environmentId, cwd, relativePath);
78 const optimisticFile = appAtomRegistry.get(atom);
79 if (optimisticFile?.data.contents !== contents) return false;
80
81 const queryAtom = getProjectFileQueryAtom(environmentId, cwd, relativePath);
82 const confirmed = {
83 ...optimisticFile,
84 confirmedAgainst: appAtomRegistry.get(queryAtom),
85 };
86 appAtomRegistry.set(atom, confirmed);
87 appAtomRegistry.refresh(queryAtom);
88 void executeAtomQuery(appAtomRegistry, queryAtom, {
89 reportDefect: false,
90 reportFailure: false,
91 }).then((result) => {
92 if (result._tag === "Success" && appAtomRegistry.get(atom) === confirmed) {
93 appAtomRegistry.set(atom, null);
94 }
95 });
96 return true;
97}
98
99export function resolveProjectFileQueryData(
100 environmentId: EnvironmentId,
101 cwd: string,
102 relativePath: string | null,
103 data: ProjectReadFileResult | null,
104): ProjectReadFileResult | null {
105 if (relativePath === null) return data;
106 return appAtomRegistry.get(optimisticFileAtom(environmentId, cwd, relativePath))?.data ?? data;
107}
108
109export function clearProjectFileQueryData(
110 environmentId: EnvironmentId,
111 cwd: string,
112 relativePath: string,
113): void {
114 appAtomRegistry.set(optimisticFileAtom(environmentId, cwd, relativePath), null);
115}
116
117function errorMessage<A>(result: AsyncResult.AsyncResult<A, unknown>): string | null {
118 if (result._tag !== "Failure") return null;
119 const cause = Cause.squash(result.cause);
120 return cause instanceof Error ? cause.message : "Workspace query failed.";
121}
122
123export function useProjectEntriesQuery(
124 environmentId: EnvironmentId,
125 cwd: string,
126): ProjectQueryState<ProjectListEntriesResult> {
127 const atom = getProjectEntriesQueryAtom(environmentId, cwd);
128 const result = useAtomValue(atom);
129 const refreshAtom = useAtomRefresh(atom);
130 const refresh = useCallback(() => refreshAtom(), [refreshAtom]);
131 return {
132 data: Option.getOrNull(AsyncResult.value(result)),
133 error: errorMessage(result),
134 isPending: result.waiting,
135 refresh,
136 };
137}
138
139export function useProjectFileQuery(
140 environmentId: EnvironmentId,
141 cwd: string,
142 relativePath: string | null,
143 enabled = true,
144): ProjectQueryState<ProjectReadFileResult> {
145 const atom = enabled
146 ? getProjectFileQueryAtom(environmentId, cwd, relativePath)
147 : EMPTY_PROJECT_FILE_QUERY_ATOM;
148 const result = useAtomValue(atom);
149 const refreshAtom = useAtomRefresh(atom);
150 const refresh = useCallback(() => refreshAtom(), [refreshAtom]);
151 const data = Option.getOrNull(AsyncResult.value(result));
152 const optimisticResult = useAtomValue(
153 optimisticFileAtom(environmentId, cwd, relativePath ?? EMPTY_PROJECT_FILE_PATH),
154 );
155 const optimisticFile = relativePath === null ? null : optimisticResult;
156
157 return {
158 data: optimisticFile?.data ?? data,
159 error: errorMessage(result),
160 isPending: result.waiting,
161 refresh,
162 };
163}