@payloadcms/plugin-multi-tenant
Version:
Multi Tenant plugin for Payload
126 lines (125 loc) • 4.07 kB
JavaScript
'use client';
import { jsx as _jsx } from "react/jsx-runtime";
import { useAuth } from '@payloadcms/ui';
import { useRouter } from 'next/navigation.js';
import React, { createContext } from 'react';
const Context = /*#__PURE__*/ createContext({
options: [],
selectedTenantID: undefined,
setPreventRefreshOnChange: ()=>null,
setTenant: ()=>null
});
export const TenantSelectionProviderClient = ({ children, initialValue, tenantCookie, tenantOptions })=>{
const [selectedTenantID, setSelectedTenantID] = React.useState(initialValue);
const [preventRefreshOnChange, setPreventRefreshOnChange] = React.useState(false);
const { user } = useAuth();
const userID = React.useMemo(()=>user?.id, [
user?.id
]);
const selectedTenantLabel = React.useMemo(()=>tenantOptions.find((option)=>option.value === selectedTenantID)?.label, [
selectedTenantID,
tenantOptions
]);
const router = useRouter();
const setCookie = React.useCallback((value)=>{
const expires = '; expires=Fri, 31 Dec 9999 23:59:59 GMT';
document.cookie = 'payload-tenant=' + (value || '') + expires + '; path=/';
}, []);
const deleteCookie = React.useCallback(()=>{
document.cookie = 'payload-tenant=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/';
}, []);
const setTenant = React.useCallback(({ id, refresh })=>{
if (id === undefined) {
if (tenantOptions.length > 1) {
setSelectedTenantID(undefined);
deleteCookie();
} else {
setSelectedTenantID(tenantOptions[0]?.value);
setCookie(String(tenantOptions[0]?.value));
}
} else {
setSelectedTenantID(id);
setCookie(String(id));
}
if (!preventRefreshOnChange && refresh) {
router.refresh();
}
}, [
deleteCookie,
preventRefreshOnChange,
router,
setCookie,
setSelectedTenantID,
tenantOptions
]);
React.useEffect(()=>{
if (selectedTenantID && !tenantOptions.find((option)=>option.value === selectedTenantID)) {
if (tenantOptions?.[0]?.value) {
setTenant({
id: tenantOptions[0].value,
refresh: true
});
} else {
setTenant({
id: undefined,
refresh: true
});
}
}
}, [
tenantCookie,
setTenant,
selectedTenantID,
tenantOptions,
initialValue,
setCookie
]);
React.useEffect(()=>{
if (userID && !tenantCookie) {
// User is logged in, but does not have a tenant cookie, set it
setSelectedTenantID(initialValue);
if (initialValue) {
setCookie(String(initialValue));
} else {
deleteCookie();
}
}
}, [
userID,
tenantCookie,
initialValue,
setCookie,
deleteCookie,
router
]);
React.useEffect(()=>{
if (!userID && tenantCookie) {
// User is not logged in, but has a tenant cookie, delete it
deleteCookie();
setSelectedTenantID(undefined);
} else if (userID) {
// User changed, refresh
router.refresh();
}
}, [
userID,
tenantCookie,
deleteCookie,
router
]);
return /*#__PURE__*/ _jsx("span", {
"data-selected-tenant-id": selectedTenantID,
"data-selected-tenant-title": selectedTenantLabel,
children: /*#__PURE__*/ _jsx(Context, {
value: {
options: tenantOptions,
selectedTenantID,
setPreventRefreshOnChange,
setTenant
},
children: children
})
});
};
export const useTenantSelection = ()=>React.use(Context);
//# sourceMappingURL=index.client.js.map