Skip to content

Commit 6ed3ecc

Browse files
committed
feat(mosaic): open the combined profile as its own modal
The combined profile opens at /modal/profile and the account-only profile at /modal/user, so a routing-opened modal shows the profile its route names.
1 parent 9facc2e commit 6ed3ecc

12 files changed

Lines changed: 164 additions & 45 deletions

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ import { createContext, useContext } from 'react';
22

33
import type { OrganizationProfileProps } from '../organization-profile/organization-profile';
44
import type { OrganizationProfilePageId } from '../organization-profile/organization-profile.types';
5+
import type { ProfileModalOptions } from '../profile/profile.modal.types';
56
import type { UserProfileProps } from '../user-profile/user-profile';
7+
import type { UserProfileModalOptions } from '../user-profile/user-profile.modal.types';
68
import type { CustomProfilePage, UserProfilePageId } from '../user-profile/user-profile.types';
79

810
export interface MosaicProfile {
@@ -69,10 +71,14 @@ export function useMosaicProfile(): MosaicProfile {
6971
}
7072

7173
/** What the user profile modal opens with, whichever path opens it. */
72-
export function userProfileModalOptions({
73-
combined = true,
74+
export function userProfileModalOptions({ userProfileProps }: MosaicProfile): UserProfileModalOptions {
75+
return { ...userProfileProps };
76+
}
77+
78+
/** What the combined profile modal opens with, whichever path opens it. */
79+
export function profileModalOptions({
7480
userProfileProps,
7581
organizationProfileProps,
76-
}: MosaicProfile) {
77-
return { ...userProfileProps, combined, organizationProfileProps };
82+
}: MosaicProfile): ProfileModalOptions {
83+
return { ...userProfileProps, organizationProfileProps };
7884
}

‎packages/mosaic/src/features/profile/__tests__/profile.feature.test.tsx‎

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,17 @@ import {
1414
fapiUser,
1515
} from '../../../__tests__/feature/fapi';
1616
import { renderWithClerk } from '../../../__tests__/feature/render';
17+
import { useModal } from '../../../modals/use-modal';
1718
import { Profile } from '../profile';
19+
import { profileModal } from '../profile.modal';
1820

1921
beforeAll(() => page.viewport(1280, 800));
2022

23+
function OpenProfile({ page }: { page: string }) {
24+
const profile = useModal(profileModal);
25+
return <button onClick={() => profile.open({ page })}>Open profile</button>;
26+
}
27+
2128
function tabNames() {
2229
return screen.getAllByRole('tab').map(tab => tab.textContent);
2330
}
@@ -112,6 +119,20 @@ describe('Profile with an active organization', () => {
112119
expect(screen.getByRole('heading', { level: 3, name: 'Members' })).toBeVisible();
113120
});
114121

122+
it('opens as a modal on the first organization page when the requested organization page is unavailable', async () => {
123+
serveWithOrganization({ permissions: [] });
124+
await renderWithClerk(<OpenProfile page='organization/members' />);
125+
const user = userEvent.setup();
126+
127+
await user.click(screen.getByRole('button', { name: 'Open profile' }));
128+
const dialog = await screen.findByRole('dialog');
129+
130+
expect(await within(dialog).findByRole('tab', { name: 'General', description: 'Organization' })).toHaveAttribute(
131+
'aria-selected',
132+
'true',
133+
);
134+
});
135+
115136
it('opens the organization page that shares an account page name', async () => {
116137
serveWithOrganization();
117138
await renderWithClerk(
@@ -130,9 +151,7 @@ describe('Profile with an active organization', () => {
130151
it('skips an account page whose path is an organization page id', async () => {
131152
serveWithOrganization();
132153
await renderWithClerk(
133-
<Profile
134-
customPages={[{ path: 'organization/general', label: 'Imposter', content: <p>Imposter content</p> }]}
135-
/>,
154+
<Profile customPages={[{ path: 'organization/general', label: 'Imposter', content: <p>Imposter content</p> }]} />,
136155
);
137156

138157
expect(tabNames()).toEqual(['Account', 'Security', 'General', 'Members']);
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { useUser } from '@clerk/shared/react';
2+
import React from 'react';
3+
4+
import { profileModalOptions, useMosaicProfile } from '../profile-pages/use-mosaic-profile';
5+
import { UserProfileSurface } from './profile';
6+
import type { ProfileModalContentProps, ProfileModalOptions } from './profile.modal.types';
7+
8+
export default function ProfileModalContent({ options, close, path }: ProfileModalContentProps): React.ReactElement {
9+
const profile = useMosaicProfile();
10+
return (
11+
<ModalProfile
12+
options={options ?? profileModalOptions(profile)}
13+
close={close}
14+
path={path}
15+
combined
16+
/>
17+
);
18+
}
19+
20+
export function ModalProfile({
21+
options: { page: _page, ...props },
22+
close,
23+
path,
24+
combined,
25+
}: {
26+
options: ProfileModalOptions;
27+
close: () => void;
28+
path: string;
29+
combined: boolean;
30+
}): React.ReactElement {
31+
const { isLoaded, user } = useUser();
32+
const signedOut = isLoaded && !user;
33+
34+
React.useEffect(() => {
35+
if (signedOut) {
36+
close();
37+
}
38+
}, [signedOut, close]);
39+
40+
return (
41+
<UserProfileSurface
42+
{...props}
43+
combined={combined}
44+
presentation='modal'
45+
path={path}
46+
/>
47+
);
48+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { defineModalSurface } from '../../modals/define-modal-surface';
2+
import { pageParts } from '../profile-pages/profile-pages.layout';
3+
import type { ProfileModalOptions } from './profile.modal.types';
4+
5+
export const profileModal = defineModalSurface<ProfileModalOptions>({
6+
id: 'profile',
7+
variant: 'profile',
8+
load: () => import('./profile.modal-content'),
9+
path: '/modal/profile',
10+
initialParts: options => pageParts(options?.page),
11+
});
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import type { ModalContentProps } from '../../modals/modal.types';
2+
import type { UserProfileModalOptions } from '../user-profile/user-profile.modal.types';
3+
import type { CombinedProfileProps } from './profile';
4+
5+
export interface ProfileModalOptions
6+
extends UserProfileModalOptions, Pick<CombinedProfileProps, 'organizationProfileProps'> {}
7+
8+
export type ProfileModalContentProps = ModalContentProps<ProfileModalOptions>;

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,10 @@ export function Profile(props: ProfileProps): ReactNode {
125125

126126
function withUserDefaults(
127127
props: UserProfileProps,
128-
defaults: Pick<UserProfileProps, 'customPages' | 'pageOrder' | 'additionalOAuthScopes' | 'apiKeysProps' | 'path'> = {},
128+
defaults: Pick<
129+
UserProfileProps,
130+
'customPages' | 'pageOrder' | 'additionalOAuthScopes' | 'apiKeysProps' | 'path'
131+
> = {},
129132
): UserProfileProps {
130133
const {
131134
customPages = defaults.customPages,

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

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -341,14 +341,41 @@ function OpenByRouting({ base }: { base: string }) {
341341
return <button onClick={() => write(base, [])}>Open by routing</button>;
342342
}
343343

344-
describe('a user profile modal opened by the routing state', () => {
345-
it("renders with the provider's options", async () => {
346-
await renderWithProfile(<OpenByRouting base='/modal/user' />, { userProfileProps: { customPages: [terms] } });
344+
describe('a profile modal opened by the routing state', () => {
345+
it.each([
346+
{ name: 'the account pages at /modal/user', base: '/modal/user', expected: [...accountTabs, 'Terms'] },
347+
{
348+
name: 'both groups at /modal/profile',
349+
base: '/modal/profile',
350+
expected: [...accountTabs, 'Terms', ...organizationTabs],
351+
},
352+
])("renders $name with the provider's options", async ({ base, expected }) => {
353+
await renderWithProfile(<OpenByRouting base={base} />, { userProfileProps: { customPages: [terms] } });
347354

348355
await userEvent.setup().click(screen.getByRole('button', { name: 'Open by routing' }));
349356
const dialog = await screen.findByRole('dialog');
350357

351-
expect(await within(dialog).findByRole('tab', { name: 'Terms' })).toBeInTheDocument();
358+
await within(dialog).findByRole('tab', { name: 'Terms' });
359+
expect(tabs()).toEqual(expected);
360+
});
361+
});
362+
363+
describe('the route of the profile a switcher opens', () => {
364+
it.each([
365+
{ name: 'a user-only button opens the account profile', ui: <UserButton />, base: '/modal/user' },
366+
{ name: 'a combined button opens the combined profile', ui: <Switcher />, base: '/modal/profile' },
367+
])('$name at $base', async ({ ui, base }) => {
368+
await renderWithProfile(
369+
<>
370+
{ui}
371+
<Entry base={base} />
372+
</>,
373+
);
374+
375+
await clickSettings(buttonTrigger);
376+
377+
await selectedTab('Account');
378+
expect(JSON.parse(screen.getByTestId('entry').textContent ?? 'null')).toEqual([]);
352379
});
353380
});
354381

‎packages/mosaic/src/features/switcher/switcher.model.tsx‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ import { useGetPortalContainer } from '../../primitives/portal';
1818
import { replaceInPages, shrinkTotal } from '../../utils/infinite-pages';
1919
import { ORGANIZATION_PERMISSIONS } from '../../utils/permissions';
2020
import { organizationProfileModal } from '../organization-profile/organization-profile.modal';
21+
import { profileModal } from '../profile/profile.modal';
2122
import type { MosaicProfile } from '../profile-pages/use-mosaic-profile';
22-
import { userProfileModalOptions } from '../profile-pages/use-mosaic-profile';
23+
import { profileModalOptions, userProfileModalOptions } from '../profile-pages/use-mosaic-profile';
2324
import { ORGANIZATION_PAGE_PREFIX } from '../user-profile/user-profile.layout';
2425
import { userProfileModal } from '../user-profile/user-profile.modal';
2526
import type {
@@ -178,6 +179,7 @@ export function useSwitcherModel(
178179
const clerk = useClerk();
179180
const router = useMosaicRouter();
180181
const userProfile = useModal(userProfileModal);
182+
const combinedProfile = useModal(profileModal);
181183
const organizationProfile = useModal(organizationProfileModal);
182184
const { newProfiles } = useMosaicExperimental();
183185
const { userProfileProps, organizationProfileProps, userProfileUrl, organizationProfileUrl } = profile;
@@ -193,13 +195,14 @@ export function useSwitcherModel(
193195
});
194196

195197
const profileCombined = opensCombinedProfile(menuMode, profile);
196-
const mosaicUserProfile = userProfileModalOptions({ ...profile, combined: profileCombined });
197198

198199
const manageAccount = openOrNavigate({
199200
url: userProfileUrl,
200201
openModal: () =>
201202
newProfiles
202-
? userProfile.open(mosaicUserProfile, getContainer?.())
203+
? profileCombined
204+
? combinedProfile.open(profileModalOptions(profile), getContainer?.())
205+
: userProfile.open(userProfileModalOptions(profile), getContainer?.())
203206
: clerk.openUserProfile({
204207
getContainer,
205208
customPages: bridgedCustomPages.userProfile,
@@ -221,9 +224,9 @@ export function useSwitcherModel(
221224
apiKeysProps: organizationProfileProps?.apiKeysProps,
222225
});
223226
} else if (profileCombined) {
224-
userProfile.open(
227+
combinedProfile.open(
225228
{
226-
...mosaicUserProfile,
229+
...profileModalOptions(profile),
227230
page: `${ORGANIZATION_PAGE_PREFIX}${organizationProfileProps?.pageOrder?.[0] ?? 'general'}`,
228231
},
229232
getContainer?.(),
Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,23 @@
1-
import { useUser } from '@clerk/shared/react';
2-
import React from 'react';
1+
import type React from 'react';
32

4-
import { UserProfileSurface } from '../profile/profile';
3+
import { ModalProfile } from '../profile/profile.modal-content';
54
import { useMosaicProfile, userProfileModalOptions } from '../profile-pages/use-mosaic-profile';
6-
import type { UserProfileModalContentProps, UserProfileModalOptions } from './user-profile.modal.types';
5+
import type { UserProfileModalContentProps } from './user-profile.modal.types';
76

87
export default function UserProfileModalContent({
98
options,
109
close,
1110
path,
1211
}: UserProfileModalContentProps): React.ReactElement {
13-
const { isLoaded, user } = useUser();
1412
const profile = useMosaicProfile();
15-
const signedOut = isLoaded && !user;
16-
17-
React.useEffect(() => {
18-
if (signedOut) {
19-
close();
20-
}
21-
}, [signedOut, close]);
22-
2313
// An opening the routing state caused on its own, a deep link or a return, carries no options;
2414
// it gets the provider's, the same ones every handle-driven open spreads in.
25-
const resolved: UserProfileModalOptions = options ?? userProfileModalOptions(profile);
26-
const { page: _page, combined = true, ...props } = resolved;
2715
return (
28-
<UserProfileSurface
29-
{...props}
30-
combined={combined}
31-
presentation='modal'
16+
<ModalProfile
17+
options={options ?? userProfileModalOptions(profile)}
18+
close={close}
3219
path={path}
20+
combined={false}
3321
/>
3422
);
3523
}

‎packages/mosaic/src/features/user-profile/user-profile.modal-return.ts‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import React from 'react';
22

33
import { useModal } from '../../modals/use-modal';
4-
import { useMosaicProfile, userProfileModalOptions } from '../profile-pages/use-mosaic-profile';
4+
import { profileModal } from '../profile/profile.modal';
5+
import { profileModalOptions, useMosaicProfile, userProfileModalOptions } from '../profile-pages/use-mosaic-profile';
56
import { userProfileModal } from './user-profile.modal';
67
import type { UserProfileOptions } from './user-profile.provider';
78

@@ -22,7 +23,8 @@ export function withUserProfileModalReturn(
2223
}
2324

2425
export function UserProfileModalReturn(): null {
25-
const { open } = useModal(userProfileModal);
26+
const { open: openUserProfile } = useModal(userProfileModal);
27+
const { open: openProfile } = useModal(profileModal);
2628
const profile = useMosaicProfile();
2729

2830
React.useEffect(() => {
@@ -34,9 +36,13 @@ export function UserProfileModalReturn(): null {
3436
}
3537
url.searchParams.delete(MODAL_PARAM);
3638
window.history.replaceState(withoutNextAppRouterMarker(window.history.state), '', url);
37-
open({ ...userProfileModalOptions(profile), combined: page === COMBINED_ACCOUNT_PAGE, page: 'account' });
39+
if (page === COMBINED_ACCOUNT_PAGE) {
40+
openProfile({ ...profileModalOptions(profile), page: 'account' });
41+
} else {
42+
openUserProfile({ ...userProfileModalOptions(profile), page: 'account' });
43+
}
3844
});
39-
}, [open, profile]);
45+
}, [openProfile, openUserProfile, profile]);
4046

4147
return null;
4248
}

0 commit comments

Comments
 (0)