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 EMPTY_THREAD_HISTORY_META,
9 type EnvironmentThreadStatus,
10 type ThreadHistoryMeta,
11} from "@t3tools/client-runtime/state/threads";
12import type { ScopedProjectRef, ScopedThreadRef, ServerConfig } from "@t3tools/contracts";
13import type { EnvironmentId, OrchestrationV2ProjectedTurnItem } from "@t3tools/contracts";
14import { Atom } from "effect/reactivity";
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";
23import { waitForAtomValue } from "./waitForAtomValue";
24
25const EMPTY_THREAD_REFS: ReadonlyArray<ScopedThreadRef> = Object.freeze([]);
26const EMPTY_VISIBLE_TURN_ITEMS: ReadonlyArray<OrchestrationV2ProjectedTurnItem> = Object.freeze([]);
27
28const EMPTY_PROJECT_ATOM = Atom.make<EnvironmentProject | null>(null).pipe(
29 Atom.withLabel("web-project:empty"),
30);
31const EMPTY_THREAD_REFS_ATOM = Atom.make(EMPTY_THREAD_REFS).pipe(
32 Atom.withLabel("web-thread-refs:empty"),
33);
34const EMPTY_THREAD_SHELLS_ATOM = Atom.make<ReadonlyArray<EnvironmentThreadShell>>(
35 Object.freeze([]),
36).pipe(Atom.withLabel("web-thread-shells:empty"));
37const EMPTY_THREAD_SHELL_ATOM = Atom.make<EnvironmentThreadShell | null>(null).pipe(
38 Atom.withLabel("web-thread-shell:empty"),
39);
40const EMPTY_THREAD_PROJECTION_ATOM = Atom.make<EnvironmentThread | null>(null).pipe(
41 Atom.withLabel("web-thread-projection:empty"),
42);
43const EMPTY_THREAD_STATUS_ATOM = Atom.make<EnvironmentThreadStatus>("empty").pipe(
44 Atom.withLabel("web-thread-status:empty"),
45);
46const EMPTY_VISIBLE_TURN_ITEMS_ATOM = Atom.make(EMPTY_VISIBLE_TURN_ITEMS).pipe(
47 Atom.withLabel("web-thread-visible-turn-items:empty"),
48);
49const EMPTY_THREAD_HISTORY_ATOM = Atom.make<ThreadHistoryMeta>(EMPTY_THREAD_HISTORY_META).pipe(
50 Atom.withLabel("web-thread-history:empty"),
51);
52
53const activeEnvironmentIdAtom = Atom.make<EnvironmentId | null>(null).pipe(
54 Atom.keepAlive,
55 Atom.withLabel("web-active-environment-id"),
56);
57
58export function useActiveEnvironmentId(): EnvironmentId | null {
59 return useAtomValue(activeEnvironmentIdAtom);
60}
61
62export function setActiveEnvironmentId(environmentId: EnvironmentId | null): void {
63 appAtomRegistry.set(activeEnvironmentIdAtom, environmentId);
64}
65
66export function useThreadRefs(): ReadonlyArray<ScopedThreadRef> {
67 return useAtomValue(environmentThreadShells.threadRefsAtom);
68}
69
70export function useEnvironmentThreadRefs(
71 environmentId: EnvironmentId | null,
72): ReadonlyArray<ScopedThreadRef> {
73 return useAtomValue(
74 environmentId === null
75 ? EMPTY_THREAD_REFS_ATOM
76 : environmentThreadShells.environmentThreadRefsAtom(environmentId),
77 );
78}
79
80export function useProjects(): ReadonlyArray<EnvironmentProject> {
81 return useAtomValue(environmentProjects.projectsAtom);
82}
83
84export function useServerConfigs(): ReadonlyMap<EnvironmentId, ServerConfig> {
85 return useAtomValue(environmentServerConfigsAtom);
86}
87
88/** Every thread shell. Pass `enabled: false` to read a stable empty list and
89 skip re-rendering on each shell update while the caller does not need them. */
90export function useThreadShells(enabled = true): ReadonlyArray<EnvironmentThreadShell> {
91 return useAtomValue(
92 enabled ? environmentThreadShells.threadShellsAtom : EMPTY_THREAD_SHELLS_ATOM,
93 );
94}
95
96export function useAllEnvironmentShellsBootstrapped(): boolean {
97 return useAtomValue(allEnvironmentShellsBootstrappedAtom);
98}
99
100export function useAllEnvironmentProjectSnapshotsReady(): boolean {
101 return useAtomValue(allEnvironmentProjectSnapshotsReadyAtom);
102}
103
104export function useThreadShellsForProjectRefs(
105 refs: ReadonlyArray<ScopedProjectRef>,
106): ReadonlyArray<EnvironmentThreadShell> {
107 return useAtomValue(environmentThreadShells.threadShellsForProjectRefsAtom(refs));
108}
109
110export function useProject(ref: ScopedProjectRef | null): EnvironmentProject | null {
111 return useAtomValue(ref === null ? EMPTY_PROJECT_ATOM : environmentProjects.projectAtom(ref));
112}
113
114export function useThreadShell(ref: ScopedThreadRef | null): EnvironmentThreadShell | null {
115 return useAtomValue(
116 ref === null ? EMPTY_THREAD_SHELL_ATOM : environmentThreadShells.threadShellAtom(ref),
117 );
118}
119
120export function useThreadProjection(ref: ScopedThreadRef | null): EnvironmentThread | null {
121 return useAtomValue(
122 ref === null ? EMPTY_THREAD_PROJECTION_ATOM : environmentThreadDetails.threadAtom(ref),
123 );
124}
125
126export function useThreadStatus(ref: ScopedThreadRef | null): EnvironmentThreadStatus {
127 return useAtomValue(
128 ref === null ? EMPTY_THREAD_STATUS_ATOM : environmentThreadDetails.statusAtom(ref),
129 );
130}
131
132export function useThreadHistory(ref: ScopedThreadRef | null): ThreadHistoryMeta {
133 return useAtomValue(
134 ref === null ? EMPTY_THREAD_HISTORY_ATOM : environmentThreadDetails.historyAtom(ref),
135 );
136}
137
138export function resolveThreadDetailRef(
139 ref: ScopedThreadRef | null,
140 options: {
141 shellExists: boolean;
142 waitForShell: boolean;
143 },
144): ScopedThreadRef | null {
145 return ref !== null && (!options.waitForShell || options.shellExists) ? ref : null;
146}
147
148export function useThreadVisibleTurnItems(
149 ref: ScopedThreadRef | null,
150): ReadonlyArray<OrchestrationV2ProjectedTurnItem> {
151 return useAtomValue(
152 ref === null
153 ? EMPTY_VISIBLE_TURN_ITEMS_ATOM
154 : environmentThreadDetails.visibleTurnItemsAtom(ref),
155 );
156}
157
158export function readProject(ref: ScopedProjectRef): EnvironmentProject | null {
159 return appAtomRegistry.get(environmentProjects.projectAtom(ref));
160}
161
162export function readProjects(): ReadonlyArray<EnvironmentProject> {
163 return appAtomRegistry.get(environmentProjects.projectsAtom);
164}
165
166/** Resolves when the project event reaches the live client store. */
167export function waitForProject(
168 ref: ScopedProjectRef,
169 timeoutMs = 10_000,
170): Promise<EnvironmentProject> {
171 const current = readProject(ref);
172 if (current !== null) return Promise.resolve(current);
173
174 return new Promise((resolve, reject) => {
175 let unsubscribe: (() => void) | null = null;
176 const timeout = setTimeout(() => {
177 unsubscribe?.();
178 reject(new Error("The project did not appear in the desktop app."));
179 }, timeoutMs);
180 const finish = (project: EnvironmentProject | null) => {
181 if (project === null) return;
182 clearTimeout(timeout);
183 unsubscribe?.();
184 resolve(project);
185 };
186 unsubscribe = appAtomRegistry.subscribe(environmentProjects.projectAtom(ref), finish);
187 finish(readProject(ref));
188 });
189}
190
191export function readThreadShell(ref: ScopedThreadRef): EnvironmentThreadShell | null {
192 return appAtomRegistry.get(environmentThreadShells.threadShellAtom(ref));
193}
194
195export function waitForThreadShell(ref: ScopedThreadRef, timeoutMs = 5_000): Promise<boolean> {
196 return waitForAtomValue({
197 registry: appAtomRegistry,
198 atom: environmentThreadShells.threadShellAtom(ref),
199 predicate: (thread) => thread !== null,
200 timeoutMs,
201 });
202}
203
204/** Whether the environment hosts preview tabs in its own browser (`runtime: "server"`),
205 so clients without Electron can still use the Browser panel. */
206export function useEnvironmentSupportsServerBrowser(environmentId: EnvironmentId | null): boolean {
207 const configs = useServerConfigs();
208 return (
209 environmentId !== null &&
210 configs.get(environmentId)?.environment.capabilities.serverBrowser === true
211 );
212}
213
214export function readEnvironmentSupportsServerBrowser(environmentId: EnvironmentId): boolean {
215 return (
216 appAtomRegistry.get(environmentServerConfigsAtom).get(environmentId)?.environment.capabilities
217 .serverBrowser === true
218 );
219}
220
221export function readEnvironmentSupportsTitleRegeneration(environmentId: EnvironmentId): boolean {
222 return (
223 appAtomRegistry.get(environmentServerConfigsAtom).get(environmentId)?.environment.capabilities
224 .threadTitleRegeneration === true
225 );
226}
227
228/** Whether the environment's server understands thread.pin/unpin.
229 Same version-skew contract as settlement. */
230export function readEnvironmentSupportsPinning(environmentId: EnvironmentId): boolean {
231 return (
232 appAtomRegistry.get(environmentServerConfigsAtom).get(environmentId)?.environment.capabilities
233 .threadPinning === true
234 );
235}
236
237/** Whether the environment's server understands thread.pin.reorder (and
238 orderKey on thread.pin). Same version-skew contract as settlement. */
239export function readEnvironmentSupportsPinReorder(environmentId: EnvironmentId): boolean {
240 return (
241 appAtomRegistry.get(environmentServerConfigsAtom).get(environmentId)?.environment.capabilities
242 .threadPinReorder === true
243 );
244}
245
246/** Whether the environment's server understands thread.auto-settle.set.
247 Same version-skew contract as settlement. */
248export function readEnvironmentSupportsAutoSettleOptOut(environmentId: EnvironmentId): boolean {
249 return (
250 appAtomRegistry.get(environmentServerConfigsAtom).get(environmentId)?.environment.capabilities
251 .threadAutoSettleOptOut === true
252 );
253}
254
255export function readEnvironmentSupportsActiveReorder(environmentId: EnvironmentId): boolean {
256 return (
257 appAtomRegistry.get(environmentServerConfigsAtom).get(environmentId)?.environment.capabilities
258 .threadActiveReorder === true
259 );
260}
261
262/** Whether the environment's server understands thread.settle/unsettle.
263 False for pre-settlement servers (capability defaults false on decode),
264 so clients under version skew fall back instead of erroring. */
265export function readEnvironmentSupportsSettlement(environmentId: EnvironmentId): boolean {
266 return (
267 appAtomRegistry.get(environmentServerConfigsAtom).get(environmentId)?.environment.capabilities
268 .threadSettlement === true
269 );
270}
271
272/** Whether the environment's server understands thread.snooze/unsnooze.
273 Same version-skew contract as settlement. */
274export function readEnvironmentSupportsSnooze(environmentId: EnvironmentId): boolean {
275 return (
276 appAtomRegistry.get(environmentServerConfigsAtom).get(environmentId)?.environment.capabilities
277 .threadSnooze === true
278 );
279}
280
281/** Whether the environment's server understands thread.visit/mark-unread and
282 projects lastVisitedAt on thread shells. Same version-skew contract as
283 settlement: against older servers, clients keep the browser-local visited
284 state instead. */
285export function readEnvironmentSupportsVisitedTracking(environmentId: EnvironmentId): boolean {
286 return (
287 appAtomRegistry.get(environmentServerConfigsAtom).get(environmentId)?.environment.capabilities
288 .threadVisitedTracking === true
289 );
290}
291
292export function readEnvironmentThreadRefs(
293 environmentId: EnvironmentId,
294): ReadonlyArray<ScopedThreadRef> {
295 return appAtomRegistry.get(environmentThreadShells.environmentThreadRefsAtom(environmentId));
296}
297
298export function readThreadShells(): ReadonlyArray<EnvironmentThreadShell> {
299 return appAtomRegistry.get(environmentThreadShells.threadShellsAtom);
300}