UNPKG

@payloadcms/plugin-multi-tenant

Version:
85 lines (84 loc) 3.29 kB
'use client'; import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime"; import { getTranslation } from '@payloadcms/translations'; import { ConfirmationModal, SelectInput, useModal, useTranslation } from '@payloadcms/ui'; import React from 'react'; import { useTenantSelection } from '../../providers/TenantSelectionProvider/index.client.js'; import './index.scss'; const confirmLeaveWithoutSavingSlug = 'confirm-leave-without-saving'; export const TenantSelectorClient = ({ disabled: disabledFromProps, label, viewType })=>{ const { entityType, modified, options, selectedTenantID, setTenant } = useTenantSelection(); const { closeModal, openModal } = useModal(); const { i18n, t } = useTranslation(); const [tenantSelection, setTenantSelection] = React.useState(); 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 === '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); } }, [ 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: label ? getTranslation(label, i18n) : t('plugin-multi-tenant:nav-tenantSelector-label'), name: "setTenant", onChange: onChange, options: options, path: "setTenant", readOnly: disabledFromProps || entityType !== 'global' && viewType && [ 'document', 'version' ].includes(viewType), value: selectedTenantID }), /*#__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.client.js.map