Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

entities.ts

302 lines10.7 KB

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 { useComposerDraftStore } from "../composerDraftStore";
16import { appAtomRegistry } from "../rpc/atomRegistry";
17import { environmentProjects } from "./projects";
18import { environmentServerConfigsAtom } from "./server";
19import {
20 allEnvironmentProjectSnapshotsReadyAtom,
21 allEnvironmentShellsBootstrappedAtom,
22} from "./shell";
23import { environmentThreadDetails, environmentThreadShells } from "./threads";
24​
25const EMPTY_THREAD_REFS: ReadonlyArray<ScopedThreadRef> = Object.freeze([]);
26​
27const EMPTY_PROJECT_ATOM = Atom.make<EnvironmentProject | null>(null).pipe(
28 Atom.withLabel("web-project:empty"),
29);
30const EMPTY_THREAD_REFS_ATOM = Atom.make(EMPTY_THREAD_REFS).pipe(
31 Atom.withLabel("web-thread-refs:empty"),
32);
33const EMPTY_THREAD_SHELL_ATOM = Atom.make<EnvironmentThreadShell | null>(null).pipe(
34 Atom.withLabel("web-thread-shell:empty"),
35);
36const EMPTY_THREAD_DETAIL_ATOM = Atom.make<EnvironmentThread | null>(null).pipe(
37 Atom.withLabel("web-thread-detail:empty"),
38);
39const EMPTY_THREAD_STATUS_ATOM = Atom.make<EnvironmentThreadStatus>("empty").pipe(
40 Atom.withLabel("web-thread-status:empty"),
41);
42​
43const activeEnvironmentIdAtom = Atom.make<EnvironmentId | null>(null).pipe(
44 Atom.keepAlive,
45 Atom.withLabel("web-active-environment-id"),
46);
47​
48export function useActiveEnvironmentId(): EnvironmentId | null {
49 return useAtomValue(activeEnvironmentIdAtom);
51​
52export function setActiveEnvironmentId(environmentId: EnvironmentId | null): void {
53 appAtomRegistry.set(activeEnvironmentIdAtom, environmentId);
55​
56export function useThreadRefs(): ReadonlyArray<ScopedThreadRef> {
57 return useAtomValue(environmentThreadShells.threadRefsAtom);
59​
60export function useEnvironmentThreadRefs(
61 environmentId: EnvironmentId | null,
62): ReadonlyArray<ScopedThreadRef> {
63 return useAtomValue(
64 environmentId === null
65 ? EMPTY_THREAD_REFS_ATOM
66 : environmentThreadShells.environmentThreadRefsAtom(environmentId),
67 );
69​
70export function useProjects(): ReadonlyArray<EnvironmentProject> {
71 return useAtomValue(environmentProjects.projectsAtom);
73​
74export function useServerConfigs(): ReadonlyMap<EnvironmentId, ServerConfig> {
75 return useAtomValue(environmentServerConfigsAtom);
77​
78export function useThreadShells(): ReadonlyArray<EnvironmentThreadShell> {
79 return useAtomValue(environmentThreadShells.threadShellsAtom);
81​
82export function useAllEnvironmentShellsBootstrapped(): boolean {
83 return useAtomValue(allEnvironmentShellsBootstrappedAtom);
85​
86export function useAllEnvironmentProjectSnapshotsReady(): boolean {
87 return useAtomValue(allEnvironmentProjectSnapshotsReadyAtom);
89​
90export function useThreadShellsForProjectRefs(
91 refs: ReadonlyArray<ScopedProjectRef>,
92): ReadonlyArray<EnvironmentThreadShell> {
93 return useAtomValue(environmentThreadShells.threadShellsForProjectRefsAtom(refs));
95​
96export function useProject(ref: ScopedProjectRef | null): EnvironmentProject | null {
97 return useAtomValue(ref === null ? EMPTY_PROJECT_ATOM : environmentProjects.projectAtom(ref));
99​
100export function useThreadShell(ref: ScopedThreadRef | null): EnvironmentThreadShell | null {
101 return useAtomValue(
102 ref === null ? EMPTY_THREAD_SHELL_ATOM : environmentThreadShells.threadShellAtom(ref),
103 );
105​
106export function useThreadDetail(ref: ScopedThreadRef | null): EnvironmentThread | null {
107 return useAtomValue(
108 ref === null ? EMPTY_THREAD_DETAIL_ATOM : environmentThreadDetails.detailAtom(ref),
109 );
111​
112export type ThreadDetailClassification =
113 | { readonly kind: "server-thread" }
114 | {
115 readonly kind: "local-draft";
116 readonly hasServerShell: boolean;
117 };
118​
119export function classifyThreadDetail(input: {
120 readonly hasLocalDraft: boolean;
121 readonly hasServerShell: boolean;
122 readonly waitForShell?: boolean;
123}): ThreadDetailClassification {
124 return input.hasLocalDraft || input.waitForShell === true
125 ? { kind: "local-draft", hasServerShell: input.hasServerShell }
126 : { kind: "server-thread" };
128​
129/**
130 * Returns the detail ref for server threads and materialized drafts. A local
131 * draft stays unsubscribed until its server shell appears after the first send.
132 */
133export function resolveThreadDetailRef(
134 ref: ScopedThreadRef | null,
135 classification: ThreadDetailClassification,
136): ScopedThreadRef | null {
137 return ref !== null && (classification.kind === "server-thread" || classification.hasServerShell)
138 ? ref
139 : null;
141​
142export function useThreadDetailWhenReady(
143 ref: ScopedThreadRef | null,
144 classification: ThreadDetailClassification,
145): EnvironmentThread | null {
146 return useThreadDetail(resolveThreadDetailRef(ref, classification));
148​
149export function useThreadStatus(ref: ScopedThreadRef | null): EnvironmentThreadStatus {
150 return useAtomValue(
151 ref === null ? EMPTY_THREAD_STATUS_ATOM : environmentThreadDetails.statusAtom(ref),
152 );
154​
155export function useThreadStatusWhenReady(
156 ref: ScopedThreadRef | null,
157 classification: ThreadDetailClassification,
158): EnvironmentThreadStatus {
159 return useThreadStatus(resolveThreadDetailRef(ref, classification));
161​
162/** Detail collections composed with shell-authoritative thread/workspace metadata. */
163export function useThread(
164 ref: ScopedThreadRef | null,
165 options?: {
166 /**
167 * Client-reserved draft thread ids do not exist on the server until the
168 * first send. Waiting for the shell index avoids polling the detail
169 * endpoint for an intentionally missing thread during that window.
170 */
171 waitForShell?: boolean;
172 },
173): EnvironmentThread | null {
174 const shell = useThreadShell(ref);
175 const hasLocalDraft = useComposerDraftStore((store) =>
176 ref === null ? false : store.getDraftThreadByRef(ref) !== null,
177 );
178 const classification = classifyThreadDetail({
179 hasLocalDraft,
180 hasServerShell: shell !== null,
181 waitForShell: options?.waitForShell === true,
182 });
183 const detail = useThreadDetailWhenReady(ref, classification);
184 return useMemo(() => mergeEnvironmentThread(detail, shell), [detail, shell]);
186​
187export function readProject(ref: ScopedProjectRef): EnvironmentProject | null {
188 return appAtomRegistry.get(environmentProjects.projectAtom(ref));
190​
191export function readProjects(): ReadonlyArray<EnvironmentProject> {
192 return appAtomRegistry.get(environmentProjects.projectsAtom);
194​
195/** Resolves when the project event reaches the live client store. */
196export function waitForProject(
197 ref: ScopedProjectRef,
198 timeoutMs = 10_000,
199): Promise<EnvironmentProject> {
200 const current = readProject(ref);
201 if (current !== null) return Promise.resolve(current);
202​
203 return new Promise((resolve, reject) => {
204 let unsubscribe: (() => void) | null = null;
205 const timeout = setTimeout(() => {
206 unsubscribe?.();
207 reject(new Error("The project did not appear in the desktop app."));
208 }, timeoutMs);
209 const finish = (project: EnvironmentProject | null) => {
210 if (project === null) return;
211 clearTimeout(timeout);
212 unsubscribe?.();
213 resolve(project);
214 };
215 unsubscribe = appAtomRegistry.subscribe(environmentProjects.projectAtom(ref), finish);
216 finish(readProject(ref));
217 });
219​
220export function readThreadShell(ref: ScopedThreadRef): EnvironmentThreadShell | null {
221 return appAtomRegistry.get(environmentThreadShells.threadShellAtom(ref));
223​
224/** The thread as `useThread` returns it, read outside React. */
225export function readThread(ref: ScopedThreadRef): EnvironmentThread | null {
226 return mergeEnvironmentThread(
227 appAtomRegistry.get(environmentThreadDetails.detailAtom(ref)),
228 readThreadShell(ref),
229 );
231​
232/** Whether the environment's server understands thread.settle/unsettle.
233 False for pre-settlement servers (capability defaults false on decode),
234 so clients under version skew fall back instead of erroring. */
235export function readEnvironmentSupportsSettlement(environmentId: EnvironmentId): boolean {
236 return (
237 appAtomRegistry.get(environmentServerConfigsAtom).get(environmentId)?.environment.capabilities
238 .threadSettlement === true
239 );
241​
242/** Whether the environment's server understands thread.snooze/unsnooze.
243 Same version-skew contract as settlement. */
244export function readEnvironmentSupportsSnooze(environmentId: EnvironmentId): boolean {
245 return (
246 appAtomRegistry.get(environmentServerConfigsAtom).get(environmentId)?.environment.capabilities
247 .threadSnooze === true
248 );
250​
251/** Whether the environment's server understands thread.pin/unpin.
252 Same version-skew contract as settlement. */
253export function readEnvironmentSupportsPinning(environmentId: EnvironmentId): boolean {
254 return (
255 appAtomRegistry.get(environmentServerConfigsAtom).get(environmentId)?.environment.capabilities
256 .threadPinning === true
257 );
259​
260/** Whether the environment's server understands thread title regeneration.
261 Same version-skew contract as settlement. */
262export function readEnvironmentSupportsTitleRegeneration(environmentId: EnvironmentId): boolean {
263 return (
264 appAtomRegistry.get(environmentServerConfigsAtom).get(environmentId)?.environment.capabilities
265 .threadTitleRegeneration === true
266 );
268​
269/** Whether the environment's server understands thread.pin.reorder (and
270 orderKey on thread.pin). Same version-skew contract as settlement. */
271export function readEnvironmentSupportsPinReorder(environmentId: EnvironmentId): boolean {
272 return (
273 appAtomRegistry.get(environmentServerConfigsAtom).get(environmentId)?.environment.capabilities
274 .threadPinReorder === true
275 );
277​
278/** Whether the environment's server understands thread.auto-settle.set.
279 Same version-skew contract as settlement. */
280export function readEnvironmentSupportsAutoSettleOptOut(environmentId: EnvironmentId): boolean {
281 return (
282 appAtomRegistry.get(environmentServerConfigsAtom).get(environmentId)?.environment.capabilities
283 .threadAutoSettleOptOut === true
284 );
286​
287export function readEnvironmentSupportsActiveReorder(environmentId: EnvironmentId): boolean {
288 return (
289 appAtomRegistry.get(environmentServerConfigsAtom).get(environmentId)?.environment.capabilities
290 .threadActiveReorder === true
291 );
293​
294export function readEnvironmentThreadRefs(
295 environmentId: EnvironmentId,
296): ReadonlyArray<ScopedThreadRef> {
297 return appAtomRegistry.get(environmentThreadShells.environmentThreadRefsAtom(environmentId));
299​
300export function readThreadShells(): ReadonlyArray<EnvironmentThreadShell> {
301 return appAtomRegistry.get(environmentThreadShells.threadShellsAtom);