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

archivedThreadsState.ts

43 lines1.4 KB

1import { useAtomValue } from "@effect/atom-react";
2import {
3 type ArchivedSnapshotEntry,
4 createArchivedThreadsManager,
5 makeArchivedThreadsEnvironmentKey,
6 readArchivedThreadsSnapshotState,
7} from "@t3tools/client-runtime";
8import type { EnvironmentId } from "@t3tools/contracts";
9import { useCallback, useMemo } from "react";
10​
11import { readEnvironmentApi } from "../environmentApi";
12import { appAtomRegistry } from "../rpc/atomRegistry";
13​
14const archivedThreadsManager = createArchivedThreadsManager({
15 getRegistry: () => appAtomRegistry,
16 getClient: (environmentId) => readEnvironmentApi(environmentId)?.orchestration ?? null,
17});
18​
19export function refreshArchivedThreadsForEnvironment(environmentId: EnvironmentId): void {
20 archivedThreadsManager.refreshForEnvironment(environmentId);
22​
23export function useArchivedThreadSnapshots(environmentIds: ReadonlyArray<EnvironmentId>): {
24 readonly snapshots: ReadonlyArray<ArchivedSnapshotEntry>;
25 readonly error: string | null;
26 readonly isLoading: boolean;
27 readonly refresh: () => void;
28} {
29 const environmentKey = useMemo(
30 () => makeArchivedThreadsEnvironmentKey(environmentIds),
31 [environmentIds],
32 );
33 const atom = archivedThreadsManager.getAtom(environmentKey);
34 const result = useAtomValue(atom);
35 const refresh = useCallback(() => {
36 archivedThreadsManager.refresh(environmentIds);
37 }, [environmentIds]);
38​
39 return {
40 ...readArchivedThreadsSnapshotState(result),
41 refresh,
42 };