import type { Library } from "@/api/types"; import { Badge } from "@/components/ui/badge"; import { Label } from "@/components/ui/label"; import { Switch } from "@/components/ui/switch"; interface LibraryAccessSelectorProps { libraries: Library[]; value: number[] | null; onChange: (value: number[] | null) => void; } function sortByLibraryOrder(libraries: Library[], ids: number[]) { const selected = new Set(ids); return libraries.filter((library) => selected.has(library.id)).map((library) => library.id); } export function LibraryAccessSelector({ libraries, value, onChange }: LibraryAccessSelectorProps) { const allLibraries = value === null; function handleAllLibrariesChange(checked: boolean) { onChange(checked ? null : libraries.map((library) => library.id)); } function handleLibraryToggle(libraryId: number, checked: boolean) { const current = value ?? libraries.map((library) => library.id); const next = checked ? sortByLibraryOrder(libraries, [...current, libraryId]) : current.filter((id) => id !== libraryId); onChange(next); } return (
No libraries available.
) : ( libraries.map((library) => { const checked = value?.includes(library.id) ?? false; return (