import React, { useEffect, useState } from "react"; import { message, Modal } from "antd"; import { ChevronRight } from "lucide-react"; import { useGalleryStore } from "./store"; import { GallerySidebar } from "./sidebar"; import { GalleryDetail } from "./detail"; import { GalleryCreateModal } from "./create-modal"; import type { Gallery } from "./types"; export const GalleryManager: React.FC = () => { const [isLoading, setIsLoading] = useState(false); const [hasUnsavedChanges, setHasUnsavedChanges] = useState(false); const [isCreateModalOpen, setIsCreateModalOpen] = useState(false); const [isSidebarOpen, setIsSidebarOpen] = useState(() => { if (typeof window !== "undefined") { const stored = localStorage.getItem("gallerySidebar"); return stored !== null ? JSON.parse(stored) : true; } return true; }); const { galleries, selectedGalleryId, selectGallery, addGallery, updateGallery, removeGallery, setDefaultGallery, getSelectedGallery, getDefaultGallery, } = useGalleryStore(); const [messageApi, contextHolder] = message.useMessage(); const currentGallery = getSelectedGallery(); // Persist sidebar state useEffect(() => { if (typeof window !== "undefined") { localStorage.setItem("gallerySidebar", JSON.stringify(isSidebarOpen)); } }, [isSidebarOpen]); // Handle URL params useEffect(() => { const params = new URLSearchParams(window.location.search); const galleryId = params.get("galleryId"); if (galleryId && !selectedGalleryId) { handleSelectGallery(galleryId); } }, []); // Update URL when gallery changes useEffect(() => { if (selectedGalleryId) { window.history.pushState({}, "", `?galleryId=${selectedGalleryId}`); } }, [selectedGalleryId]); const handleSelectGallery = async (galleryId: string) => { if (hasUnsavedChanges) { Modal.confirm({ title: "Unsaved Changes", content: "You have unsaved changes. Do you want to discard them?", okText: "Discard", cancelText: "Go Back", onOk: () => { selectGallery(galleryId); setHasUnsavedChanges(false); }, }); } else { selectGallery(galleryId); } }; const handleCreateGallery = async (galleryData: Gallery) => { const newGallery: Gallery = { id: `gallery_${Date.now()}`, name: galleryData.name || "New Gallery", url: galleryData.url, metadata: { ...galleryData.metadata, created_at: new Date().toISOString(), updated_at: new Date().toISOString(), }, items: galleryData.items || { teams: [], components: { agents: [], models: [], tools: [], terminations: [], }, }, }; try { setIsLoading(true); await addGallery(newGallery); messageApi.success("Gallery created successfully"); selectGallery(newGallery.id); } catch (error) { messageApi.error("Failed to create gallery"); console.error(error); } finally { setIsLoading(false); } }; const handleDeleteGallery = async (galleryId: string) => { try { await removeGallery(galleryId); messageApi.success("Gallery deleted successfully"); } catch (error) { messageApi.error("Failed to delete gallery"); console.error(error); } }; const handleUpdateGallery = async ( galleryId: string, updates: Partial ) => { try { await updateGallery(galleryId, updates); setHasUnsavedChanges(false); messageApi.success("Gallery updated successfully"); } catch (error) { messageApi.error("Failed to update gallery"); console.error(error); } }; return (
{contextHolder} {/* Create Modal */} setIsCreateModalOpen(false)} onCreateGallery={handleCreateGallery} /> {/* Sidebar */}
setIsSidebarOpen(!isSidebarOpen)} onSelectGallery={(gallery) => handleSelectGallery(gallery.id)} onCreateGallery={() => setIsCreateModalOpen(true)} onDeleteGallery={handleDeleteGallery} defaultGalleryId={getDefaultGallery()?.id} onSetDefault={setDefaultGallery} isLoading={isLoading} />
{/* Main Content */}
{/* Breadcrumb */}
Galleries {currentGallery && ( <> {currentGallery.name} )}
{/* Content Area */} {currentGallery ? ( handleUpdateGallery(currentGallery.id, updates) } onDirtyStateChange={setHasUnsavedChanges} /> ) : (
Select a gallery from the sidebar or create a new one
)}
); }; export default GalleryManager;