'use client' import { useEffect, useState } from 'react' import { useTranslations } from 'next-intl' import { FullArchiveDialog } from '@/components/import/FullArchiveDialog' import { SettingsGroup, SettingsRow, SettingsRowEnd, SettingsRowNote, } from '@/components/settings/SettingsRows' import type { ApiResponse, ArchiveEstimate } from '@/types' const ESTIMATE_RETRY_DELAY_MS = 1_000 const ESTIMATE_MAX_ATTEMPTS = 3 export function CompanyMigrationArchiveRow({ companyId }: { companyId: string }) { const t = useTranslations('settings_company') const [loadedEstimate, setLoadedEstimate] = useState<{ companyId: string value: ArchiveEstimate } | null>(null) const [open, setOpen] = useState(false) const estimate = loadedEstimate?.companyId === companyId ? loadedEstimate.value : null useEffect(() => { const controller = new AbortController() let cancelled = false let retryTimer: ReturnType | undefined const scheduleRetry = (attempt: number) => { if (cancelled || attempt >= ESTIMATE_MAX_ATTEMPTS) return retryTimer = setTimeout(() => { void loadEstimate(attempt + 1) }, ESTIMATE_RETRY_DELAY_MS) } const loadEstimate = async (attempt: number) => { try { const response = await fetch( `/api/company/${companyId}/migration-reset/archive?estimate=1`, { signal: controller.signal }, ) if (!response.ok) { if (response.status >= 500 || response.status === 429) scheduleRetry(attempt) return } const body = await response.json() as ApiResponse if (!cancelled && body.data) { setLoadedEstimate({ companyId, value: body.data }) } } catch (error) { if (error instanceof DOMException && error.name === 'AbortError') return scheduleRetry(attempt) } } void loadEstimate(1) return () => { cancelled = true if (retryTimer) clearTimeout(retryTimer) controller.abort() } }, [companyId]) if (!estimate) return null return ( <> {t('reset_archive_row_note', { count: estimate.document_count })} ) }