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 { serverEnvironment } from "../state/server";
11import { environmentThreadShells } from "../state/threads";
12import { appAtomRegistry } from "../rpc/atomRegistry";
13
14function archivedSnapshotAtom(environmentId: EnvironmentId) {
15 return orchestrationEnvironment.archivedShellSnapshot({
16 environmentId,
17 input: {},
18 });
19}
20
21const archivedSnapshotsAtom = createArchivedThreadSnapshotsAtomFamily({
22 getSnapshotAtom: archivedSnapshotAtom,
23 hiddenThreadTagsAtom: serverEnvironment.hiddenThreadTagsAtom,
24 hiddenThreadKeysAtom: environmentThreadShells.hiddenThreadKeysAtom,
25 liveThreadRefsAtom: environmentThreadShells.threadRefsAtom,
26 anyEnvironmentHidingAtom: environmentThreadShells.anyEnvironmentHidingAtom,
27 labelPrefix: "web:archived-thread-snapshots",
28});
29
30export function refreshArchivedThreadsForEnvironment(environmentId: EnvironmentId): void {
31 appAtomRegistry.refresh(archivedSnapshotAtom(environmentId));
32}
33
34export function useArchivedThreadSnapshots(environmentIds: ReadonlyArray<EnvironmentId>): {
35 readonly snapshots: ReadonlyArray<ArchivedSnapshotEntry>;
36 readonly error: string | null;
37 readonly isLoading: boolean;
38 readonly refresh: () => void;
39} {
40 const environmentKey = makeArchivedThreadsEnvironmentKey(environmentIds);
41 const result = useAtomValue(archivedSnapshotsAtom(environmentKey));
42 const refresh = () => {
43 for (const environmentId of environmentIds) {
44 appAtomRegistry.refresh(archivedSnapshotAtom(environmentId));
45 }
46 };
47
48 return {
49 ...result,
50 refresh,
51 };
52}