Skip to content

Commit c76d839

Browse files
authored
fix(plugin-multi-tenant): updates tenant selector upon tenant creation (#12936)
### What? Updates the tenant selector displayed in the sidebar when a new tenant is created. ### Why? Currently when using the multi-tenant plugin and creating a new tenant doc, the tenant selector dropdown does not display the new tenant as an option until the page gets refreshed. ### How? Extends the `WatchTenantCollection` helper to check if the tenant `id` from the current doc exists, if the tenant is new it manually calls `updateTenants`. The `updateTenants` function previously only adjusted the title on existing tenants, this has been updated to add a new tenant as an option when it doesn't exist. #### Reported by client
1 parent a1822d2 commit c76d839

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

packages/plugin-multi-tenant/src/components/WatchTenantCollection/index.tsx

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,36 @@ import { useTenantSelection } from '../../providers/TenantSelectionProvider/inde
1616
export const WatchTenantCollection = () => {
1717
const { id, collectionSlug } = useDocumentInfo()
1818
const { title } = useDocumentTitle()
19+
const addedNewTenant = React.useRef(false)
1920

2021
const { getEntityConfig } = useConfig()
2122
const [useAsTitleName] = React.useState(
2223
() => (getEntityConfig({ collectionSlug }) as ClientCollectionConfig).admin.useAsTitle,
2324
)
2425
const titleField = useFormFields(([fields]) => (useAsTitleName ? fields[useAsTitleName] : {}))
2526

26-
const { updateTenants } = useTenantSelection()
27+
const { options, updateTenants } = useTenantSelection()
2728

2829
const syncTenantTitle = useEffectEvent(() => {
2930
if (id) {
3031
updateTenants({ id, label: title })
3132
}
3233
})
3334

35+
React.useEffect(() => {
36+
if (!id || !title || addedNewTenant.current) {
37+
return
38+
}
39+
// Track tenant creation and add it to the tenant selector
40+
const exists = options.some((opt) => opt.value === id)
41+
if (!exists) {
42+
addedNewTenant.current = true
43+
updateTenants({ id, label: title })
44+
}
45+
// eslint-disable-next-line react-compiler/react-compiler
46+
// eslint-disable-next-line react-hooks/exhaustive-deps
47+
}, [id])
48+
3449
React.useEffect(() => {
3550
// only update the tenant selector when the document saves
3651
// → aka when initial value changes

packages/plugin-multi-tenant/src/providers/TenantSelectionProvider/index.client.tsx

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,15 +104,29 @@ export const TenantSelectionProviderClient = ({
104104

105105
const updateTenants = React.useCallback<ContextType['updateTenants']>(({ id, label }) => {
106106
setTenantOptions((prev) => {
107-
return prev.map((currentTenant) => {
108-
if (id === currentTenant.value) {
107+
const stringID = String(id)
108+
let exists = false
109+
const updated = prev.map((currentTenant) => {
110+
if (stringID === String(currentTenant.value)) {
111+
exists = true
109112
return {
110113
label,
111-
value: id,
114+
value: stringID,
112115
}
113116
}
114117
return currentTenant
115118
})
119+
120+
if (!exists) {
121+
updated.push({ label, value: stringID })
122+
}
123+
124+
// Sort alphabetically by label (or value as fallback)
125+
return updated.sort((a, b) => {
126+
const aKey = typeof a.label === 'string' ? a.label : String(a.value)
127+
const bKey = typeof b.label === 'string' ? b.label : String(b.value)
128+
return aKey.localeCompare(bKey)
129+
})
116130
})
117131
}, [])
118132

0 commit comments

Comments
 (0)