'use client';
import { useState, useMemo } from 'react';
import {
Stack,
TextInput,
Text,
Box,
Checkbox,
Badge,
Group,
} from '@mantine/core';
import { ApiClient } from '@/api-client/ApiClient';
import { useCollectionSearch } from '@/hooks/useCollectionSearch';
import { CreateCollectionModal } from './CreateCollectionModal';
interface Collection {
id: string;
name: string;
description?: string;
cardCount?: number;
authorId: string;
}
interface CollectionSelectorProps {
apiClient: ApiClient;
userId?: string;
selectedCollectionIds: string[];
onSelectionChange: (collectionIds: string[]) => void;
existingCollections?: Collection[];
disabled?: boolean;
showCreateOption?: boolean;
placeholder?: string;
preSelectedCollectionId?: string | null;
}
export function CollectionSelector({
apiClient,
userId,
selectedCollectionIds,
onSelectionChange,
existingCollections = [],
disabled = false,
showCreateOption = true,
placeholder = 'Search collections...',
preSelectedCollectionId,
}: CollectionSelectorProps) {
const [createModalOpen, setCreateModalOpen] = useState(false);
// Get existing collection IDs for filtering
const existingCollectionIds = useMemo(() => {
return existingCollections.map((collection) => collection.id);
}, [existingCollections]);
// Collection search hook
const {
collections: allCollections,
loading: collectionsLoading,
searchText,
setSearchText,
handleSearchKeyPress,
loadCollections,
} = useCollectionSearch({
apiClient,
initialLoad: true,
});
// Filter out existing collections from search results
const availableCollections = useMemo(() => {
return allCollections.filter(
(collection) => !existingCollectionIds.includes(collection.id),
);
}, [allCollections, existingCollectionIds]);
const handleCollectionToggle = (collectionId: string) => {
const newSelection = selectedCollectionIds.includes(collectionId)
? selectedCollectionIds.filter((id) => id !== collectionId)
: [...selectedCollectionIds, collectionId];
onSelectionChange(newSelection);
};
const handleCreateCollection = (e?: React.MouseEvent) => {
e?.preventDefault();
e?.stopPropagation();
setCreateModalOpen(true);
};
const handleCreateCollectionSuccess = (
collectionId: string,
collectionName: string,
) => {
onSelectionChange([...selectedCollectionIds, collectionId]);
loadCollections(searchText.trim() || undefined);
setCreateModalOpen(false);
};
return (
<>
Collections
{/* Show existing collections */}
{existingCollections.length > 0 && (
Already in {existingCollections.length} collection
{existingCollections.length !== 1 && 's'}:
{existingCollections.map((collection) => (
{collection.name}
))}
)}
{existingCollections.length > 0
? 'Add to additional collections (optional)'
: 'Select collections (optional)'}
setSearchText(e.currentTarget.value)}
onKeyPress={handleSearchKeyPress}
disabled={disabled}
size="sm"
/>
{availableCollections.length > 0 ? (
{availableCollections.length} collection
{availableCollections.length !== 1 && 's'} found
{searchText.trim() && showCreateOption && (
handleCreateCollection(e)}
>
{`Create new collection "${searchText.trim()}"`}
Click to create a new collection with this name
+ New
)}
{availableCollections.map((collection, index) => (
handleCollectionToggle(collection.id)}
>
{collection.name}
{collection.cardCount} cards
{collection.description && (
{collection.description}
)}
handleCollectionToggle(collection.id)}
disabled={disabled}
onClick={(e) => e.stopPropagation()}
size="sm"
/>
))}
) : searchText.trim() ? (
{!collectionsLoading && (
{`No collections found for "${searchText.trim()}"`}
)}
{showCreateOption && (
handleCreateCollection(e)}
>
{`Create new collection "${searchText.trim()}"`}
Click to create a new collection with this name
+ New
)}
) : (
No collections found. You can create collections from your
library.
)}
{showCreateOption && (
setCreateModalOpen(false)}
onSuccess={handleCreateCollectionSuccess}
apiClient={apiClient}
initialName={searchText.trim()}
/>
)}
>
);
}