@payloadcms/plugin-multi-tenant
Version:
Multi Tenant plugin for Payload
111 lines (110 loc) • 3.94 kB
JavaScript
'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 = 'confirmSwitchTenant';
export const TenantSelector = ({ label, viewType })=>{
const { options, preventRefreshOnChange, selectedTenantID, setTenant } = useTenantSelection();
const { 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 (!preventRefreshOnChange) {
switchTenant(option);
return;
} else {
setTenantSelection(option);
openModal(confirmSwitchTenantSlug);
}
}, [
openModal,
preventRefreshOnChange,
switchTenant
]);
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: /*#__PURE__*/ _jsx(Translation, {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
i18nKey: "plugin-multi-tenant:confirm-tenant-switch--heading",
t: t,
variables: {
tenantLabel: getTranslation(label, i18n)
}
}),
modalSlug: confirmSwitchTenantSlug,
onConfirm: ()=>{
switchTenant(tenantSelection);
}
})
]
});
};
//# sourceMappingURL=index.js.map