supermemory/apps/web/hooks/use-trigger-sync.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

49 lines
1.4 KiB
TypeScript

"use client"
import { $fetch } from "@lib/api"
import { useMutation, useQueryClient } from "@tanstack/react-query"
import { toast } from "sonner"
import type { ImportProvider } from "@/components/settings/sync-utils"
export function useTriggerSync() {
const queryClient = useQueryClient()
return useMutation({
mutationFn: async ({
provider,
containerTags,
}: {
// connectionId is not sent to the backend — the import endpoint is keyed
// by provider, so it re-syncs all connections for that provider.
// It's kept here so onSuccess can target cache invalidation.
connectionId: string
provider: ImportProvider
containerTags?: string[]
}) => {
const response = await $fetch("@post/connections/:provider/import", {
params: { provider },
body: { containerTags },
})
if (response.error) {
throw new Error(
(response.error as { message?: string })?.message ||
"Failed to trigger sync",
)
}
return response.data
},
onSuccess: (_data, variables) => {
toast.success("Sync started")
queryClient.invalidateQueries({ queryKey: ["connections"] })
queryClient.invalidateQueries({
queryKey: ["sync-runs", variables.connectionId],
})
queryClient.invalidateQueries({ queryKey: ["processing-documents"] })
},
onError: (error) => {
toast.error("Failed to start sync", {
description: error instanceof Error ? error.message : "Unknown error",
})
},
})
}