Skip to content

fix(plugin-multi-tenant): updates tenant selector upon tenant creation #12936

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,36 @@ import { useTenantSelection } from '../../providers/TenantSelectionProvider/inde
export const WatchTenantCollection = () => {
const { id, collectionSlug } = useDocumentInfo()
const { title } = useDocumentTitle()
const addedNewTenant = React.useRef(false)

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

const { updateTenants } = useTenantSelection()
const { options, updateTenants } = useTenantSelection()

const syncTenantTitle = useEffectEvent(() => {
if (id) {
updateTenants({ id, label: title })
}
})

React.useEffect(() => {
if (!id || !title || addedNewTenant.current) {
return
}
// Track tenant creation and add it to the tenant selector
const exists = options.some((opt) => opt.value === id)
if (!exists) {
addedNewTenant.current = true
updateTenants({ id, label: title })
}
// eslint-disable-next-line react-compiler/react-compiler
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [id])

React.useEffect(() => {
// only update the tenant selector when the document saves
// → aka when initial value changes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,29 @@ export const TenantSelectionProviderClient = ({

const updateTenants = React.useCallback<ContextType['updateTenants']>(({ id, label }) => {
setTenantOptions((prev) => {
return prev.map((currentTenant) => {
if (id === currentTenant.value) {
const stringID = String(id)
let exists = false
const updated = prev.map((currentTenant) => {
if (stringID === String(currentTenant.value)) {
exists = true
return {
label,
value: id,
value: stringID,
}
}
return currentTenant
})

if (!exists) {
updated.push({ label, value: stringID })
}

// Sort alphabetically by label (or value as fallback)
return updated.sort((a, b) => {
const aKey = typeof a.label === 'string' ? a.label : String(a.value)
const bKey = typeof b.label === 'string' ? b.label : String(b.value)
return aKey.localeCompare(bKey)
})
})
}, [])

Expand Down
Loading