Skip to content
Draft
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
7 changes: 7 additions & 0 deletions .changeset/mosaic-links.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@clerk/mosaic': minor
---

A `?.user=security` link now opens a Mosaic component on that page in every routing mode, not only in `query` mode: the param is named after the component's base, so `?.modal.user=account` opens the profile modal on its account page. A component in `virtual`, `path` or `hash` mode moves to the page the link names and removes the param from the URL, including when the link is followed while the page is already open; a component that already has a page in the URL when it loads keeps it.

Redirect flows in `UserProfile` (connecting an OAuth or enterprise account, and verifying an email address through an identity provider) return to the page and modal they started from through such a link, in every routing mode. The `__clerk_mosaic_modal` query param is no longer written or read.
20 changes: 20 additions & 0 deletions packages/mosaic/src/__tests__/feature/routing.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import type { ReactNode } from 'react';

import { MosaicRoutingScope } from '../../routing/routing-scope';
import type { RoutingMode } from '../../routing/use-mosaic-location';
import { useRouterScope } from '../../routing/use-router';

const routes = { root: '' } as const;

export function InRoutingScope({
path,
routing = 'virtual',
children,
}: {
path: string;
routing?: RoutingMode;
children: ReactNode;
}) {
const { scope } = useRouterScope(routes, { routing, path, root: 'root' });
return <MosaicRoutingScope scope={scope}>{children}</MosaicRoutingScope>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ const NO_PAGE = '';
// there when it is.
export function useProfilePagesController(pageIds: readonly string[] | undefined, options: ProfilePagesOptions) {
const ids = pageIds ?? [];
const { route, go } = useRouterScope(Object.fromEntries(ids.map(id => [id, pageSegment(id)])), {
const { route, go, scope } = useRouterScope(Object.fromEntries(ids.map(id => [id, pageSegment(id)])), {
...options,
root: ids[0] ?? NO_PAGE,
ready: pageIds !== undefined,
});

return { activePage: route, onPageChange: go };
return { activePage: route, onPageChange: go, scope };
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ export function ModalProfile({
<UserProfileSurface
{...props}
combined={combined}
presentation='modal'
path={path}
routing={routing}
/>
Expand Down
63 changes: 31 additions & 32 deletions packages/mosaic/src/features/profile/profile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import type { ReactNode } from 'react';

import { MosaicRoutingScope } from '../../routing/routing-scope';
import type { RoutingMode } from '../../routing/use-mosaic-location';
import type { APIKeysPageProps } from '../api-keys/api-keys-table';
import type { OrganizationProfileProps } from '../organization-profile/organization-profile';
Expand All @@ -22,7 +23,6 @@ import { useMosaicProfile } from '../profile-pages/use-mosaic-profile';
import { useUserProfilePages } from '../switcher/switcher.pages';
import type { UserProfileProps } from '../user-profile/user-profile';
import { ORGANIZATION_PAGE_PREFIX, userProfilePageIds } from '../user-profile/user-profile.layout';
import type { UserProfileOptions } from '../user-profile/user-profile.provider';
import { UserProfileProvider } from '../user-profile/user-profile.provider';
import type { UserProfileViewProps } from '../user-profile/user-profile.view';
import { UserProfileView } from '../user-profile/user-profile.view';
Expand Down Expand Up @@ -105,7 +105,6 @@ export function Profile(props: ProfileProps): ReactNode {
return (
<UserProfileSurface
{...userProps}
presentation='mounted'
combined={false}
/>
);
Expand All @@ -118,7 +117,6 @@ export function Profile(props: ProfileProps): ReactNode {
('organizationProfileProps' in ownProps ? ownProps.organizationProfileProps : undefined) ?? {},
provider.organizationProfileProps,
)}
presentation='mounted'
combined
/>
);
Expand Down Expand Up @@ -157,7 +155,6 @@ function withOrganizationDefaults<Props extends NonNullable<CombinedProfileProps
}

export function UserProfileSurface({
presentation,
combined,
organizationProfileProps,
customPages,
Expand All @@ -167,7 +164,7 @@ export function UserProfileSurface({
additionalOAuthScopes,
fallback,
apiKeysProps: { hide: hideAPIKeys = false, ...apiKeysProps } = {},
}: CombinedProfileProps & { presentation: NonNullable<UserProfileOptions['mode']>; combined: boolean }) {
}: CombinedProfileProps & { combined: boolean }) {
const model = useUserProfileUserModel();
const builtInPages = useUserProfilePages();

Expand All @@ -193,11 +190,7 @@ export function UserProfileSurface({
};

return (
<UserProfileProvider
additionalOAuthScopes={additionalOAuthScopes}
mode={presentation}
combined={combined}
>
<UserProfileProvider additionalOAuthScopes={additionalOAuthScopes}>
{combined ? (
<CombinedUserProfileView
key={routing}
Expand All @@ -221,7 +214,7 @@ type AccountViewProps = Omit<UserProfileViewProps, 'activePage' | 'onPageChange'
};

function AccountUserProfileView({ path, routing, ...viewProps }: AccountViewProps) {
const { activePage, onPageChange } = useProfilePagesController(userProfilePageIds(viewProps), {
const { activePage, onPageChange, scope } = useProfilePagesController(userProfilePageIds(viewProps), {
routing,
path,
});
Expand All @@ -231,11 +224,13 @@ function AccountUserProfileView({ path, routing, ...viewProps }: AccountViewProp
}

return (
<UserProfileView
{...viewProps}
activePage={activePage}
onPageChange={onPageChange}
/>
<MosaicRoutingScope scope={scope}>
<UserProfileView
{...viewProps}
activePage={activePage}
onPageChange={onPageChange}
/>
</MosaicRoutingScope>
);
}

Expand All @@ -252,7 +247,7 @@ function CombinedUserProfileView({
? { pages: organizationProfilePanels(model.pages, apiKeysProps), customPages, pageOrder }
: undefined;
const pageIds = model.status === 'loading' ? undefined : userProfilePageIds({ ...viewProps, organization });
const { activePage, onPageChange } = useProfilePagesController(pageIds, {
const { activePage, onPageChange, scope } = useProfilePagesController(pageIds, {
routing,
path,
notFound: parts =>
Expand All @@ -267,12 +262,14 @@ function CombinedUserProfileView({

return (
<OrganizationProfileProvider afterLeaveOrganizationUrl={afterLeaveOrganizationUrl}>
<UserProfileView
{...viewProps}
activePage={activePage}
onPageChange={onPageChange}
organization={organization}
/>
<MosaicRoutingScope scope={scope}>
<UserProfileView
{...viewProps}
activePage={activePage}
onPageChange={onPageChange}
organization={organization}
/>
</MosaicRoutingScope>
</OrganizationProfileProvider>
);
}
Expand All @@ -288,7 +285,7 @@ export function OrganizationProfileSurface({
}: OrganizationProfileProps) {
const model = useOrganizationProfileModel();
const pages = model.status === 'ready' ? organizationProfilePanels(model.pages, apiKeysProps) : undefined;
const { activePage, onPageChange } = useProfilePagesController(
const { activePage, onPageChange, scope } = useProfilePagesController(
pages && organizationProfilePageIds({ pages, customPages, pageOrder }),
{ routing, path },
);
Expand All @@ -302,14 +299,16 @@ export function OrganizationProfileSurface({

return (
<OrganizationProfileProvider afterLeaveOrganizationUrl={afterLeaveOrganizationUrl}>
<OrganizationProfileView
elevation='flush'
activePage={activePage}
onPageChange={onPageChange}
customPages={customPages}
pageOrder={pageOrder}
pages={pages}
/>
<MosaicRoutingScope scope={scope}>
<OrganizationProfileView
elevation='flush'
activePage={activePage}
onPageChange={onPageChange}
customPages={customPages}
pageOrder={pageOrder}
pages={pages}
/>
</MosaicRoutingScope>
</OrganizationProfileProvider>
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ describe('Switcher with experimental.newProfiles', () => {
});

it.each([
{ name: 'combined', props: {}, expected: 'profile/account' },
{ name: 'user-only', props: { mode: 'user' as const }, expected: 'user-profile/account' },
{ name: 'combined', props: {}, expected: '.modal.profile' },
{ name: 'user-only', props: { mode: 'user' as const }, expected: '.modal.user' },
])('opens the $name profile as a modal, so an OAuth connection returns to it', async ({ props, expected }) => {
await renderWithNewProfiles(props, withConnectedAccounts([]));
const request = holdRequests('post', '/v1/me/external_accounts');
Expand All @@ -141,7 +141,7 @@ describe('Switcher with experimental.newProfiles', () => {

const body = new URLSearchParams(await request.requests[0]?.text());
const params = new URL(body.get('redirect_url') || '').searchParams;
expect(params.get('__clerk_mosaic_modal')).toBe(expected);
expect(params.get(expected)).toBe('account');
expect(params.has('__clerk_modal_state')).toBe(false);
request.fail();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -362,16 +362,16 @@ describe('UserProfile routed in the URL', () => {

it('starts over from the URL when its mode changes', async () => {
serve();
startAt('/app?.user=security');
startAt('/app#.user=security');
const { rerender } = await renderWithClerk(<UserProfile />);
const user = userEvent.setup();
expectSelected('Account');

await user.click(screen.getByRole('tab', { name: 'Security' }));
rerender(<UserProfile routing='hash' />);
rerender(<UserProfile routing='query' />);
expectSelected('Account');

rerender(<UserProfile routing='query' />);
rerender(<UserProfile routing='hash' />);
expectSelected('Security');
});

Expand Down
Loading
Loading