import { Button, DialogDescription, Heading, Modal, ModalBody, ModalFooter, ModalTitle, Portal, Tabs, TabsBar, TabsPanel, useToast } from "@vertesia/ui/core"; import { useUserSession } from "@vertesia/ui/session"; import { useCallback, useState } from "react"; import { useUITranslation } from '../../../../../i18n/index.js'; import { i18nInstance, NAMESPACE } from '../../../../../i18n/instance.js'; import { CreateCollectionForm, SelectCollection } from "../../../collections"; import { useObjectsActionCallback } from "../ObjectsActionHooks"; import { ActionComponentTypeProps, ObjectsActionSpec } from "../ObjectsActionSpec"; export function AddToCollectionActionComponent({ action, objectIds }: ActionComponentTypeProps) { const [showModal, setShowModal] = useState(false); const callback = useCallback(() => { if (objectIds.length > 0) { setShowModal(true); } return Promise.resolve(true); }, [objectIds]) const onClose = () => { setShowModal(false) } useObjectsActionCallback(action.id, callback); return showModal && ; } const t = i18nInstance.getFixedT(null, NAMESPACE); export const AddToCollectionAction: ObjectsActionSpec = { id: "addToCollection", name: t('store.actions.addToCollection'), description: t('store.actions.addToCollectionDesc'), confirm: false, component: AddToCollectionActionComponent, } interface SelectCollectionModalProps { isOpen: boolean; onClose: () => void; objectIds: string[]; } function SelectCollectionModal({ isOpen, onClose, objectIds }: SelectCollectionModalProps) { const { t } = useUITranslation(); return ( {t('store.actions.addToCollectionTitle')} {t('store.actions.addToCollectionBody')}
) } interface AddToCollectionFormProps { objectIds: string[]; onClose: () => void; } function AddToCollectionForm({ onClose, objectIds }: AddToCollectionFormProps) { const { t } = useUITranslation(); const toast = useToast(); const { client } = useUserSession(); const [selectedCollectionId, setSelectedCollectionId] = useState(); const onAddToCollection = ({ collectionId }: { collectionId: string }) => { if (!collectionId || !objectIds?.length) { return; } client.store.collections.addMembers(collectionId, objectIds).then(() => { toast({ title: t('store.actions.addToCollectionSuccess'), status: 'success', description: t('store.actions.addToCollectionSuccessDesc', { count: objectIds.length }), duration: 3000 }); onClose(); }).catch(() => { toast({ title: t('store.actions.addToCollectionFailure'), status: 'error', description: t('store.actions.addToCollectionFailureDesc'), duration: 5000 }); }); } const onCollectionChange = (collectionId: string | string[] | undefined, _collection?: any) => { if (typeof collectionId === "string" || typeof collectionId === "undefined") { setSelectedCollectionId(collectionId); } else if (Array.isArray(collectionId) && collectionId.length > 0) { setSelectedCollectionId(collectionId[0]); } else { setSelectedCollectionId(undefined); } }; const tabs = [ { name: 'select', label: t('store.actions.selectCollection'), content: (
{t('store.actions.chooseExistingCollections')}
) }, { name: 'create', label: t('store.actions.createNew'), content:
onAddToCollection({ collectionId: id })} redirect={false} />
} ]; return ( ) }