UNPKG

@payloadcms/plugin-multi-tenant

Version:
132 lines (131 loc) 5 kB
'use client'; import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime"; import { getTranslation } from '@payloadcms/translations'; import { ConfirmationModal, SelectInput, Translation, useModal, useTranslation } from '@payloadcms/ui'; import React from 'react'; import { useTenantSelection } from '../../providers/TenantSelectionProvider/index.client.js'; import './index.scss'; const confirmSwitchTenantSlug = 'confirm-switch-tenant'; const confirmLeaveWithoutSavingSlug = 'confirm-leave-without-saving'; export const TenantSelector = ({ label, viewType })=>{ const { entityType, modified, options, selectedTenantID, setTenant } = useTenantSelection(); const { closeModal, openModal } = useModal(); const { i18n, t } = useTranslation(); const [tenantSelection, setTenantSelection] = React.useState(); const selectedValue = React.useMemo(()=>{ if (selectedTenantID) { return options.find((option)=>option.value === selectedTenantID); } return undefined; }, [ options, selectedTenantID ]); const newSelectedValue = React.useMemo(()=>{ if (tenantSelection && 'value' in tenantSelection) { return options.find((option)=>option.value === tenantSelection.value); } return undefined; }, [ options, tenantSelection ]); const switchTenant = React.useCallback((option)=>{ if (option && 'value' in option) { setTenant({ id: option.value, refresh: true }); } else { setTenant({ id: undefined, refresh: true }); } }, [ setTenant ]); const onChange = React.useCallback((option)=>{ if (option && 'value' in option && option.value === selectedTenantID) { // If the selected option is the same as the current tenant, do nothing return; } if (entityType !== 'document') { if (entityType === 'global' && modified) { // If the entityType is 'global' and there are unsaved changes, prompt for confirmation setTenantSelection(option); openModal(confirmLeaveWithoutSavingSlug); } else { // If the entityType is not 'document', switch tenant without confirmation switchTenant(option); } } else { // non-unique documents should always prompt for confirmation setTenantSelection(option); openModal(confirmSwitchTenantSlug); } }, [ selectedTenantID, entityType, modified, switchTenant, openModal ]); if (options.length <= 1) { return null; } return /*#__PURE__*/ _jsxs("div", { className: "tenant-selector", children: [ /*#__PURE__*/ _jsx(SelectInput, { isClearable: viewType === 'list', label: getTranslation(label, i18n), name: "setTenant", onChange: onChange, options: options, path: "setTenant", value: selectedTenantID }), /*#__PURE__*/ _jsx(ConfirmationModal, { body: /*#__PURE__*/ _jsx(Translation, { elements: { 0: ({ children })=>{ return /*#__PURE__*/ _jsx("b", { children: children }); } }, // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-expect-error i18nKey: "plugin-multi-tenant:confirm-tenant-switch--body", t: t, variables: { fromTenant: selectedValue?.label, toTenant: newSelectedValue?.label } }), heading: t('plugin-multi-tenant:confirm-tenant-switch--heading', { tenantLabel: getTranslation(label, i18n) }), modalSlug: confirmSwitchTenantSlug, onConfirm: ()=>{ switchTenant(tenantSelection); } }), /*#__PURE__*/ _jsx(ConfirmationModal, { body: t('general:changesNotSaved'), cancelLabel: t('general:stayOnThisPage'), confirmLabel: t('general:leaveAnyway'), heading: t('general:leaveWithoutSaving'), modalSlug: confirmLeaveWithoutSavingSlug, onCancel: ()=>{ closeModal(confirmLeaveWithoutSavingSlug); }, onConfirm: ()=>{ switchTenant(tenantSelection); } }) ] }); }; //# sourceMappingURL=index.js.map