Skip to content

Commit 8481b4a

Browse files
feat(mosaic): Configure the profile on MosaicProvider (#10546)
1 parent aa23ee5 commit 8481b4a

37 files changed

Lines changed: 1369 additions & 365 deletions
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
'@clerk/mosaic': minor
3+
---
4+
5+
Configure the profile on `MosaicProvider` with the new `profile` prop, so `UserButton`, `OrganizationSwitcher` and `UserProfile` share one setup. This makes pairings like a user-only `UserButton` beside an `OrganizationSwitcher` with one combined profile possible.
6+
7+
```tsx
8+
<MosaicProvider profile={{ combined: true, userProfileProps, organizationProfileProps }}>
9+
<OrganizationSwitcher />
10+
<UserButton combined={false} />
11+
</MosaicProvider>
12+
```
13+
14+
Breaking changes:
15+
16+
- `UserButton`: `mode` is replaced by `combined`, which only decides what the menu lists. `mode='user'` becomes `combined={false}`, and to keep the account-only profile too, also set `profile={{ combined: false }}` on `MosaicProvider`.
17+
- `UserButton` and `OrganizationSwitcher`: `userProfileProps` and `organizationProfileProps` move to `MosaicProvider`'s `profile`.
18+
- `UserButton` and `OrganizationSwitcher`: `userProfileUrl` and `organizationProfileUrl` move to `MosaicProvider`'s `profile`.
19+
- `UserButton` and `OrganizationSwitcher`: `userProfileMode` and `organizationProfileMode` are removed. Setting a URL routes there, and leaving it out opens the modal.
20+
- `UserButton` and `OrganizationSwitcher`: `appearance` is no longer accepted in `userProfileProps` or `organizationProfileProps`.
21+
- `UserButton` and `OrganizationSwitcher`: link items (`href`) are no longer accepted in `customPages`.
22+
- `UserButton` and `OrganizationSwitcher`: `afterLeaveOrganizationUrl` moves to `profile.organizationProfileProps`, which also takes `apiKeysProps`.
23+
- `UserProfile`: `mode` is replaced by `combined`. When unset it follows `profile.combined`, and it also picks up the provider's profile props, with its own props winning. `OrganizationProfile` picks them up the same way.
24+
- With `experimental.newProfiles`, `OrganizationSwitcher` now opens the combined profile by default. Set `profile={{ combined: false }}` for the standalone organization profile.
25+
- The `UserProfileMode` type is removed.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@clerk/mosaic': patch
3+
---
4+
5+
Fix the organization profile's page order when `OrganizationSwitcher` or `UserButton` opens the legacy profile for a member who can manage the SSO bypass allowlist. The Security page now stays where `pageOrder` puts it instead of moving to the top.

‎integration/templates/next-app-router-mosaic/src/app/custom/page.tsx‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ import { MosaicProvider, UserButton } from '@clerk/mosaic';
44

55
export default function Page() {
66
return (
7-
<MosaicProvider experimental={{ newProfiles: true }}>
8-
<UserButton
9-
customMenuItems={[{ id: 'custom-link', label: 'Custom link', href: '/custom/link-target' }]}
10-
userProfileProps={{
7+
<MosaicProvider
8+
experimental={{ newProfiles: true }}
9+
profile={{
10+
userProfileProps: {
1111
customPages: [{ label: 'Custom page', path: 'custom-page', content: <p>custom-page-content</p> }],
12-
}}
13-
/>
12+
},
13+
}}
14+
>
15+
<UserButton customMenuItems={[{ id: 'custom-link', label: 'Custom link', href: '/custom/link-target' }]} />
1416
</MosaicProvider>
1517
);
1618
}

‎integration/templates/next-app-router-mosaic/src/app/organization-switcher/page.tsx‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,16 @@ import { MosaicProvider, OrganizationSwitcher } from '@clerk/mosaic';
44

55
export default function Page() {
66
return (
7-
<MosaicProvider experimental={{ newProfiles: true }}>
7+
<MosaicProvider
8+
experimental={{ newProfiles: true }}
9+
profile={{
10+
organizationProfileProps: {
11+
customPages: [{ label: 'Custom page', path: 'custom-page', content: <p>custom-page-content</p> }],
12+
},
13+
}}
14+
>
815
<OrganizationSwitcher
916
customMenuItems={[{ id: 'custom-link', label: 'Custom link', href: '/custom/link-target' }]}
10-
organizationProfileProps={{
11-
customPages: [{ label: 'Custom page', path: 'custom-page', content: <p>custom-page-content</p> }],
12-
}}
1317
/>
1418
</MosaicProvider>
1519
);

‎packages/mosaic/docs/models.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export type SwitcherModel =
3838
```
3939

4040
- **Cache updates.** Update the cache from inside the callback that mutated. On success patch the returned resource in with `replaceInPages`; on failure revalidate with `Promise.allSettled`, since a failed mutation may mean stale state and a failed revalidate must not look like a failed mutation (`onAcceptSuggestion` in `switcher.model.tsx`).
41-
- **Navigation vs modal.** A consumer's URL is the whole opt-in. Type the pair so it cannot contradict itself: `{ userProfileUrl: string; userProfileMode?: 'navigation' } | { userProfileUrl?: never; userProfileMode?: 'modal' }`.
41+
- **Navigation vs modal.** A consumer's URL is the whole opt-in. Type the pair so it cannot contradict itself: `{ createOrganizationUrl: string; createOrganizationMode?: 'navigation' } | { createOrganizationUrl?: never; createOrganizationMode?: 'modal' }` (`CreateOrganizationMode` in `switcher.model.tsx`).
4242

4343
## Errors
4444

‎packages/mosaic/src/__tests__/feature/render.tsx‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { act, render } from '@testing-library/react';
44
import type { ReactElement } from 'react';
55
import { vi } from 'vitest';
66

7+
import type { MosaicProfile } from '../../features/profile-pages/use-mosaic-profile';
78
import type { MosaicExperimental } from '../../hooks/use-mosaic-experimental';
89
import type { MosaicLocalization } from '../../localization';
910
import { MosaicProvider } from '../../mosaic-provider';
@@ -14,6 +15,7 @@ export async function renderWithClerk(
1415
options?: Parameters<Clerk['load']>[0],
1516
localization?: MosaicLocalization,
1617
experimental?: MosaicExperimental,
18+
profile?: MosaicProfile,
1719
) {
1820
const clerk = new Clerk(publishableKey());
1921
const navigate = vi.fn((_to: string) => Promise.resolve());
@@ -26,6 +28,7 @@ export async function renderWithClerk(
2628
<MosaicProvider
2729
localization={localization}
2830
experimental={experimental}
31+
profile={profile}
2932
>
3033
{element}
3134
</MosaicProvider>

‎packages/mosaic/src/features/organization-profile/organization-profile.tsx‎

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type { ReactNode } from 'react';
44

55
import type { APIKeysPageProps } from '../api-keys/api-keys-table';
66
import { useProfilePagesController } from '../profile-pages/profile-pages.controller';
7+
import { useMosaicProfile } from '../profile-pages/use-mosaic-profile';
78
import { organizationProfilePageIds } from './organization-profile.layout';
89
import { useOrganizationProfileModel } from './organization-profile.model';
910
import type { OrganizationProfileOptions } from './organization-profile.provider';
@@ -79,14 +80,16 @@ export interface OrganizationProfileProps
7980
* <OrganizationProfile afterLeaveOrganizationUrl='/organizations' />
8081
* ```
8182
*/
82-
export function OrganizationProfile({
83-
customPages,
84-
pageOrder,
85-
defaultPage,
86-
afterLeaveOrganizationUrl,
87-
fallback,
88-
apiKeysProps,
89-
}: OrganizationProfileProps) {
83+
export function OrganizationProfile(props: OrganizationProfileProps) {
84+
const { organizationProfileProps } = useMosaicProfile();
85+
const {
86+
customPages = organizationProfileProps?.customPages,
87+
pageOrder = organizationProfileProps?.pageOrder,
88+
defaultPage,
89+
afterLeaveOrganizationUrl = organizationProfileProps?.afterLeaveOrganizationUrl,
90+
fallback,
91+
apiKeysProps = organizationProfileProps?.apiKeysProps,
92+
} = props;
9093
const model = useOrganizationProfileModel();
9194
const pages = model.status === 'ready' ? organizationProfilePanels(model.pages, apiKeysProps) : undefined;
9295
const navigation = useProfilePagesController(

‎packages/mosaic/src/features/organization-switcher/__tests__/organization-switcher.feature.test.tsx‎

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
fapiUser,
1818
} from '../../../__tests__/feature/fapi';
1919
import { renderWithClerk } from '../../../__tests__/feature/render';
20+
import type { MosaicProfile } from '../../profile-pages/use-mosaic-profile';
2021
import type { OrganizationSwitcherProps } from '../organization-switcher';
2122
import { OrganizationSwitcher } from '../organization-switcher';
2223

@@ -71,9 +72,13 @@ function tree(props: OrganizationSwitcherProps = {}) {
7172
);
7273
}
7374

74-
function renderSwitcher(props: OrganizationSwitcherProps = {}, seed: FakeFapiSeed = signedIn()) {
75+
function renderSwitcher(
76+
props: OrganizationSwitcherProps = {},
77+
seed: FakeFapiSeed = signedIn(),
78+
profile?: MosaicProfile,
79+
) {
7580
const fapi = serveFapi(seed);
76-
return renderWithClerk(tree(props)).then(view => ({ ...view, fapi }));
81+
return renderWithClerk(tree(props), undefined, undefined, undefined, profile).then(view => ({ ...view, fapi }));
7782
}
7883

7984
const host = () => screen.getByTestId('host');
@@ -222,7 +227,9 @@ describe('OrganizationSwitcher', () => {
222227
});
223228

224229
it('tells the organization profile where to land after leaving', async () => {
225-
const { clerk } = await renderSwitcher({ afterLeaveOrganizationUrl: '/left' }, noOffers());
230+
const { clerk } = await renderSwitcher({}, noOffers(), {
231+
organizationProfileProps: { afterLeaveOrganizationUrl: '/left' },
232+
});
226233
const openOrganizationProfile = vi.spyOn(clerk, 'openOrganizationProfile').mockImplementation(() => {});
227234
const user = await openWithList();
228235

@@ -298,6 +305,38 @@ describe('OrganizationSwitcher', () => {
298305
expect(reading('Settings', 'Terms of service', 'Support')).toEqual(['Support', 'Terms of service', 'Settings']);
299306
});
300307

308+
describe('the organization profile page order', () => {
309+
const termsPage = { label: 'Terms', path: 'terms', icon: <span />, content: <p>Terms body</p> };
310+
311+
async function openedPageLabels(permissions: string[]) {
312+
const memberships = [fapiMembership(acme, { permissions }), fapiMembership(other)];
313+
const user = fapiUser({ ...alice, organization_memberships: memberships });
314+
const session = fapiSession({ id: 'sess_1', user, last_active_organization_id: 'org_1' });
315+
const { clerk } = await renderSwitcher({}, noOffers({ client: fapiClient([session]), memberships }), {
316+
organizationProfileProps: { customPages: [termsPage], pageOrder: ['terms', 'security'] },
317+
});
318+
const openOrganizationProfile = vi.spyOn(clerk, 'openOrganizationProfile').mockImplementation(() => {});
319+
const clicker = await openWithList();
320+
321+
await clicker.click(screen.getByRole('button', { name: 'Settings' }));
322+
323+
return openOrganizationProfile.mock.calls[0]?.[0]?.customPages?.map(page => page.label);
324+
}
325+
326+
it('keeps Security where it was asked for when the member can manage the SSO bypass allowlist', async () => {
327+
expect(await openedPageLabels(['org:sys_entconns_sso_bypass:manage'])).toEqual([
328+
'Terms',
329+
'security',
330+
'general',
331+
'members',
332+
]);
333+
});
334+
335+
it('leaves Security out when the organization profile will not show it', async () => {
336+
expect(await openedPageLabels([])).toEqual(['Terms', 'general', 'members']);
337+
});
338+
});
339+
301340
it('themes its parts through organization-switcher slots', async () => {
302341
await renderSwitcher({}, noOffers());
303342
await openWithList();
@@ -378,11 +417,11 @@ describe('OrganizationSwitcher custom trigger badges', () => {
378417
});
379418

380419
describe('OrganizationSwitcher with experimental.newProfiles', () => {
381-
async function renderWithNewProfiles(props: OrganizationSwitcherProps = {}) {
420+
async function renderWithNewProfiles(profile: MosaicProfile = { combined: false }) {
382421
await page.viewport(1280, 800);
383422
onTestFinished(() => page.viewport(414, 896));
384423
serveFapi(noOffers());
385-
return renderWithClerk(tree(props), undefined, undefined, { newProfiles: true });
424+
return renderWithClerk(tree(), undefined, undefined, { newProfiles: true }, profile);
386425
}
387426

388427
it('opens the OrganizationProfile, without the account pages, from Settings', async () => {
@@ -397,8 +436,14 @@ describe('OrganizationSwitcher with experimental.newProfiles', () => {
397436
expect(openOrganizationProfile).not.toHaveBeenCalled();
398437
});
399438

400-
it('lands on afterLeaveOrganizationUrl after leaving from the opened profile', async () => {
401-
const { navigate } = await renderWithNewProfiles({ afterLeaveOrganizationUrl: '/left' });
439+
it.each([
440+
{ name: 'separate', combined: false },
441+
{ name: 'combined', combined: true },
442+
])('lands on afterLeaveOrganizationUrl after leaving from the opened profile: $name', async ({ combined }) => {
443+
const { navigate } = await renderWithNewProfiles({
444+
combined,
445+
organizationProfileProps: { afterLeaveOrganizationUrl: '/left' },
446+
});
402447
const user = await openWithList();
403448

404449
await user.click(screen.getByRole('button', { name: 'Settings' }));

‎packages/mosaic/src/features/organization-switcher/organization-switcher.tsx‎

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ import type { OrganizationSwitcherModelOptions } from '../switcher/switcher.mode
88
import type { SwitcherMenuProps } from '../switcher/switcher.types';
99
import type { SwitcherTriggerProps } from '../switcher/switcher.view';
1010

11-
/** Everything `<OrganizationSwitcher />` takes: profile routing, trigger content, the app's own menu rows, and the profile it opens. */
11+
/** Everything `<OrganizationSwitcher />` takes: trigger content, and the app's own menu rows. */
1212
export type OrganizationSwitcherProps = OrganizationSwitcherModelOptions &
1313
SwitcherTriggerProps &
1414
SwitcherMenuProps &
15-
Pick<SwitcherProps, 'organizationProfileProps' | 'fallback'>;
15+
Pick<SwitcherProps, 'fallback'>;
1616

1717
/**
1818
* The active organization's avatar, and the menu behind it: switch organization or back to the
@@ -35,26 +35,22 @@ export type OrganizationSwitcherProps = OrganizationSwitcherModelOptions &
3535
* ```
3636
*
3737
* @example
38-
* Passing a URL routes to a page of your own instead of opening Clerk's modal. `afterSelectOrganizationUrl`
39-
* is where switching organization lands, and takes a `:param` template, a plain path, or a function.
38+
* `createOrganizationUrl` routes to a page of your own instead of opening Clerk's modal. `afterSelectOrganizationUrl`
39+
* is where switching organization lands, and takes a `:param` template, a plain path, or a function. Set
40+
* `organizationProfileUrl` on `MosaicProvider`'s `profile` to route Settings to a page of your own.
4041
* ```tsx
4142
* <OrganizationSwitcher
42-
* organizationProfileUrl='/settings/organization'
4343
* createOrganizationUrl='/orgs/new'
4444
* afterSelectOrganizationUrl='/orgs/:slug'
4545
* afterSelectPersonalUrl='/dashboard'
4646
* />
4747
* ```
4848
*
4949
* @example
50-
* `customPages` adds your own pages to the organization profile; `customMenuItems` adds your own rows
51-
* beside Settings, each one either an `onClick` action or an `href` link.
50+
* `customMenuItems` adds your own rows beside Settings, each one either an `onClick` action or an
51+
* `href` link. The profile's own pages are set on `MosaicProvider`'s `profile`.
5252
* ```tsx
5353
* <OrganizationSwitcher
54-
* organizationProfileProps={{
55-
* customPages: [{ path: 'audit', label: 'Audit log', icon: <ListIcon />, content: <AuditPage /> }],
56-
* pageOrder: ['general', 'members', 'audit'],
57-
* }}
5854
* customMenuItems={[{ id: 'docs', label: 'Documentation', icon: <BookIcon />, href: 'https://example.com/docs' }]}
5955
* />
6056
* ```

‎packages/mosaic/src/features/profile-pages/profile-pages.controller.ts‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
import { useState } from 'react';
22

3-
export function useProfilePagesController(initialPage: string, pageIds: readonly string[] | undefined) {
3+
export function useProfilePagesController(
4+
initialPage: string,
5+
pageIds: readonly string[] | undefined,
6+
openingFallbackPage?: string,
7+
) {
48
const [activePage, setActivePage] = useState(initialPage);
9+
const [isOpening, setIsOpening] = useState(true);
510

611
if (pageIds) {
7-
const [firstPage] = pageIds;
8-
if (firstPage !== undefined && !pageIds.includes(activePage)) {
9-
setActivePage(firstPage);
12+
if (isOpening) {
13+
setIsOpening(false);
14+
}
15+
const fallbackPage = (isOpening ? openingFallbackPage : undefined) ?? pageIds[0];
16+
if (fallbackPage !== undefined && !pageIds.includes(activePage)) {
17+
setActivePage(fallbackPage);
1018
}
1119
}
1220

0 commit comments

Comments
 (0)