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