import { useMemo, useState } from "react"; import { Link, useNavigate, useParams, useSearchParams } from "react-router"; import { ArrowLeft } from "lucide-react"; import { Button } from "@/components/ui/button"; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"; import { CollectionTemplateGallery } from "@/components/CollectionTemplateGallery"; import { useAdminLibraries } from "@/hooks/queries/admin/libraries"; import { useAdminCollections } from "@/hooks/queries/admin/collections"; import { useDocumentTitle } from "@/hooks/useDocumentTitle"; import { buildAdminCollectionsReturnPath, CollectionEditForm, CollectionForm, MDBListImportForm, SourceTypeSelector, TMDBPresetForm, TraktPresetForm, type CollectionSourceType, } from "./adminCollectionsShared"; import SmartCollectionWizard from "./SmartCollectionWizard"; function inferCollectionSourceType(collectionType?: string): CollectionSourceType { if (collectionType === "mdblist") return "mdblist"; if (collectionType === "tmdb") return "tmdb"; if (collectionType === "trakt") return "trakt"; return "manual"; } function isImportedAdminCollectionType(collectionType?: string): boolean { return collectionType === "mdblist" || collectionType === "tmdb" || collectionType === "trakt"; } export default function AdminCollectionEditor() { const navigate = useNavigate(); const { id } = useParams<{ id: string }>(); const [searchParams] = useSearchParams(); const initialLibraryId = Number(searchParams.get("libraryId")) || null; const returnPath = buildAdminCollectionsReturnPath(initialLibraryId); const isCreate = !id; const { data: libraries = [] } = useAdminLibraries(); const { data: collections = [], isLoading } = useAdminCollections(); const collection = useMemo( () => collections.find((entry) => entry.id === id) ?? null, [collections, id], ); const [sourceType, setSourceType] = useState(null); const [galleryOpen, setGalleryOpen] = useState(false); const activeSourceType = collection ? inferCollectionSourceType(collection.collection_type) : sourceType; const sourceTypeTitles: Record = { manual: "New Manual Collection", mdblist: "Import MDBList Collection", tmdb: "Import TMDB Collection", trakt: "Import Trakt Collection", }; const title = collection ? `Edit ${collection.title}` : (activeSourceType && sourceTypeTitles[activeSourceType]) || "Add Collection"; const description = collection ? "Collections now open in a dedicated workspace so rules, artwork, and preview can stay visible." : activeSourceType === null ? "Choose how this collection should be created." : "Build the collection in a full-page editor instead of a cramped dialog."; useDocumentTitle(title); if (isLoading && libraries.length === 0) { return
Loading collection editor...
; } if (!isCreate && !collection && !isLoading) { return (
Collection not found The selected collection could not be loaded.
); } // The wizard owns its own page chrome (back button, title, step indicator). // Short-circuit the legacy editor shell so we don't render nested headers. const useWizard = (collection && collection.collection_type === "smart") || (!collection && activeSourceType === "manual"); if (useWizard) { return ( navigate(returnPath)} /> ); } return (

{title}

{description}

{!collection && activeSourceType !== null ? ( ) : null}
{!collection && activeSourceType === null ? ( Choose a Collection Type Smart/manual collections open the full query builder. Imports keep their source-specific setup. { if (type === "templates") { setGalleryOpen(true); } else { setSourceType(type); } }} /> ) : null} { if (isCreate) navigate(returnPath); }} /> {collection ? ( // Smart admin collections route to the wizard above; here we only see // imported (mdblist/tmdb/trakt) or legacy manual collections. isImportedAdminCollectionType(collection.collection_type) ? ( navigate(returnPath)} /> ) : ( navigate(returnPath)} /> ) ) : null} {!collection && activeSourceType === "mdblist" ? ( navigate(returnPath)} /> ) : null} {!collection && activeSourceType === "tmdb" ? ( navigate(returnPath)} /> ) : null} {!collection && activeSourceType === "trakt" ? ( navigate(returnPath)} /> ) : null}
); }