Skip to content

Commit f477e0e

Browse files
feat(plugin-multi-tenant): export useTenantSelection hook for public usage (#11364)
Exports the `useTenantSelection` hook from the multi-tenant plugin, this way other users can import and use the hook along with it's methods. Can be imported: ```ts import { useTenantSelection } from '@payloadcms/plugin-multi-tenant/client' ``` The context returned: ```ts type ContextType = { /** * Array of options to select from */ options: OptionObject[] /** * The currently selected tenant ID */ selectedTenantID: number | string | undefined /** * Prevents a refresh when the tenant is changed * * If not switching tenants while viewing a "global", set to true */ setPreventRefreshOnChange: React.Dispatch<React.SetStateAction<boolean>> /** * Sets the selected tenant ID * * @param args.id - The ID of the tenant to select * @param args.refresh - Whether to refresh the page after changing the tenant */ setTenant: (args: { id: number | string | undefined; refresh?: boolean }) => void } ```
1 parent 4224c68 commit f477e0e

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

docs/plugins/multi-tenant.mdx

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,50 @@ async rewrites() {
278278
}
279279
```
280280

281+
### React Hooks
282+
283+
Below are the hooks exported from the plugin that you can import into your own custom components to consume.
284+
285+
#### useTenantSelection
286+
287+
You can import this like so:
288+
289+
```tsx
290+
import { useTenantSelection } from '@payloadcms/plugin-multi-tenant/client'
291+
292+
...
293+
294+
const tenantContext = useTenantSelection()
295+
```
296+
297+
The hook returns the following context:
298+
299+
```ts
300+
type ContextType = {
301+
/**
302+
* Array of options to select from
303+
*/
304+
options: OptionObject[]
305+
/**
306+
* The currently selected tenant ID
307+
*/
308+
selectedTenantID: number | string | undefined
309+
/**
310+
* Prevents a refresh when the tenant is changed
311+
*
312+
* If not switching tenants while viewing a "global", set to true
313+
*/
314+
setPreventRefreshOnChange: React.Dispatch<React.SetStateAction<boolean>>
315+
/**
316+
* Sets the selected tenant ID
317+
*
318+
* @param args.id - The ID of the tenant to select
319+
* @param args.refresh - Whether to refresh the page after changing the tenant
320+
*/
321+
setTenant: (args: { id: number | string | undefined; refresh?: boolean }) => void
322+
}
323+
```
324+
281325
282326
## Examples
283327
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export { TenantField } from '../components/TenantField/index.client.js'
22
export { TenantSelector } from '../components/TenantSelector/index.js'
3+
export { useTenantSelection } from '../providers/TenantSelectionProvider/index.client.js'

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,26 @@ import React, { createContext } from 'react'
99
import { SELECT_ALL } from '../../constants.js'
1010

1111
type ContextType = {
12+
/**
13+
* Array of options to select from
14+
*/
1215
options: OptionObject[]
16+
/**
17+
* The currently selected tenant ID
18+
*/
1319
selectedTenantID: number | string | undefined
20+
/**
21+
* Prevents a refresh when the tenant is changed
22+
*
23+
* If not switching tenants while viewing a "global", set to true
24+
*/
1425
setPreventRefreshOnChange: React.Dispatch<React.SetStateAction<boolean>>
26+
/**
27+
* Sets the selected tenant ID
28+
*
29+
* @param args.id - The ID of the tenant to select
30+
* @param args.refresh - Whether to refresh the page after changing the tenant
31+
*/
1532
setTenant: (args: { id: number | string | undefined; refresh?: boolean }) => void
1633
}
1734

0 commit comments

Comments
 (0)