chore(plugin-multi-tenant): remove SELECT_ALL constant (#11660)

This commit is contained in:
Jarrod Flesch
2025-03-12 11:23:03 -04:00
committed by GitHub
parent c4fd27de01
commit 39d783a361
11 changed files with 700 additions and 719 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -5,7 +5,6 @@ import type { RelationshipFieldClientProps } from 'payload'
import { RelationshipField, useField } from '@payloadcms/ui' import { RelationshipField, useField } from '@payloadcms/ui'
import React from 'react' import React from 'react'
import { SELECT_ALL } from '../../constants.js'
import { useTenantSelection } from '../../providers/TenantSelectionProvider/index.client.js' import { useTenantSelection } from '../../providers/TenantSelectionProvider/index.client.js'
import './index.scss' import './index.scss'
@@ -30,14 +29,11 @@ export const TenantField = (args: Props) => {
setTenant({ id: value, refresh: unique }) setTenant({ id: value, refresh: unique })
} else { } else {
// in the document view, the tenant field should always have a value // in the document view, the tenant field should always have a value
const defaultValue = const defaultValue = selectedTenantID || options[0]?.value
!selectedTenantID || selectedTenantID === SELECT_ALL
? options[0]?.value
: selectedTenantID
setTenant({ id: defaultValue, refresh: unique }) setTenant({ id: defaultValue, refresh: unique })
} }
hasSetValueRef.current = true hasSetValueRef.current = true
} else if ((!value || value !== selectedTenantID) && selectedTenantID !== SELECT_ALL) { } else if (!value || value !== selectedTenantID) {
// Update the field on the document value when the tenant is changed // Update the field on the document value when the tenant is changed
setValue(selectedTenantID) setValue(selectedTenantID)
} }

View File

@@ -7,7 +7,6 @@ import './index.scss'
import { SelectInput, useTranslation } from '@payloadcms/ui' import { SelectInput, useTranslation } from '@payloadcms/ui'
import React from 'react' import React from 'react'
import { SELECT_ALL } from '../../constants.js'
import { useTenantSelection } from '../../providers/TenantSelectionProvider/index.client.js' import { useTenantSelection } from '../../providers/TenantSelectionProvider/index.client.js'
export const TenantSelector = ({ label, viewType }: { label: string; viewType?: ViewTypes }) => { export const TenantSelector = ({ label, viewType }: { label: string; viewType?: ViewTypes }) => {
@@ -38,13 +37,7 @@ export const TenantSelector = ({ label, viewType }: { label: string; viewType?:
onChange={handleChange} onChange={handleChange}
options={options} options={options}
path="setTenant" path="setTenant"
value={ value={selectedTenantID as string | undefined}
selectedTenantID
? selectedTenantID === SELECT_ALL
? undefined
: (selectedTenantID as string)
: undefined
}
/> />
</div> </div>
) )

View File

@@ -1,2 +0,0 @@
// The tenant cookie can be set to _ALL_ to allow users to see all results for tenants they are a member of.
export const SELECT_ALL = '_ALL_'

View File

@@ -1,6 +1,5 @@
import type { PayloadRequest, Where } from 'payload' import type { PayloadRequest, Where } from 'payload'
import { SELECT_ALL } from '../constants.js'
import { getCollectionIDType } from '../utilities/getCollectionIDType.js' import { getCollectionIDType } from '../utilities/getCollectionIDType.js'
import { getTenantFromCookie } from '../utilities/getTenantFromCookie.js' import { getTenantFromCookie } from '../utilities/getTenantFromCookie.js'
@@ -20,13 +19,13 @@ export const filterDocumentsBySelectedTenant = ({
}) })
const selectedTenant = getTenantFromCookie(req.headers, idType) const selectedTenant = getTenantFromCookie(req.headers, idType)
if (selectedTenant === SELECT_ALL) { if (selectedTenant) {
return {} return {
[tenantFieldName]: {
equals: selectedTenant,
},
}
} }
return { return {}
[tenantFieldName]: {
equals: selectedTenant,
},
}
} }

View File

