import React from "react"; import { useNavigationController, useSnackbarController, useTranslation } from "@firecms/core"; import { Button, Checkbox, CircularProgress, Dialog, DialogActions, DialogContent, DialogTitle, LoadingButton, Typography } from "@firecms/ui"; import { ProjectsApi, RootCollectionInfo } from "../api/projects"; export function CollectionSetupSelectionDialog({ open, onClose, suggestions, projectsApi, projectId, onSuccess, onError }: { open: boolean; onClose: () => void; suggestions: RootCollectionInfo[]; projectsApi: ProjectsApi; projectId: string; onSuccess?: () => void; onError?: () => void; }) { const { t } = useTranslation(); const snackbarController = useSnackbarController(); const navigationController = useNavigationController(); const [selectedPaths, setSelectedPaths] = React.useState>(new Set()); const [loading, setLoading] = React.useState(false); // Reset selection when dialog opens React.useEffect(() => { if (open) { setSelectedPaths(new Set(suggestions.map(s => s.path))); } }, [open, suggestions]); const togglePath = (path: string) => { setSelectedPaths(prev => { const next = new Set(prev); if (next.has(path)) { next.delete(path); } else { next.add(path); } return next; }); }; const selectAll = () => { setSelectedPaths(new Set(suggestions.map(s => s.path))); }; const deselectAll = () => { setSelectedPaths(new Set()); }; const selectedCount = selectedPaths.size; const allSelected = selectedCount === suggestions.length; const noneSelected = selectedCount === 0; const handleSetup = async () => { const pathsToSetup = suggestions.filter(s => selectedPaths.has(s.path)); if (pathsToSetup.length === 0) return; setLoading(true); snackbarController.open({ message: t("setting_up_collections"), type: "info" }); try { const collections = await projectsApi.setupCollections(projectId, pathsToSetup); if (!collections || collections.length === 0) { snackbarController.open({ message: t("no_collections_found_to_setup"), type: "info" }); } else { snackbarController.open({ message: <>{t("collections_have_been_setup")}
{collections.map(c => c.name).join(", ")}, type: "success" }); navigationController.refreshNavigation(); } onSuccess?.(); onClose(); } catch (error) { console.error("Error setting up collections", error); snackbarController.open({ message: t("error_setting_up_collections"), type: "error" }); onError?.(); } finally { setLoading(false); } }; return ( !open && !loading ? onClose() : undefined} maxWidth={"lg"} > {t("setup_collections_title")} {t("setup_collections_select_desc")}
{suggestions.map((info) => ( ))}
{t("setup_collections_confirm", { count: String(selectedCount) })}
); }