import React, { useEffect, useState } from "react"; import { Alert, CircularProgress, CloseIcon, IconButton } from "@firecms/ui"; import { useCollectionsConfigController } from "@firecms/collection_editor"; const ERROR_EXPIRY_MS = 30 * 60 * 1000; // 30 minutes export function CollectionsSetupLoadingLabel({ }: { }) { const configController = useCollectionsConfigController(); const collectionsSetup = configController.collectionsSetup; const setupLoading = collectionsSetup?.status === "ongoing"; const setupError = collectionsSetup?.status === "error"; const [dismissed, setDismissed] = useState(false); // Reset dismissed state when setup status changes (e.g., a new setup is triggered) useEffect(() => { if (collectionsSetup?.status === "ongoing") { setDismissed(false); } }, [collectionsSetup?.status]); // Check if the error is older than 30 minutes, with a timer to auto-expire const [isExpired, setIsExpired] = useState(() => { if (!collectionsSetup?.updated_at || collectionsSetup.status !== "error") return false; const updatedAt = collectionsSetup.updated_at instanceof Date ? collectionsSetup.updated_at : collectionsSetup.updated_at.toDate(); return Date.now() - updatedAt.getTime() > ERROR_EXPIRY_MS; }); useEffect(() => { if (!setupError || !collectionsSetup?.updated_at) { setIsExpired(false); return; } const updatedAt = collectionsSetup.updated_at instanceof Date ? collectionsSetup.updated_at : collectionsSetup.updated_at.toDate(); const elapsed = Date.now() - updatedAt.getTime(); if (elapsed > ERROR_EXPIRY_MS) { setIsExpired(true); return; } const remaining = ERROR_EXPIRY_MS - elapsed; const timer = setTimeout(() => setIsExpired(true), remaining); return () => clearTimeout(timer); }, [setupError, collectionsSetup?.updated_at]); // Auto-clear expired errors from Firestore useEffect(() => { if (isExpired && configController.clearCollectionsSetup) { configController.clearCollectionsSetup(); } }, [isExpired]); if (setupError && !dismissed && !isExpired) { return Error setting up collections {collectionsSetup?.error && ( {collectionsSetup.error} )} { setDismissed(true); configController.clearCollectionsSetup?.(); }}> } if (!setupLoading) { return null; } return Setting up your collections... }