Open History to see the last commit
1import { useAtomValue } from "@effect/atom-react";
2import { deriveReportedModelSelection } from "@t3tools/client-runtime/state/thread-execution";
3
4import { appAtomRegistry } from "./atom-registry";
5import type {
6 EnvironmentProject,
7 EnvironmentThread,
8 EnvironmentThreadShell,
9} from "@t3tools/client-runtime/state/shell";
10import type {
11 EnvironmentId,
12 ScopedProjectRef,
13 ScopedThreadRef,
14 ServerConfig,
15} from "@t3tools/contracts";
16import { Atom } from "effect/reactivity";
17
18import { environmentProjects } from "./projects";
19import { environmentServerConfigsAtom, serverEnvironment } from "./server";
20import { environmentThreadDetails, environmentThreadShells } from "./threads";
21
22const EMPTY_PROJECT_ATOM = Atom.make<EnvironmentProject | null>(null).pipe(
23 Atom.withLabel("mobile-project:empty"),
24);
25const EMPTY_THREAD_SHELL_ATOM = Atom.make<EnvironmentThreadShell | null>(null).pipe(
26 Atom.withLabel("mobile-thread-shell:empty"),
27);
28const EMPTY_SERVER_CONFIG_ATOM = Atom.make<ServerConfig | null>(null).pipe(
29 Atom.withLabel("mobile-server-config:empty"),
30);
31
32/** Resolves when the project event reaches the live client store. */
33export function waitForProject(
34 ref: ScopedProjectRef,
35 timeoutMs = 10_000,
36): Promise<EnvironmentProject | null> {
37 const atom = environmentProjects.projectAtom(ref);
38 const current = appAtomRegistry.get(atom);
39 if (current !== null) return Promise.resolve(current);
40 return new Promise((resolve) => {
41 let unsubscribe: (() => void) | null = null;
42 const timeout = setTimeout(() => {
43 unsubscribe?.();
44 resolve(null);
45 }, timeoutMs);
46 const finish = (project: EnvironmentProject | null) => {
47 if (project === null) return;
48 clearTimeout(timeout);
49 unsubscribe?.();
50 resolve(project);
51 };
52 unsubscribe = appAtomRegistry.subscribe(atom, finish);
53 finish(appAtomRegistry.get(atom));
54 });
55}
56
57export function useProjects(): ReadonlyArray<EnvironmentProject> {
58 return useAtomValue(environmentProjects.projectsAtom);
59}
60
61export function useThreadShells(): ReadonlyArray<EnvironmentThreadShell> {
62 return useAtomValue(environmentThreadShells.threadShellsAtom);
63}
64
65export function useNavigationThreadShells(): ReadonlyArray<EnvironmentThreadShell> {
66 return useAtomValue(environmentThreadShells.navigationThreadShellsAtom);
67}
68
69export function useProject(ref: ScopedProjectRef | null): EnvironmentProject | null {
70 return useAtomValue(ref === null ? EMPTY_PROJECT_ATOM : environmentProjects.projectAtom(ref));
71}
72
73export function useThreadShell(ref: ScopedThreadRef | null): EnvironmentThreadShell | null {
74 return useAtomValue(
75 ref === null ? EMPTY_THREAD_SHELL_ATOM : environmentThreadShells.threadShellAtom(ref),
76 );
77}
78
79export function useEnvironmentServerConfig(
80 environmentId: EnvironmentId | null,
81): ServerConfig | null {
82 return useAtomValue(
83 environmentId === null
84 ? EMPTY_SERVER_CONFIG_ATOM
85 : serverEnvironment.configValueAtom(environmentId),
86 );
87}
88
89export function useServerConfigs(): ReadonlyMap<EnvironmentId, ServerConfig> {
90 return useAtomValue(environmentServerConfigsAtom);
91}
92
93const selectReportedModelSelection = (thread: EnvironmentThread | null) =>
94 thread === null ? null : deriveReportedModelSelection(thread.projection);
95
96export function useThreadReportedModelSelection(ref: ScopedThreadRef) {
97 return useAtomValue(environmentThreadDetails.threadAtom(ref), selectReportedModelSelection);
98}