@payloadcms/plugin-multi-tenant
Version:
Multi Tenant plugin for Payload
47 lines (46 loc) • 1.68 kB
JavaScript
import { jsx as _jsx } from "react/jsx-runtime";
import { cookies as getCookies } from 'next/headers.js';
import { findTenantOptions } from '../../queries/findTenantOptions.js';
import { TenantSelectionProviderClient } from './index.client.js';
export const TenantSelectionProvider = async ({ children, payload, tenantsCollectionSlug, useAsTitle, user })=>{
let tenantOptions = [];
try {
const { docs } = await findTenantOptions({
limit: 0,
payload,
tenantsCollectionSlug,
useAsTitle,
user
});
tenantOptions = docs.map((doc)=>({
label: String(doc[useAsTitle]),
value: doc.id
}));
} catch (_) {
// user likely does not have access
}
const cookies = await getCookies();
let tenantCookie = cookies.get('payload-tenant')?.value;
let initialValue = undefined;
/**
* Ensure the cookie is a valid tenant
*/ if (tenantCookie) {
const matchingOption = tenantOptions.find((option)=>String(option.value) === tenantCookie);
if (matchingOption) {
initialValue = matchingOption.value;
}
}
/**
* If the there was no cookie or the cookie was an invalid tenantID set intialValue
*/ if (!initialValue) {
tenantCookie = undefined;
initialValue = tenantOptions.length > 1 ? undefined : tenantOptions[0]?.value;
}
return /*#__PURE__*/ _jsx(TenantSelectionProviderClient, {
initialValue: initialValue,
tenantCookie: tenantCookie,
tenantOptions: tenantOptions,
children: children
});
};
//# sourceMappingURL=index.js.map