Open History to see the last commit
1import { createChildThreadApprovalsAtom } from "@t3tools/client-runtime/state/thread-requests";
2import { useAtomValue } from "@effect/atom-react";
3import {
4 createEnvironmentThreadDetailAtoms,
5 createEnvironmentThreadShellAtoms,
6 createEnvironmentThreadStateAtoms,
7 EMPTY_ENVIRONMENT_THREAD_STATE,
8 type EnvironmentThreadState,
9 createOlderSettledThreadAtoms,
10 createThreadEnvironmentAtoms,
11 createThreadFamilyAtoms,
12} from "@t3tools/client-runtime/state/threads";
13import { createSteadyShellAtoms } from "@t3tools/client-runtime/state/shell";
14import type { EnvironmentId, ThreadId } from "@t3tools/contracts";
15import { AsyncResult, Atom } from "effect/reactivity";
16
17import { environmentCatalog } from "../connection/catalog";
18import { connectionAtomRuntime } from "../connection/runtime";
19import { scopedThreadKey } from "../lib/scopedEntities";
20import { appAtomRegistry } from "./atom-registry";
21import {
22 pendingThreadCreationOutcomesAtom,
23 selectThreadCreationDetail,
24} from "./pending-thread-creation";
25import { serverEnvironment } from "./server";
26import { environmentShell } from "./shell";
27import { environmentPresentations } from "./presentation";
28import { threadOutboxManager } from "./thread-outbox";
29
30export const threadFamilies = createThreadFamilyAtoms(
31 connectionAtomRuntime,
32 environmentShell.stateAtom,
33);
34export const olderSettledThreads = createOlderSettledThreadAtoms(
35 connectionAtomRuntime,
36 environmentShell.stateAtom,
37);
38export const threadEnvironment = createThreadEnvironmentAtoms(
39 connectionAtomRuntime,
40 threadFamilies.snapshotAtom,
41 environmentShell.statusAtom,
42 environmentShell.commandHistoryAtom,
43);
44export const environmentThreads = createEnvironmentThreadStateAtoms(connectionAtomRuntime);
45export const environmentThreadDetails = createEnvironmentThreadDetailAtoms(
46 environmentThreads.stateAtom,
47);
48export const environmentThreadShells = createEnvironmentThreadShellAtoms({
49 catalogValueAtom: environmentCatalog.catalogValueAtom,
50 connectionPhaseAtom: environmentPresentations.connectionPhaseAtom,
51 snapshotAtom: threadEnvironment.snapshotAtom,
52 hiddenThreadTagsAtom: serverEnvironment.hiddenThreadTagsAtom,
53});
54const steadyShells = createSteadyShellAtoms({
55 catalogValueAtom: environmentCatalog.catalogValueAtom,
56 snapshotAtom: threadEnvironment.snapshotAtom,
57 shellStateAtom: environmentShell.stateValueAtom,
58 rowActionsAtom: threadEnvironment.rowActionsAtom,
59 connectionStateAtom: environmentCatalog.stateAtom,
60});
61/**
62 * The threads that lists arrange. Rows wait while their environment synchronizes, so a list
63 * changes once when environments connect, not once per environment.
64 */
65export const steadyShellsHoldingAtom = steadyShells.holdingAtom;
66export const steadyNavigationThreadShellsAtom = createEnvironmentThreadShellAtoms({
67 catalogValueAtom: environmentCatalog.catalogValueAtom,
68 connectionPhaseAtom: environmentPresentations.connectionPhaseAtom,
69 snapshotAtom: steadyShells.snapshotAtom,
70 hiddenThreadTagsAtom: serverEnvironment.hiddenThreadTagsAtom,
71}).navigationThreadShellsAtom;
72export const childThreadApprovalsAtom = createChildThreadApprovalsAtom({
73 childThreadInputsAtom: environmentThreadShells.childThreadInputsAtom,
74 pendingRequestsAtom: environmentThreadDetails.pendingRequestsAtom,
75});
76
77// Long enough for the opened screen to subscribe; its subscription keeps the thread after this.
78const OPENING_THREAD_HOLD_MS = 3_000;
79
80/**
81 * Starts loading a thread being opened before its screen renders and subscribes, which
82 * otherwise waits for navigation to commit.
83 */
84export function startOpeningEnvironmentThread(environmentId: EnvironmentId, threadId: ThreadId) {
85 // A thread whose creation is still queued would load as not found and stay deleted;
86 // the opened screen loads it once creation is delivered.
87 const { detailRef } = selectThreadCreationDetail({
88 threadRef: { environmentId, threadId },
89 threadKey: scopedThreadKey(environmentId, threadId),
90 queuedMessagesByThreadKey: appAtomRegistry.get(
91 threadOutboxManager.queuedMessagesByThreadKeyAtom,
92 ),
93 outcomes: appAtomRegistry.get(pendingThreadCreationOutcomesAtom),
94 });
95 if (detailRef === null) return;
96 const release = appAtomRegistry.mount(environmentThreads.stateAtom(environmentId, threadId));
97 setTimeout(release, OPENING_THREAD_HOLD_MS);
98}
99
100const EMPTY_THREAD_STATE_ATOM = Atom.make(AsyncResult.success(EMPTY_ENVIRONMENT_THREAD_STATE)).pipe(
101 Atom.withLabel("mobile-environment-thread:empty"),
102);
103
104export function useEnvironmentThread(
105 environmentId: EnvironmentId | null,
106 threadId: ThreadId | null,
107): EnvironmentThreadState {
108 const result = useAtomValue(
109 environmentId !== null && threadId !== null
110 ? environmentThreads.stateAtom(environmentId, threadId)
111 : EMPTY_THREAD_STATE_ATOM,
112 );
113 const state = AsyncResult.getOrElse(
114 result,
115 () => EMPTY_ENVIRONMENT_THREAD_STATE,
116 ) as EnvironmentThreadState;
117 return state;
118}