Open History to see the last commit
1import { useAtomValue } from "@effect/atom-react";
2import type {
3 EnvironmentProject,
4 EnvironmentThread,
5 EnvironmentThreadShell,
6} from "@t3tools/client-runtime/state/shell";
7import {
8 type EnvironmentThreadStatus,
9 mergeEnvironmentThread,
10} from "@t3tools/client-runtime/state/threads";
11import type { ScopedProjectRef, ScopedThreadRef, ServerConfig } from "@t3tools/contracts";
12import type { EnvironmentId } from "@t3tools/contracts";
13import { Atom } from "effect/unstable/reactivity";
14import { useMemo } from "react";
15import { appAtomRegistry } from "../rpc/atomRegistry";
16import { environmentProjects } from "./projects";
17import { environmentServerConfigsAtom } from "./server";
18import {
19 allEnvironmentProjectSnapshotsReadyAtom,
20 allEnvironmentShellsBootstrappedAtom,
21} from "./shell";
22import { environmentThreadDetails, environmentThreadShells } from "./threads";
23
24const EMPTY_THREAD_REFS: ReadonlyArray<ScopedThreadRef> = Object.freeze([]);
25
26const EMPTY_PROJECT_ATOM = Atom.make<EnvironmentProject | null>(null).pipe(
27 Atom.withLabel("web-project:empty"),
28);
29const EMPTY_THREAD_REFS_ATOM = Atom.make(EMPTY_THREAD_REFS).pipe(
30 Atom.withLabel("web-thread-refs:empty"),
31);
32const EMPTY_THREAD_SHELL_ATOM = Atom.make<EnvironmentThreadShell | null>(null).pipe(
33 Atom.withLabel("web-thread-shell:empty"),
34);
35const EMPTY_THREAD_DETAIL_ATOM = Atom.make<EnvironmentThread | null>(null).pipe(
36 Atom.withLabel("web-thread-detail:empty"),
37);
38const EMPTY_THREAD_STATUS_ATOM = Atom.make<EnvironmentThreadStatus>("empty").pipe(
39 Atom.withLabel("web-thread-status:empty"),
40);
41
42const activeEnvironmentIdAtom = Atom.make<EnvironmentId | null>(null).pipe(
43 Atom.keepAlive,
44 Atom.withLabel("web-active-environment-id"),
45);
46
47export function useActiveEnvironmentId(): EnvironmentId | null {
48 return useAtomValue(activeEnvironmentIdAtom);
49}
50
51export function setActiveEnvironmentId(environmentId: EnvironmentId | null): void {
52 appAtomRegistry.set(activeEnvironmentIdAtom, environmentId);
53}
54
55export function useThreadRefs(): ReadonlyArray<ScopedThreadRef> {
56 return useAtomValue(environmentThreadShells.threadRefsAtom);
57}
58
59export function useEnvironmentThreadRefs(
60 environmentId: EnvironmentId | null,
61): ReadonlyArray<ScopedThreadRef> {
62 return useAtomValue(
63 environmentId === null
64 ? EMPTY_THREAD_REFS_ATOM
65 : environmentThreadShells.environmentThreadRefsAtom(environmentId),
66 );
67}
68
69export function useProjects(): ReadonlyArray<EnvironmentProject> {
70 return useAtomValue(environmentProjects.projectsAtom);
71}
72
73export function useServerConfigs(): ReadonlyMap<EnvironmentId, ServerConfig> {
74 return useAtomValue(environmentServerConfigsAtom);
75}
76
77export function useThreadShells(): ReadonlyArray<EnvironmentThreadShell> {
78 return useAtomValue(environmentThreadShells.threadShellsAtom);
79}
80
81export function useAllEnvironmentShellsBootstrapped(): boolean {
82 return useAtomValue(allEnvironmentShellsBootstrappedAtom);
83}
84
85export function useAllEnvironmentProjectSnapshotsReady(): boolean {
86 return useAtomValue(allEnvironmentProjectSnapshotsReadyAtom);
87}
88
89export function useThreadShellsForProjectRefs(
90 refs: ReadonlyArray<ScopedProjectRef>,
91): ReadonlyArray<EnvironmentThreadShell> {
92 return useAtomValue(environmentThreadShells.threadShellsForProjectRefsAtom(refs));
93}
94
95export function useProject(ref: ScopedProjectRef | null): EnvironmentProject | null {
96 return useAtomValue(ref === null ? EMPTY_PROJECT_ATOM : environmentProjects.projectAtom(ref));
97}
98
99export function useThreadShell(ref: ScopedThreadRef | null): EnvironmentThreadShell | null {
100 return useAtomValue(
101 ref === null ? EMPTY_THREAD_SHELL_ATOM : environmentThreadShells.threadShellAtom(ref),
102 );
103}
104
105export function useThreadDetail(ref: ScopedThreadRef | null): EnvironmentThread | null {
106 return useAtomValue(
107 ref === null ? EMPTY_THREAD_DETAIL_ATOM : environmentThreadDetails.detailAtom(ref),
108 );
109}
110
111export function useThreadStatus(ref: ScopedThreadRef | null): EnvironmentThreadStatus {
112 return useAtomValue(
113 ref === null ? EMPTY_THREAD_STATUS_ATOM : environmentThreadDetails.statusAtom(ref),
114 );
115}
116
117export function resolveThreadDetailRef(
118 ref: ScopedThreadRef | null,
119 options: {
120 shellExists: boolean;
121 waitForShell: boolean;
122 },
123): ScopedThreadRef | null {
124 return ref !== null && (!options.waitForShell || options.shellExists) ? ref : null;
125}
126
127/** Detail collections composed with shell-authoritative thread/workspace metadata. */
128export function useThread(
129 ref: ScopedThreadRef | null,
130 options?: {
131 /**
132 * Client-reserved draft thread ids do not exist on the server until the
133 * first send. Waiting for the shell index avoids polling the detail
134 * endpoint for an intentionally missing thread during that window.
135 */
136 waitForShell?: boolean;
137 },
138): EnvironmentThread | null {
139 const shell = useThreadShell(ref);
140 const detail = useThreadDetail(
141 resolveThreadDetailRef(ref, {
142 shellExists: shell !== null,
143 waitForShell: options?.waitForShell === true,
144 }),
145 );
146 return useMemo(() => mergeEnvironmentThread(detail, shell), [detail, shell]);
147}
148
149export function readProject(ref: ScopedProjectRef): EnvironmentProject | null {
150 return appAtomRegistry.get(environmentProjects.projectAtom(ref));
151}
152
153export function readProjects(): ReadonlyArray<EnvironmentProject> {
154 return appAtomRegistry.get(environmentProjects.projectsAtom);
155}
156
157/** Resolves when the project event reaches the live client store. */
158export function waitForProject(
159 ref: ScopedProjectRef,
160 timeoutMs = 10_000,
161): Promise<EnvironmentProject> {
162 const current = readProject(ref);
163 if (current !== null) return Promise.resolve(current);
164
165 return new Promise((resolve, reject) => {
166 let unsubscribe: (() => void) | null = null;
167 const timeout = setTimeout(() => {
168 unsubscribe?.();
169 reject(new Error("The project did not appear in the desktop app."));
170 }, timeoutMs);
171 const finish = (project: EnvironmentProject | null) => {
172 if (project === null) return;
173 clearTimeout(timeout);
174 unsubscribe?.();
175 resolve(project);
176 };
177 unsubscribe = appAtomRegistry.subscribe(environmentProjects.projectAtom(ref), finish);
178 finish(readProject(ref));
179 });
180}
181
182export function readThreadShell(ref: ScopedThreadRef): EnvironmentThreadShell | null {
183 return appAtomRegistry.get(environmentThreadShells.threadShellAtom(ref));
184}
185
186/** Whether the environment's server understands thread.settle/unsettle.
187 False for pre-settlement servers (capability defaults false on decode),
188 so clients under version skew fall back instead of erroring. */
189export function readEnvironmentSupportsSettlement(environmentId: EnvironmentId): boolean {
190 return (
191 appAtomRegistry.get(environmentServerConfigsAtom).get(environmentId)?.environment.capabilities
192 .threadSettlement === true
193 );
194}
195
196/** Whether the environment's server understands thread.snooze/unsnooze.
197 Same version-skew contract as settlement. */
198export function readEnvironmentSupportsSnooze(environmentId: EnvironmentId): boolean {
199 return (
200 appAtomRegistry.get(environmentServerConfigsAtom).get(environmentId)?.environment.capabilities
201 .threadSnooze === true
202 );
203}
204
205/** Whether the environment's server understands thread.pin/unpin.
206 Same version-skew contract as settlement. */
207export function readEnvironmentSupportsPinning(environmentId: EnvironmentId): boolean {
208 return (
209 appAtomRegistry.get(environmentServerConfigsAtom).get(environmentId)?.environment.capabilities
210 .threadPinning === true
211 );
212}
213
214/** Whether the environment's server understands thread title regeneration.
215 Same version-skew contract as settlement. */
216export function readEnvironmentSupportsTitleRegeneration(environmentId: EnvironmentId): boolean {
217 return (
218 appAtomRegistry.get(environmentServerConfigsAtom).get(environmentId)?.environment.capabilities
219 .threadTitleRegeneration === true
220 );
221}
222
223/** Whether the environment's server understands thread.pin.reorder (and
224 orderKey on thread.pin). Same version-skew contract as settlement. */
225export function readEnvironmentSupportsPinReorder(environmentId: EnvironmentId): boolean {
226 return (
227 appAtomRegistry.get(environmentServerConfigsAtom).get(environmentId)?.environment.capabilities
228 .threadPinReorder === true
229 );
230}
231
232export function readEnvironmentSupportsActiveReorder(environmentId: EnvironmentId): boolean {
233 return (
234 appAtomRegistry.get(environmentServerConfigsAtom).get(environmentId)?.environment.capabilities
235 .threadActiveReorder === true
236 );
237}
238
239export function readEnvironmentThreadRefs(
240 environmentId: EnvironmentId,
241): ReadonlyArray<ScopedThreadRef> {
242 return appAtomRegistry.get(environmentThreadShells.environmentThreadRefsAtom(environmentId));
243}
244
245export function readThreadShells(): ReadonlyArray<EnvironmentThreadShell> {
246 return appAtomRegistry.get(environmentThreadShells.threadShellsAtom);
247}