supermemory/apps/web/hooks/use-sync-runs.ts
MaheshtheDev 1c1901f711 refactor: address review feedback for sync visibility
- Restore 60s baseline polling (was regressed to false)
- Extract getConnectionMeta helper to eliminate 5 inline casts
- Remove unnecessary containerTags intersection casts
- Deduplicate provider title maps into shared PROVIDER_DISPLAY_NAMES
- Fix formatRelativeTime: handle future dates, add minutes tier
- Preserve auth/expired state in SyncStatusBadge (red Disconnected)
- Disable sync button for expired connections
- Fix mobile width overflow on sync history sheet
- Add smart polling to useSyncRuns when a run is running
- Add clarifying comment on connectionId in useTriggerSync
- Replace z.any() with z.unknown() for import endpoint output
- Remove unnecessary 'use client' from sync-status-badge
2026-05-11 20:33:03 +00:00

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
},
})
}