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