import { AppDropdownSelect } from '@/components/ui/dropdown'; import { AppMaterialIcons } from '@/components/ui'; import { AppTooltip } from '@/components/ui/tooltip'; import { useAppLayoutContextSelector } from '@/context/BusterAppLayout'; import { useBusterCollectionListContextSelector } from '@/context/Collections'; import { BusterRoutes, createBusterRoute } from '@/routes/busterRoutes'; import { useMemoizedFn } from 'ahooks'; import { Button } from 'antd'; import React, { useMemo } from 'react'; import type { BusterCollectionListItem } from '@/api/asset_interfaces'; import { NewCollectionModal } from '../modals/NewCollectionModal'; export const SaveToCollectionsDropdown: React.FC<{ children: React.ReactNode; selectedCollections: string[]; onSaveToCollection: (collectionId: string[]) => Promise; onRemoveFromCollection: (collectionId: string) => Promise; }> = React.memo(({ children, onRemoveFromCollection, onSaveToCollection, selectedCollections }) => { const collectionsList = useBusterCollectionListContextSelector((x) => x.collectionsList); const onChangePage = useAppLayoutContextSelector((s) => s.onChangePage); const [openCollectionModal, setOpenCollectionModal] = React.useState(false); const [showDropdown, setShowDropdown] = React.useState(false); const items = useMemo( () => (collectionsList || []).map((collection) => { return { key: collection.id, label: collection.name, onClick: () => onClickItem(collection), link: createBusterRoute({ route: BusterRoutes.APP_COLLECTIONS_ID, collectionId: collection.id }) }; }), [collectionsList] ); const onClickItem = useMemoizedFn((collection: BusterCollectionListItem) => { const isSelected = selectedCollections.some((id) => id === collection.id); if (isSelected) { onRemoveFromCollection(collection.id); } else { const allCollectionsAndSelected = selectedCollections.map((id) => id).concat(collection.id); onSaveToCollection(allCollectionsAndSelected); } }); const onCollectionCreated = useMemoizedFn(async (collectionId: string) => { await onSaveToCollection([collectionId]); onChangePage({ route: BusterRoutes.APP_COLLECTIONS_ID, collectionId }); }); const onCloseCollectionModal = useMemoizedFn(() => { setOpenCollectionModal(false); setShowDropdown(false); }); const onOpenChange = useMemoizedFn((open: boolean) => { setShowDropdown(open); }); const onClick = useMemoizedFn(() => { setOpenCollectionModal(true); setShowDropdown(false); }); const memoizedButton = useMemo(() => { return ( ); }, [onClick]); return ( <> {showDropdown ? ( <>{children} ) : ( {children} )} ); }); SaveToCollectionsDropdown.displayName = 'SaveToCollectionsDropdown';