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