import useSWR, { type SWRConfiguration } from "swr"; import type { PaginatedBoardRunList, PaginatedEventList, PaginatedRunFileList, PaginatedRunList, PaginatedRunStageList, PaginatedStageTurnList, CommandLogResponse, CommandOutputStream, RunBilling, RunProjection, ServerSettings, RunSummary, SystemInfoResponse, WorkflowSettings, } from "@qltysh/fabro-api-client"; import type { PaginatedWorkflowListResponse, WorkflowDetailResponse } from "./workflow-api"; import { apiFetcher, apiNullableFetcher, apiNullableTextFetcher, apiPaginatedFetcher, apiTextFetcher, type PaginatedEnvelope, } from "./api-client"; import { queryKeys } from "./query-keys"; const immutableOptions: SWRConfiguration = { revalidateIfStale: false, revalidateOnFocus: false, revalidateOnReconnect: false, }; export function useAuthConfig() { return useSWR<{ methods: string[] }>(queryKeys.auth.config(), apiFetcher, immutableOptions); } export function useAuthMe() { return useSWR<{ user: { login: string; name: string; email: string; avatarUrl: string; userUrl: string; }; provider: string; demoMode: boolean; }>(queryKeys.auth.me(), apiFetcher, { dedupingInterval: 10_000 }); } export function useSystemInfo() { return useSWR( queryKeys.system.info(), apiFetcher, immutableOptions, ); } export function useBoardsRuns() { return useSWR< PaginatedEnvelope & { columns: { id: string; name: string }[]; } >(queryKeys.boards.runs(), apiPaginatedFetcher); } export function useRun(id: string | undefined) { return useSWR( id ? queryKeys.runs.detail(id) : null, apiNullableFetcher, ); } export function useRunState(id: string | undefined) { return useSWR( id ? queryKeys.runs.state(id) : null, apiNullableFetcher, ); } export function useRunFiles(id: string | undefined) { return useSWR( id ? queryKeys.runs.files(id) : null, apiNullableFetcher, { keepPreviousData: true }, ); } export function useRunStages(id: string | undefined) { return useSWR( id ? queryKeys.runs.stages(id) : null, apiNullableFetcher, ); } export function useRunGraph(id: string | undefined, direction?: "LR" | "TB") { return useSWR( id ? queryKeys.runs.graph(id, direction) : null, apiNullableTextFetcher, ); } export function useRunGraphSource(id: string | undefined, enabled: boolean) { return useSWR( id && enabled ? queryKeys.runs.graphSource(id) : null, apiNullableTextFetcher, ); } export function useRunLogs(id: string | undefined, refreshInterval?: number) { return useSWR( id ? queryKeys.runs.logs(id) : null, apiNullableTextFetcher, refreshInterval ? { refreshInterval } : undefined, ); } export function useRunSettings(id: string | undefined) { return useSWR( id ? queryKeys.runs.settings(id) : null, apiFetcher, immutableOptions, ); } export function useRunBilling(id: string | undefined) { return useSWR(id ? queryKeys.runs.billing(id) : null, apiFetcher); } export function useRunQuestionText(id: string | undefined, enabled: boolean) { return useSWR( id && enabled ? queryKeys.runs.questions(id, 1, 0) : null, async (key) => { const payload = await apiNullableFetcher<{ data: { text?: string | null }[] }>(key); return payload?.data[0]?.text ?? null; }, ); } export function useRunStageTurns( id: string | undefined, stageId: string | undefined, enabled = true, ) { return useSWR( id && stageId && enabled ? queryKeys.runs.stageTurns(id, stageId) : null, apiNullableFetcher, ); } export function useRunEventsList(id: string | undefined, enabled = true) { return useSWR( id && enabled ? queryKeys.runs.events(id, 1000) : null, apiNullableFetcher, ); } export function fetchRunCommandLog( id: string, stageId: string, stream: CommandOutputStream, offset: number, limit?: number, ) { return apiFetcher( queryKeys.runs.stageLog(id, stageId, stream, offset, limit), ); } export function useWorkflows() { return useSWR( queryKeys.workflows.list(), apiNullableFetcher, immutableOptions, ); } export function useWorkflow(name: string | undefined) { return useSWR( name ? queryKeys.workflows.detail(name) : null, apiNullableFetcher, immutableOptions, ); } export function useWorkflowRuns(name: string | undefined) { return useSWR( name ? queryKeys.workflows.runs(name) : null, apiNullableFetcher, ); } export function useInsightsQueries() { return useSWR(queryKeys.insights.queries(), apiFetcher, immutableOptions); } export function useInsightsHistory() { return useSWR(queryKeys.insights.history(), apiFetcher, immutableOptions); } export function useServerSettings() { return useSWR(queryKeys.settings.server(), apiFetcher, immutableOptions); } export { apiTextFetcher };