82 lines
2.5 KiB
TypeScript
82 lines
2.5 KiB
TypeScript
import { useState } from 'react';
|
|
import {
|
|
Dialog,
|
|
DialogContent,
|
|
DialogHeader,
|
|
DialogTitle,
|
|
} from '@/components/ui/dialog';
|
|
import { Input } from '@/components/ui/input';
|
|
import { Checkbox } from '@/components/ui/checkbox';
|
|
import { ScrollArea } from '@/components/ui/scroll-area';
|
|
import { useCollections } from '@/hooks/useCollections';
|
|
import { collectionsApi } from '@/api/collections';
|
|
import type { Collection } from '@/types';
|
|
|
|
interface AddEntryToCollectionDialogProps {
|
|
open: boolean;
|
|
onOpenChange: (open: boolean) => void;
|
|
entryId: number;
|
|
entryCollections?: Collection[];
|
|
}
|
|
|
|
export function AddEntryToCollectionDialog({
|
|
open,
|
|
onOpenChange,
|
|
entryId,
|
|
entryCollections = [],
|
|
}: AddEntryToCollectionDialogProps) {
|
|
const [search, setSearch] = useState('');
|
|
const { data: allCollections = [] } = useCollections();
|
|
const entryCollectionIds = new Set(entryCollections.map((c) => c.id));
|
|
|
|
const filtered = allCollections.filter((c) =>
|
|
c.name.toLowerCase().includes(search.toLowerCase())
|
|
);
|
|
|
|
const handleToggle = async (collectionId: number, checked: boolean) => {
|
|
if (checked) {
|
|
await collectionsApi.addEntry(collectionId, entryId);
|
|
} else {
|
|
await collectionsApi.removeEntry(collectionId, entryId);
|
|
}
|
|
};
|
|
|
|
return (
|
|
<Dialog open={open} onOpenChange={onOpenChange}>
|
|
<DialogContent className="sm:max-w-md">
|
|
<DialogHeader>
|
|
<DialogTitle>Add to Collection</DialogTitle>
|
|
</DialogHeader>
|
|
<Input
|
|
placeholder="Search collections..."
|
|
value={search}
|
|
onChange={(e) => setSearch(e.target.value)}
|
|
/>
|
|
<ScrollArea className="h-64">
|
|
<div className="space-y-2 pr-4">
|
|
{filtered.map((collection) => (
|
|
<label
|
|
key={collection.id}
|
|
className="flex cursor-pointer items-center gap-3 rounded-lg p-2 hover:bg-accent/50"
|
|
>
|
|
<Checkbox
|
|
checked={entryCollectionIds.has(collection.id)}
|
|
onCheckedChange={(checked) =>
|
|
handleToggle(collection.id, !!checked)
|
|
}
|
|
/>
|
|
<span className="text-sm">{collection.name}</span>
|
|
</label>
|
|
))}
|
|
{filtered.length === 0 && (
|
|
<p className="py-4 text-center text-sm text-muted-foreground">
|
|
No collections found.
|
|
</p>
|
|
)}
|
|
</div>
|
|
</ScrollArea>
|
|
</DialogContent>
|
|
</Dialog>
|
|
);
|
|
}
|