supermemory/apps/web/hooks/use-sync-runs.ts
vorflux[bot] 68a7781de7
feat: add connector sync visibility to NOVA settings page (#930)
Co-authored-by: MaheshtheDev <38828053+MaheshtheDev@users.noreply.github.com>
Co-authored-by: Mahesh Sanikommu <maheshthedev@gmail.com>
2026-05-12 11:59:56 +05:30

46 lines
1.2 KiB
TypeScript

"use client"
import { $fetch } from "@lib/api"
import { useQuery } from "@tanstack/react-query"
/**
* Mirrors the Zod schema at `apiSchema["@get/connections/:connectionId/sync-runs"].output`.
* Keep in sync with `packages/lib/api.ts` if fields are added/removed.
*/
export type SyncRun = {
id: string
connectionId: string
status: "running" | "completed" | "failed"
triggerType: "event" | "cron" | "manual"
startedAt: string
completedAt: string | null
itemsProcessed: number
itemsFailed: number
error: string | null
}
export function useSyncRuns(connectionId: string) {
return useQuery<SyncRun[]>({
queryKey: ["sync-runs", connectionId],
queryFn: async () => {
const response = await $fetch(
"@get/connections/:connectionId/sync-runs",
{ params: { connectionId } },
)
if (response.error) {
throw new Error("Failed to fetch sync runs")
}
return response.data as SyncRun[]
},
enabled: !!connectionId,
staleTime: 30 * 1000,
refetchOnMount: "always",
refetchInterval: (query) => {
const runs = query.state.data as SyncRun[] | undefined
if (runs?.some((r) => r.status === "running")) {
return 5000
}
return false
},
})
}