import type { SessionBackgroundCommand } from "@opengeni/sdk"; import { useCallback, useEffect, useRef } from "react"; import { useOpenGeni, type ClientOverride } from "../provider"; import { usePolledValue } from "./internal"; export type UseSessionBackgroundCommandsOptions = ClientOverride & { pollIntervalMs?: number | undefined; enabled?: boolean | undefined; }; export type UseSessionBackgroundCommandsResult = { commands: SessionBackgroundCommand[]; loading: boolean; error: Error | null; refresh: () => Promise; cancel: (commandId: string) => Promise; }; export function useSessionBackgroundCommands( sessionId: string | null | undefined, options: UseSessionBackgroundCommandsOptions = {}, ): UseSessionBackgroundCommandsResult { const { client, workspaceId } = useOpenGeni(options); const enabled = (options.enabled ?? true) && Boolean(sessionId); const load = useCallback( async (signal?: AbortSignal) => { if (!sessionId) return { commands: [] }; if (!client.listSessionBackgroundCommands) { throw new Error("The configured Opengeni client does not support background commands"); } return await client.listSessionBackgroundCommands(workspaceId, sessionId, { signal }); }, [client, workspaceId, sessionId], ); const state = usePolledValue(load, { enabled, pollIntervalMs: options.pollIntervalMs, }); const refresh = state.refresh; const generation = useRef(0); useEffect(() => { generation.current += 1; return () => { generation.current += 1; }; }, [client, workspaceId, sessionId, enabled]); const cancel = useCallback( async (commandId: string): Promise => { if (!sessionId) return; if (!client.cancelSessionBackgroundCommand) { throw new Error("The configured Opengeni client does not support background commands"); } const ticket = generation.current; await client.cancelSessionBackgroundCommand(workspaceId, sessionId, commandId); if (enabled && ticket === generation.current) await refresh(); }, [client, workspaceId, sessionId, refresh, enabled], ); return { commands: state.data?.commands ?? [], loading: state.loading, error: state.error, refresh, cancel, }; }