Files
t3code/apps/web/src/lib/archivedThreadsState.ts
T
2026-06-18 20:24:48 -07:00

51 lines
1.6 KiB
TypeScript

import { useAtomValue } from "@effect/atom-react";
import {
type ArchivedSnapshotEntry,
createArchivedThreadSnapshotsAtomFamily,
makeArchivedThreadsEnvironmentKey,
} from "@t3tools/client-runtime/state/threads";
import type { EnvironmentId } from "@t3tools/contracts";
import { useCallback, useMemo } from "react";
import { orchestrationEnvironment } from "../state/orchestration";
import { appAtomRegistry } from "../rpc/atomRegistry";
function archivedSnapshotAtom(environmentId: EnvironmentId) {
return orchestrationEnvironment.archivedShellSnapshot({
environmentId,
input: {},
});
}
const archivedSnapshotsAtom = createArchivedThreadSnapshotsAtomFamily({
getSnapshotAtom: archivedSnapshotAtom,
labelPrefix: "web:archived-thread-snapshots",
});
export function refreshArchivedThreadsForEnvironment(environmentId: EnvironmentId): void {
appAtomRegistry.refresh(archivedSnapshotAtom(environmentId));
}
export function useArchivedThreadSnapshots(environmentIds: ReadonlyArray<EnvironmentId>): {
readonly snapshots: ReadonlyArray<ArchivedSnapshotEntry>;
readonly error: string | null;
readonly isLoading: boolean;
readonly refresh: () => void;
} {
const environmentKey = useMemo(
() => makeArchivedThreadsEnvironmentKey(environmentIds),
[environmentIds],
);
const result = useAtomValue(archivedSnapshotsAtom(environmentKey));
const refresh = useCallback(() => {
for (const environmentId of environmentIds) {
appAtomRegistry.refresh(archivedSnapshotAtom(environmentId));
}
}, [environmentIds]);
return {
...result,
refresh,
};
}