Skip to content

Commit 7731e2f

Browse files
refactor(mosaic): resolve the open profile page with one shared controller (#10530)
1 parent 2f988b8 commit 7731e2f

11 files changed

Lines changed: 103 additions & 150 deletions

‎.changeset/shy-pages-share.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
---
2+
---

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

Lines changed: 0 additions & 30 deletions
This file was deleted.

‎packages/mosaic/src/features/organization-profile/organization-profile.layout.ts‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ import { withUniquePaths } from '../profile-pages/profile-pages.layout';
99
import type {
1010
CustomOrganizationProfilePage,
1111
OrganizationProfilePageId,
12+
OrganizationProfilePageListOptions,
1213
OrganizationProfilePages,
1314
} from './organization-profile.types';
14-
import type { OrganizationProfilePageListOptions } from './organization-profile.view';
1515

1616
export const ORGANIZATION_PROFILE_PAGE_IDS: readonly OrganizationProfilePageId[] = [
1717
'general',
@@ -58,12 +58,14 @@ export function resolveOrganizationProfilePages(
5858
return applyOrder(order, entries, entry => entry.id);
5959
}
6060

61-
export function organizationProfilePageIds({
61+
export function organizationProfileNavEntries({
6262
pages,
6363
customPages,
6464
pageOrder,
65-
}: OrganizationProfilePageListOptions): string[] {
66-
return resolveOrganizationProfilePages(getAvailableOrganizationProfilePages(pages), customPages, pageOrder).map(
67-
entry => entry.id,
68-
);
65+
}: OrganizationProfilePageListOptions): OrganizationProfileNavEntry[] {
66+
return resolveOrganizationProfilePages(getAvailableOrganizationProfilePages(pages), customPages, pageOrder);
67+
}
68+
69+
export function organizationProfilePageIds(options: OrganizationProfilePageListOptions): string[] {
70+
return organizationProfileNavEntries(options).map(entry => entry.id);
6971
}

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

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

55
import type { APIKeysPageProps } from '../api-keys/api-keys-table';
6-
import { useOrganizationProfileController } from './organization-profile.controller';
6+
import { useProfilePagesController } from '../profile-pages/profile-pages.controller';
7+
import { organizationProfilePageIds } from './organization-profile.layout';
78
import { useOrganizationProfileModel } from './organization-profile.model';
89
import type { OrganizationProfileOptions } from './organization-profile.provider';
910
import { OrganizationProfileProvider } from './organization-profile.provider';
@@ -88,7 +89,10 @@ export function OrganizationProfile({
8889
}: OrganizationProfileProps) {
8990
const model = useOrganizationProfileModel();
9091
const pages = model.status === 'ready' ? organizationProfilePanels(model.pages, apiKeysProps) : undefined;
91-
const navigation = useOrganizationProfileController({ pages, customPages, pageOrder, defaultPage });
92+
const navigation = useProfilePagesController(
93+
defaultPage ?? pageOrder?.[0] ?? 'general',
94+
pages && organizationProfilePageIds({ pages, customPages, pageOrder }),
95+
);
9296

9397
if (model.status === 'loading') {
9498
return fallback ?? null;

‎packages/mosaic/src/features/organization-profile/organization-profile.types.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,16 @@ export interface CustomOrganizationProfilePage {
1717
icon?: ReactNode;
1818
content: ReactNode;
1919
}
20+
21+
/** What the organization's navigation is built from. */
22+
export interface OrganizationProfilePageListOptions {
23+
/** The built-in pages. One left undefined is left out of the navigation. */
24+
pages: OrganizationProfilePages;
25+
/** Pages of the consumer's own, added to the navigation after the built-ins. */
26+
customPages?: readonly CustomOrganizationProfilePage[];
27+
/**
28+
* The order the navigation runs in, by id: a built-in page's id, or a custom page's `path`. Ids
29+
* left out keep their default place behind the ones named.
30+
*/
31+
pageOrder?: readonly (OrganizationProfilePageId | (string & {}))[];
32+
}

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

Lines changed: 17 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,8 @@ import { Icon } from '../../components/icon';
55
import { useMessages } from '../../localization';
66
import type { ProfilePage, ProfilePagesViewProps } from '../profile-pages/profile-pages.view';
77
import { ProfilePagesView } from '../profile-pages/profile-pages.view';
8-
import {
9-
getAvailableOrganizationProfilePages,
10-
ORGANIZATION_PROFILE_PAGE_ICONS,
11-
resolveOrganizationProfilePages,
12-
} from './organization-profile.layout';
13-
import type {
14-
CustomOrganizationProfilePage,
15-
OrganizationProfilePageId,
16-
OrganizationProfilePages,
17-
} from './organization-profile.types';
18-
19-
/** What the organization's navigation is built from. */
20-
export interface OrganizationProfilePageListOptions {
21-
/** The built-in pages. One left undefined is left out of the navigation. */
22-
pages: OrganizationProfilePages;
23-
/** Pages of the consumer's own, added to the navigation after the built-ins. */
24-
customPages?: readonly CustomOrganizationProfilePage[];
25-
/**
26-
* The order the navigation runs in, by id: a built-in page's id, or a custom page's `path`. Ids
27-
* left out keep their default place behind the ones named.
28-
*/
29-
pageOrder?: readonly (OrganizationProfilePageId | (string & {}))[];
30-
}
8+
import { ORGANIZATION_PROFILE_PAGE_ICONS, organizationProfileNavEntries } from './organization-profile.layout';
9+
import type { OrganizationProfilePageId, OrganizationProfilePageListOptions } from './organization-profile.types';
3110

3211
export interface OrganizationProfileViewProps
3312
extends
@@ -59,23 +38,22 @@ export const OrganizationProfileView = React.forwardRef<HTMLDivElement, Organiza
5938

6039
/** The organization's navigation, in order: the built-ins it was given content for, then the consumer's pages. */
6140
export function organizationProfilePageList(
62-
{ pages, customPages, pageOrder }: OrganizationProfilePageListOptions,
41+
options: OrganizationProfilePageListOptions,
6342
labels: Record<OrganizationProfilePageId, string>,
6443
): ProfilePage[] {
65-
return resolveOrganizationProfilePages(getAvailableOrganizationProfilePages(pages), customPages, pageOrder).map(
66-
entry =>
67-
entry.custom
68-
? { id: entry.id, label: entry.custom.label, icon: entry.custom.icon, content: entry.custom.content }
69-
: {
70-
id: entry.id,
71-
label: labels[entry.id],
72-
icon: (
73-
<Icon
74-
glyph={ORGANIZATION_PROFILE_PAGE_ICONS[entry.id]}
75-
size='sm'
76-
/>
77-
),
78-
content: pages[entry.id],
79-
},
44+
return organizationProfileNavEntries(options).map(entry =>
45+
entry.custom
46+
? { id: entry.id, label: entry.custom.label, icon: entry.custom.icon, content: entry.custom.content }
47+
: {
48+
id: entry.id,
49+
label: labels[entry.id],
50+
icon: (
51+
<Icon
52+
glyph={ORGANIZATION_PROFILE_PAGE_ICONS[entry.id]}
53+
size='sm'
54+
/>
55+
),
56+
content: options.pages[entry.id],
57+
},
8058
);
8159
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { useState } from 'react';
2+
3+
export function useProfilePagesController(initialPage: string, pageIds: readonly string[] | undefined) {
4+
const [activePage, setActivePage] = useState(initialPage);
5+
6+
if (pageIds) {
7+
const [firstPage] = pageIds;
8+
if (firstPage !== undefined && !pageIds.includes(activePage)) {
9+
setActivePage(firstPage);
10+
}
11+
}
12+
13+
return { activePage, onPageChange: setActivePage };
14+
}

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

Lines changed: 0 additions & 45 deletions
This file was deleted.

‎packages/mosaic/src/features/user-profile/user-profile.layout.ts‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import { CreditCard } from '../../icons/glyphs/credit-card';
66
import { ShieldCheck } from '../../icons/glyphs/shield-check';
77
import { UserCircle } from '../../icons/glyphs/user-circle';
88
import { applyOrder } from '../../utils/apply-order';
9+
import { organizationProfilePageIds } from '../organization-profile/organization-profile.layout';
10+
import type { OrganizationProfilePageListOptions } from '../organization-profile/organization-profile.types';
911
import { withUniquePaths } from '../profile-pages/profile-pages.layout';
1012
import type { CustomProfilePage, UserProfilePageId, UserProfilePages } from './user-profile.types';
1113

@@ -58,7 +60,30 @@ export function resolveUserProfilePages(
5860
return applyOrder(order, entries, entry => entry.id);
5961
}
6062

61-
export function withoutOrganizationPaths(customPages: readonly CustomProfilePage[] = []): CustomProfilePage[] {
63+
type UserProfilePageListOptions = {
64+
pages: UserProfilePages;
65+
customPages?: readonly CustomProfilePage[];
66+
pageOrder?: readonly string[];
67+
organization?: OrganizationProfilePageListOptions;
68+
};
69+
70+
export function userProfileNavEntries({
71+
pages,
72+
customPages,
73+
pageOrder,
74+
}: UserProfilePageListOptions): UserProfileNavEntry[] {
75+
return resolveUserProfilePages(getAvailableUserProfilePages(pages), withoutOrganizationPaths(customPages), pageOrder);
76+
}
77+
78+
export function userProfilePageIds(options: UserProfilePageListOptions): string[] {
79+
const accountPageIds = userProfileNavEntries(options).map(entry => entry.id);
80+
const organizationPageIds = options.organization
81+
? organizationProfilePageIds(options.organization).map(id => `${ORGANIZATION_PAGE_PREFIX}${id}`)
82+
: [];
83+
return [...accountPageIds, ...organizationPageIds];
84+
}
85+
86+
function withoutOrganizationPaths(customPages: readonly CustomProfilePage[] = []): CustomProfilePage[] {
6287
return customPages.filter(page => {
6388
if (!page.path.startsWith(ORGANIZATION_PAGE_PREFIX)) {
6489
return true;

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

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ import type { OrganizationProfileProps } from '../organization-profile/organizat
77
import { organizationProfilePanels } from '../organization-profile/organization-profile';
88
import { useOrganizationProfileModel } from '../organization-profile/organization-profile.model';
99
import { OrganizationProfileProvider } from '../organization-profile/organization-profile.provider';
10+
import { useProfilePagesController } from '../profile-pages/profile-pages.controller';
1011
import { useUserProfilePages } from '../switcher/switcher.pages';
1112
import type { SwitcherMode } from '../switcher/switcher.types';
12-
import { useUserProfileController } from './user-profile.controller';
13+
import { userProfilePageIds } from './user-profile.layout';
1314
import type { UserProfileOptions } from './user-profile.provider';
1415
import { UserProfileProvider } from './user-profile.provider';
1516
import type { UserProfileViewProps } from './user-profile.view';
@@ -174,7 +175,7 @@ type AccountViewProps = Omit<UserProfileViewProps, 'activePage' | 'onPageChange'
174175
};
175176

176177
function AccountUserProfileView({ initialPage, ...viewProps }: AccountViewProps) {
177-
const navigation = useUserProfileController({ initialPage, ...viewProps });
178+
const navigation = useProfilePagesController(initialPage, userProfilePageIds(viewProps));
178179

179180
return (
180181
<UserProfileView
@@ -190,25 +191,21 @@ function CombinedUserProfileView({
190191
...viewProps
191192
}: AccountViewProps & Pick<UserProfileProps, 'organizationProfileProps'>) {
192193
const model = useOrganizationProfileModel();
193-
const organizationState =
194+
const organization =
194195
model.status === 'ready'
195-
? {
196-
status: 'ready' as const,
197-
pageList: { pages: organizationProfilePanels(model.pages, apiKeysProps), customPages, pageOrder },
198-
}
199-
: { status: model.status };
200-
const navigation = useUserProfileController({
196+
? { pages: organizationProfilePanels(model.pages, apiKeysProps), customPages, pageOrder }
197+
: undefined;
198+
const navigation = useProfilePagesController(
201199
initialPage,
202-
...viewProps,
203-
organization: organizationState,
204-
});
200+
model.status === 'loading' ? undefined : userProfilePageIds({ ...viewProps, organization }),
201+
);
205202

206203
return (
207204
<OrganizationProfileProvider afterLeaveOrganizationUrl={afterLeaveOrganizationUrl}>
208205
<UserProfileView
209206
{...viewProps}
210207
{...navigation}
211-
organization={organizationState.status === 'ready' ? organizationState.pageList : undefined}
208+
organization={organization}
212209
/>
213210
</OrganizationProfileProvider>
214211
);

0 commit comments

Comments
 (0)