import { UrlCardView } from '@/api-client/types'; import useCollectionSearch from '@/features/collections/lib/queries/useCollectionSearch'; import { DEFAULT_OVERLAY_PROPS } from '@/styles/overlays'; import { Group, Modal, Stack, Text, TextInput, CloseButton, Tabs, ScrollArea, Button, Loader, Alert, } from '@mantine/core'; import { useDebouncedValue } from '@mantine/hooks'; import { notifications } from '@mantine/notifications'; import { Fragment, useState } from 'react'; import { IoSearch } from 'react-icons/io5'; import { BiPlus } from 'react-icons/bi'; import CollectionSelectorError from '../../../collections/components/collectionSelector/Error.CollectionSelector'; import CollectionSelectorItemList from '../../../collections/components/collectionSelectorItemList/CollectionSelectorItemList'; import CreateCollectionDrawer from '../../../collections/components/createCollectionDrawer/CreateCollectionDrawer'; import CardToBeAddedPreview from './CardToBeAddedPreview'; import useAddCardToLibrary from '../../lib/mutations/useAddCardToLibrary'; import useGetCardFromMyLibrary from '../../lib/queries/useGetCardFromMyLibrary'; import useMyCollections from '../../../collections/lib/queries/useMyCollections'; interface Props { isOpen: boolean; onClose: () => void; cardContent: UrlCardView['cardContent']; cardId: string; } export default function AddCardToModal(props: Props) { const [isDrawerOpen, setIsDrawerOpen] = useState(false); const [search, setSearch] = useState(''); const [debouncedSearch] = useDebouncedValue(search, 200); const searchedCollections = useCollectionSearch({ query: debouncedSearch }); const addCardToLibrary = useAddCardToLibrary(); const cardStaus = useGetCardFromMyLibrary({ url: props.cardContent.url }); const { data, error } = useMyCollections(); const [selectedCollections, setSelectedCollections] = useState< SelectableCollectionItem[] >([]); const handleCollectionChange = ( checked: boolean, item: SelectableCollectionItem, ) => { if (checked) { if (!selectedCollections.some((col) => col.id === item.id)) { setSelectedCollections([...selectedCollections, item]); } } else { setSelectedCollections( selectedCollections.filter((col) => col.id !== item.id), ); } }; const allCollections = data?.pages.flatMap((page) => page.collections ?? []) ?? []; const collectionsWithCard = allCollections.filter((c) => cardStaus.data.collections?.some((col) => col.id === c.id), ); const collectionsWithoutCard = allCollections.filter( (c) => !collectionsWithCard.some((col) => col.id === c.id), ); const isInUserLibrary = collectionsWithCard.length > 0; const hasCollections = allCollections.length > 0; const hasSelectedCollections = selectedCollections.length > 0; const handleAddCard = (e: React.FormEvent) => { e.preventDefault(); addCardToLibrary.mutate( { url: props.cardContent.url, collectionIds: selectedCollections.map((c) => c.id), }, { onSuccess: () => { setSelectedCollections([]); props.onClose(); }, onError: () => { notifications.show({ message: 'Could not add card.', }); }, onSettled: () => { setSelectedCollections([]); props.onClose(); }, }, ); }; if (error) { return ; } return ( { setSearch(e.currentTarget.value); }} size="md" variant="filled" id="search" leftSection={} rightSection={ setSearch('')} style={{ display: search ? undefined : 'none' }} /> } /> Collections Selected ({selectedCollections.length}) {search ? ( {searchedCollections.isPending && ( Searching collections... )} {searchedCollections.data && (searchedCollections.data.collections.length === 0 ? ( ) : ( ))} ) : hasCollections ? ( ) : ( No collections )} {hasSelectedCollections ? ( ) : ( )} {hasSelectedCollections && ( )} setIsDrawerOpen(false)} initialName={search} onCreate={(newCollection) => { setSelectedCollections([...selectedCollections, newCollection]); }} /> ); }