import type { LibraryTabCollection, LibraryTabGroup, LibraryTabUngrouped } from "@/api/types";
import { useLibraryCollections } from "@/hooks/queries/libraryCollections";
import { Card, CardContent } from "@/components/ui/card";
import { Skeleton } from "@/components/ui/skeleton";
import {
COLLECTION_POSTER_GRID_CLASSES as GRID_CLASSES,
CollectionPosterCard,
} from "@/components/collections/CollectionPosterCard";
interface LibraryCollectionsProps {
libraryId: number;
}
export default function LibraryCollections({ libraryId }: LibraryCollectionsProps) {
const { data, isLoading } = useLibraryCollections(libraryId);
if (isLoading) {
return (
{Array.from({ length: 24 }, (_, i) => (
))}
);
}
const groups = data?.groups ?? [];
const ungroupedData = data?.ungrouped ?? null;
const ungrouped = ungroupedData?.collections ?? [];
if (groups.length === 0 && ungrouped.length === 0) {
return (
No collections yet
Create library collections from the admin area to feature curated shelves here.
);
}
return (
Collections
Browse hand-picked shelves and smart lists created for this library.
{buildRenderOrder(groups, ungroupedData).map((item) =>
item.kind === "ungrouped" ? (
) : (
),
)}
);
}
// Build a unified render order from groups + (optional) ungrouped, sorted by
// each item's effective sort position.
type RenderItem =
| { kind: "group"; group: LibraryTabGroup }
| { kind: "ungrouped"; collections: LibraryTabCollection[] };
function buildRenderOrder(
groups: LibraryTabGroup[],
ungroupedData: LibraryTabUngrouped | null,
): RenderItem[] {
type Slot = { order: number; item: RenderItem };
const slots: Slot[] = groups.map((g) => ({
order: g.sort_order,
item: { kind: "group" as const, group: g },
}));
if (ungroupedData && ungroupedData.collections.length > 0) {
slots.push({
order: ungroupedData.sort_order,
item: { kind: "ungrouped" as const, collections: ungroupedData.collections },
});
}
slots.sort((a, b) => a.order - b.order);
return slots.map((s) => s.item);
}
function UngroupedGroupSection({
collections,
libraryId,
}: {
collections: LibraryTabCollection[];
libraryId: number;
}) {
return (
{collections.map((c) => (
))}
);
}
function GroupSection({ group, libraryId }: { group: LibraryTabGroup; libraryId: number }) {
return (
{group.name}
{group.collections.map((c) => (
))}
);
}