@@ -1,6 +1,5 @@
import type { PayloadRequest, Where } from 'payload' import type { PayloadRequest, Where } from 'payload'
import { SELECT_ALL } from '../constants.js'
import { getCollectionIDType } from '../utilities/getCollectionIDType.js' import { getCollectionIDType } from '../utilities/getCollectionIDType.js'
import { getTenantFromCookie } from '../utilities/getTenantFromCookie.js' import { getTenantFromCookie } from '../utilities/getTenantFromCookie.js'
@@ -18,13 +17,13 @@ export const filterTenantsBySelectedTenant = ({
}) })
const selectedTenant = getTenantFromCookie(req.headers, idType) const selectedTenant = getTenantFromCookie(req.headers, idType)
if (selectedTenant === SELECT_ALL) { if (selectedTenant) {
return {} return {
id: {
equals: selectedTenant,
},
}
} }
return { return {}
id: {
equals: selectedTenant,
},
}
} }

View File

@@ -1,6 +1,5 @@
import type { PayloadRequest, Where } from 'payload' import type { PayloadRequest, Where } from 'payload'
import { SELECT_ALL } from '../constants.js'
import { getCollectionIDType } from '../utilities/getCollectionIDType.js' import { getCollectionIDType } from '../utilities/getCollectionIDType.js'
import { getTenantFromCookie } from '../utilities/getTenantFromCookie.js' import { getTenantFromCookie } from '../utilities/getTenantFromCookie.js'
@@ -25,13 +24,13 @@ export const filterUsersBySelectedTenant = ({
}) })
const selectedTenant = getTenantFromCookie(req.headers, idType) const selectedTenant = getTenantFromCookie(req.headers, idType)
if (selectedTenant === SELECT_ALL) { if (selectedTenant) {
return {} return {
[`${tenantsArrayFieldName}.${tenantsArrayTenantFieldName}`]: {
in: [selectedTenant],
},
}
} }
return { return {}
[`${tenantsArrayFieldName}.${tenantsArrayTenantFieldName}`]: {
in: [selectedTenant],
},
}
} }

View File

