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

archivedThreadsState.ts

46 lines1.5 KB

1import { useAtomValue } from "@effect/atom-react";
2import {
3 type ArchivedSnapshotEntry,
4 createArchivedThreadSnapshotsAtomFamily,
5 makeArchivedThreadsEnvironmentKey,
6} from "@t3tools/client-runtime/state/threads";
7import type { EnvironmentId } from "@t3tools/contracts";
8​
9import { orchestrationEnvironment } from "../state/orchestration";
10import { appAtomRegistry } from "../rpc/atomRegistry";
11​
12function archivedSnapshotAtom(environmentId: EnvironmentId) {
13 return orchestrationEnvironment.archivedShellSnapshot({
14 environmentId,
15 input: {},
16 });
18​
19const archivedSnapshotsAtom = createArchivedThreadSnapshotsAtomFamily({
20 getSnapshotAtom: archivedSnapshotAtom,
21 labelPrefix: "web:archived-thread-snapshots",
22});
23​
24export function refreshArchivedThreadsForEnvironment(environmentId: EnvironmentId): void {
25 appAtomRegistry.refresh(archivedSnapshotAtom(environmentId));
27​
28export function useArchivedThreadSnapshots(environmentIds: ReadonlyArray<EnvironmentId>): {
29 readonly snapshots: ReadonlyArray<ArchivedSnapshotEntry>;
30 readonly error: string | null;
31 readonly isLoading: boolean;
32 readonly refresh: () => void;
33} {
34 const environmentKey = makeArchivedThreadsEnvironmentKey(environmentIds);
35 const result = useAtomValue(archivedSnapshotsAtom(environmentKey));
36 const refresh = () => {
37 for (const environmentId of environmentIds) {
38 appAtomRegistry.refresh(archivedSnapshotAtom(environmentId));
39 }
40 };
41​
42 return {
43 ...result,
44 refresh,
45 };