11'use client' ;
22
33import type { ReactNode } from 'react' ;
4- import { useState } from 'react' ;
54
65import type { APIKeysPageProps } from '../api-keys/api-keys-table' ;
76import type { OrganizationProfileProps } from '../organization-profile/organization-profile' ;
8- import { organizationProfilePageIds , organizationProfilePanels } from '../organization-profile/organization-profile' ;
7+ import { organizationProfilePanels } from '../organization-profile/organization-profile' ;
98import { useOrganizationProfileModel } from '../organization-profile/organization-profile.model' ;
109import { OrganizationProfileProvider } from '../organization-profile/organization-profile.provider' ;
1110import { useUserProfilePages } from '../switcher/switcher.pages' ;
1211import type { SwitcherMode } from '../switcher/switcher.types' ;
13- import {
14- getAvailableUserProfilePages ,
15- ORGANIZATION_PAGE_PREFIX ,
16- resolveUserProfilePages ,
17- withoutOrganizationPaths ,
18- } from './user-profile.layout' ;
12+ import { useUserProfileController } from './user-profile.controller' ;
1913import type { UserProfileOptions } from './user-profile.provider' ;
2014import { UserProfileProvider } from './user-profile.provider' ;
2115import type { UserProfileViewProps } from './user-profile.view' ;
@@ -175,18 +169,17 @@ export function UserProfile({
175169 ) ;
176170}
177171
178- type AccountViewProps = Omit < UserProfileViewProps , 'activePage' | 'onPageChange' > & {
172+ type AccountViewProps = Omit < UserProfileViewProps , 'activePage' | 'onPageChange' | 'organization' > & {
179173 initialPage : UserProfileViewProps [ 'activePage' ] ;
180174} ;
181175
182176function AccountUserProfileView ( { initialPage, ...viewProps } : AccountViewProps ) {
183- const [ activePage , setActivePage ] = useState ( initialPage ) ;
177+ const navigation = useUserProfileController ( { initialPage, ... viewProps } ) ;
184178
185179 return (
186180 < UserProfileView
187181 { ...viewProps }
188- activePage = { activePage }
189- onPageChange = { setActivePage }
182+ { ...navigation }
190183 />
191184 ) ;
192185}
@@ -197,34 +190,25 @@ function CombinedUserProfileView({
197190 ...viewProps
198191} : AccountViewProps & Pick < UserProfileProps , 'organizationProfileProps' > ) {
199192 const model = useOrganizationProfileModel ( ) ;
200- const [ activePage , setActivePage ] = useState ( initialPage ) ;
201- const organization =
193+ const organizationState =
202194 model . status === 'ready'
203- ? { pages : organizationProfilePanels ( model . pages , apiKeysProps ) , customPages, pageOrder }
204- : undefined ;
205-
206- if ( model . status !== 'loading' && activePage . startsWith ( ORGANIZATION_PAGE_PREFIX ) ) {
207- const organizationPageIds = organization
208- ? organizationProfilePageIds ( organization ) . map ( id => `${ ORGANIZATION_PAGE_PREFIX } ${ id } ` )
209- : [ ] ;
210- if ( ! organizationPageIds . includes ( activePage ) ) {
211- setActivePage (
212- resolveUserProfilePages (
213- getAvailableUserProfilePages ( viewProps . pages ) ,
214- withoutOrganizationPaths ( viewProps . customPages ) ,
215- viewProps . pageOrder ,
216- ) [ 0 ] ?. id ?? 'account' ,
217- ) ;
218- }
219- }
195+ ? {
196+ status : 'ready' as const ,
197+ pageList : { pages : organizationProfilePanels ( model . pages , apiKeysProps ) , customPages, pageOrder } ,
198+ }
199+ : { status : model . status } ;
200+ const navigation = useUserProfileController ( {
201+ initialPage,
202+ ...viewProps ,
203+ organization : organizationState ,
204+ } ) ;
220205
221206 return (
222207 < OrganizationProfileProvider afterLeaveOrganizationUrl = { afterLeaveOrganizationUrl } >
223208 < UserProfileView
224209 { ...viewProps }
225- activePage = { activePage }
226- onPageChange = { setActivePage }
227- organization = { organization }
210+ { ...navigation }
211+ organization = { organizationState . status === 'ready' ? organizationState . pageList : undefined }
228212 />
229213 </ OrganizationProfileProvider >
230214 ) ;
0 commit comments