@@ -6,8 +6,6 @@ import { useAuth } from '@payloadcms/ui'
import { useRouter } from 'next/navigation.js' import { useRouter } from 'next/navigation.js'
import React, { createContext } from 'react' import React, { createContext } from 'react'
import { SELECT_ALL } from '../../constants.js'
type ContextType = { type ContextType = {
/** /**
* Array of options to select from * Array of options to select from
@@ -76,8 +74,8 @@ export const TenantSelectionProviderClient = ({
({ id, refresh }) => { ({ id, refresh }) => {
if (id === undefined) { if (id === undefined) {
if (tenantOptions.length > 1) { if (tenantOptions.length > 1) {
setSelectedTenantID(SELECT_ALL) setSelectedTenantID(undefined)
setCookie(SELECT_ALL) deleteCookie()
} else { } else {
setSelectedTenantID(tenantOptions[0]?.value) setSelectedTenantID(tenantOptions[0]?.value)
setCookie(String(tenantOptions[0]?.value)) setCookie(String(tenantOptions[0]?.value))
@@ -90,15 +88,11 @@ export const TenantSelectionProviderClient = ({
router.refresh() router.refresh()
} }
}, },
[setSelectedTenantID, setCookie, router, preventRefreshOnChange, tenantOptions], [deleteCookie, preventRefreshOnChange, router, setCookie, setSelectedTenantID, tenantOptions],
) )
React.useEffect(() => { React.useEffect(() => {
if ( if (selectedTenantID && !tenantOptions.find((option) => option.value === selectedTenantID)) {
selectedTenantID &&
selectedTenantID !== SELECT_ALL &&
!tenantOptions.find((option) => option.value === selectedTenantID)
) {
if (tenantOptions?.[0]?.value) { if (tenantOptions?.[0]?.value) {
setTenant({ id: tenantOptions[0].value, refresh: true }) setTenant({ id: tenantOptions[0].value, refresh: true })
} else { } else {
@@ -111,9 +105,13 @@ export const TenantSelectionProviderClient = ({
if (userID && !tenantCookie) { if (userID && !tenantCookie) {
// User is logged in, but does not have a tenant cookie, set it // User is logged in, but does not have a tenant cookie, set it
setSelectedTenantID(initialValue) setSelectedTenantID(initialValue)
setCookie(String(initialValue)) if (initialValue) {
setCookie(String(initialValue))
} else {
deleteCookie()
}
} }
}, [userID, tenantCookie, initialValue, setCookie, router]) }, [userID, tenantCookie, initialValue, setCookie, deleteCookie, router])
React.useEffect(() => { React.useEffect(() => {
if (!userID && tenantCookie) { if (!userID && tenantCookie) {
@@ -131,7 +129,7 @@ export const TenantSelectionProviderClient = ({
data-selected-tenant-id={selectedTenantID} data-selected-tenant-id={selectedTenantID}
data-selected-tenant-title={selectedTenantLabel} data-selected-tenant-title={selectedTenantLabel}
> >
<Context.Provider <Context
value={{ value={{
options: tenantOptions, options: tenantOptions,
selectedTenantID, selectedTenantID,
@@ -140,9 +138,9 @@ export const TenantSelectionProviderClient = ({
}} }}
> >
{children} {children}
</Context.Provider> </Context>
</span> </span>
) )
} }
export const useTenantSelection = () => React.useContext(Context) export const useTenantSelection = () => React.use(Context)

View File

@@ -2,7 +2,6 @@ import type { OptionObject, Payload, User } from 'payload'
import { cookies as getCookies } from 'next/headers.js' import { cookies as getCookies } from 'next/headers.js'
import { SELECT_ALL } from '../../constants.js'
import { findTenantOptions } from '../../queries/findTenantOptions.js' import { findTenantOptions } from '../../queries/findTenantOptions.js'
import { TenantSelectionProviderClient } from './index.client.js' import { TenantSelectionProviderClient } from './index.client.js'
@@ -43,18 +42,24 @@ export const TenantSelectionProvider = async ({
let tenantCookie = cookies.get('payload-tenant')?.value let tenantCookie = cookies.get('payload-tenant')?.value
let initialValue = undefined let initialValue = undefined
if (tenantOptions.length > 1 && tenantCookie === SELECT_ALL) { /**
initialValue = SELECT_ALL * Ensure the cookie is a valid tenant
} else { */
if (tenantCookie) {
const matchingOption = tenantOptions.find((option) => String(option.value) === tenantCookie) const matchingOption = tenantOptions.find((option) => String(option.value) === tenantCookie)
if (matchingOption) { if (matchingOption) {
initialValue = matchingOption.value initialValue = matchingOption.value
} else {
tenantCookie = undefined
initialValue = tenantOptions.length > 1 ? SELECT_ALL : tenantOptions[0]?.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 ( return (
<TenantSelectionProviderClient <TenantSelectionProviderClient
initialValue={initialValue} initialValue={initialValue}

View File

@@ -1,6 +1,5 @@
import type { Payload, User, ViewTypes } from 'payload' import type { Payload, User, ViewTypes } from 'payload'
import { SELECT_ALL } from '../constants.js'
import { findTenantOptions } from '../queries/findTenantOptions.js' import { findTenantOptions } from '../queries/findTenantOptions.js'
import { getCollectionIDType } from './getCollectionIDType.js' import { getCollectionIDType } from './getCollectionIDType.js'
import { getTenantFromCookie } from './getTenantFromCookie.js' import { getTenantFromCookie } from './getTenantFromCookie.js'
@@ -34,7 +33,7 @@ export async function getGlobalViewRedirect({
let tenant = getTenantFromCookie(headers, idType) let tenant = getTenantFromCookie(headers, idType)
let redirectRoute let redirectRoute
if (!tenant || tenant === SELECT_ALL) { if (!tenant) {
const tenantsQuery = await findTenantOptions({ const tenantsQuery = await findTenantOptions({
limit: 1, limit: 1,
payload, payload,

View File

@@ -31,7 +31,7 @@
} }
], ],
"paths": { "paths": {
"@payload-config": ["./test/fields/config.ts"], "@payload-config": ["./test/plugin-multi-tenant/config.ts"],
"@payloadcms/admin-bar": ["./packages/admin-bar/src"], "@payloadcms/admin-bar": ["./packages/admin-bar/src"],
"@payloadcms/live-preview": ["./packages/live-preview/src"], "@payloadcms/live-preview": ["./packages/live-preview/src"],
"@payloadcms/live-preview-react": ["./packages/live-preview-react/src/index.ts"], "@payloadcms/live-preview-react": ["./packages/live-preview-react/src/index.ts"],