Files
t3code/apps/web/src/state/threads.ts
T

46 lines
1.7 KiB
TypeScript

import { useAtomValue } from "@effect/atom-react";
import {
createEnvironmentThreadDetailAtoms,
createEnvironmentThreadShellAtoms,
createEnvironmentThreadStateAtoms,
EMPTY_ENVIRONMENT_THREAD_STATE,
type EnvironmentThreadState,
createThreadEnvironmentAtoms,
} from "@t3tools/client-runtime/state/threads";
import type { EnvironmentId, ThreadId } from "@t3tools/contracts";
import * as Option from "effect/Option";
import { AsyncResult, Atom } from "effect/unstable/reactivity";
import { environmentCatalog } from "../connection/catalog";
import { connectionAtomRuntime } from "../connection/runtime";
import { environmentSnapshotAtom } from "./shell";
export const threadEnvironment = createThreadEnvironmentAtoms(connectionAtomRuntime);
const environmentThreads = createEnvironmentThreadStateAtoms(connectionAtomRuntime);
export const environmentThreadDetails = createEnvironmentThreadDetailAtoms(
environmentThreads.stateAtom,
);
export const environmentThreadShells = createEnvironmentThreadShellAtoms({
catalogValueAtom: environmentCatalog.catalogValueAtom,
snapshotAtom: environmentSnapshotAtom,
});
const EMPTY_THREAD_STATE_ATOM = Atom.make(AsyncResult.success(EMPTY_ENVIRONMENT_THREAD_STATE)).pipe(
Atom.withLabel("web-environment-thread:empty"),
);
export function useEnvironmentThread(
environmentId: EnvironmentId | null,
threadId: ThreadId | null,
): EnvironmentThreadState {
const result = useAtomValue(
environmentId !== null && threadId !== null
? environmentThreads.stateAtom(environmentId, threadId)
: EMPTY_THREAD_STATE_ATOM,
);
return Option.getOrElse(
AsyncResult.value(result),
() => EMPTY_ENVIRONMENT_THREAD_STATE,
) as EnvironmentThreadState;
}