@@ -64,7 +64,7 @@ type UseCustomPagesOptions = {
6464 allowForAnyChildren : boolean ;
6565} ;
6666
67- type CustomPageWithIdType = UserProfilePageProps & { children ?: React . ReactNode } ;
67+ type CustomPageWithIdType = UserProfilePageProps & { children ?: React . ReactNode ; portalId ?: string } ;
6868
6969/**
7070 * Exclude any children that is used for identifying Custom Pages or Custom Items.
@@ -121,13 +121,21 @@ const useCustomPages = (params: UseCustomPagesParams, options?: UseCustomPagesOp
121121
122122 const { children, label, url, labelIcon } = props ;
123123
124+ const childKey = ( child as ReactElement ) . key ;
125+
124126 if ( isThatComponent ( child , PageComponent ) ) {
125127 if ( isReorderItem ( props , reorderItemsLabels ) ) {
126128 // This is a reordering item
127129 validChildren . push ( { label } ) ;
128130 } else if ( isCustomPage ( props ) ) {
129131 // this is a custom page
130- validChildren . push ( { label, labelIcon, children, url } ) ;
132+ validChildren . push ( {
133+ label,
134+ labelIcon,
135+ children,
136+ url,
137+ portalId : getCustomPagePortalId ( 'page' , props , childKey ) ,
138+ } ) ;
131139 } else {
132140 logErrorInDevMode ( customPageWrongProps ( componentName ) ) ;
133141 return ;
@@ -137,7 +145,7 @@ const useCustomPages = (params: UseCustomPagesParams, options?: UseCustomPagesOp
137145 if ( isThatComponent ( child , LinkComponent ) ) {
138146 if ( isExternalLink ( props ) ) {
139147 // This is an external link
140- validChildren . push ( { label, labelIcon, url } ) ;
148+ validChildren . push ( { label, labelIcon, url, portalId : getCustomPagePortalId ( 'link' , props , childKey ) } ) ;
141149 } else {
142150 logErrorInDevMode ( customLinkWrongProps ( componentName ) ) ;
143151 return ;
@@ -151,12 +159,12 @@ const useCustomPages = (params: UseCustomPagesParams, options?: UseCustomPagesOp
151159
152160 validChildren . forEach ( ( cp , index ) => {
153161 if ( isCustomPage ( cp ) ) {
154- customPageContents . push ( { component : cp . children , id : index } ) ;
155- customPageLabelIcons . push ( { component : cp . labelIcon , id : index } ) ;
162+ customPageContents . push ( { component : cp . children , id : cp . portalId || index } ) ;
163+ customPageLabelIcons . push ( { component : cp . labelIcon , id : cp . portalId || index } ) ;
156164 return ;
157165 }
158166 if ( isExternalLink ( cp ) ) {
159- customLinkLabelIcons . push ( { component : cp . labelIcon , id : index } ) ;
167+ customLinkLabelIcons . push ( { component : cp . labelIcon , id : cp . portalId || index } ) ;
160168 }
161169 } ) ;
162170
@@ -177,12 +185,12 @@ const useCustomPages = (params: UseCustomPagesParams, options?: UseCustomPagesOp
177185 portal : contentPortal ,
178186 mount,
179187 unmount,
180- } = customPageContentsPortals . find ( p => p . id === index ) as UseCustomElementPortalReturn ;
188+ } = customPageContentsPortals . find ( p => p . id === ( cp . portalId || index ) ) as UseCustomElementPortalReturn ;
181189 const {
182190 portal : labelPortal ,
183191 mount : mountIcon ,
184192 unmount : unmountIcon ,
185- } = customPageLabelIconsPortals . find ( p => p . id === index ) as UseCustomElementPortalReturn ;
193+ } = customPageLabelIconsPortals . find ( p => p . id === ( cp . portalId || index ) ) as UseCustomElementPortalReturn ;
186194 customPages . push ( { label : cp . label , url : cp . url , mount, unmount, mountIcon, unmountIcon } ) ;
187195 customPagesPortals . push ( contentPortal ) ;
188196 customPagesPortals . push ( labelPortal ) ;
@@ -193,7 +201,7 @@ const useCustomPages = (params: UseCustomPagesParams, options?: UseCustomPagesOp
193201 portal : labelPortal ,
194202 mount : mountIcon ,
195203 unmount : unmountIcon ,
196- } = customLinkLabelIconsPortals . find ( p => p . id === index ) as UseCustomElementPortalReturn ;
204+ } = customLinkLabelIconsPortals . find ( p => p . id === ( cp . portalId || index ) ) as UseCustomElementPortalReturn ;
197205 customPages . push ( { label : cp . label , url : cp . url , mountIcon, unmountIcon } ) ;
198206 customPagesPortals . push ( labelPortal ) ;
199207 return ;
@@ -203,6 +211,14 @@ const useCustomPages = (params: UseCustomPagesParams, options?: UseCustomPagesOp
203211 return { customPages, customPagesPortals } ;
204212} ;
205213
214+ const getCustomPagePortalId = (
215+ type : 'page' | 'link' ,
216+ props : Pick < CustomPageWithIdType , 'label' | 'url' > ,
217+ key : React . Key | null ,
218+ ) => {
219+ return key == null ? `${ type } :${ props . label } :${ props . url } ` : `${ type } :key:${ key } ` ;
220+ } ;
221+
206222const isReorderItem = ( childProps : any , validItems : string [ ] ) : boolean => {
207223 const { children, label, url, labelIcon } = childProps ;
208224 return ! children && ! url && ! labelIcon && validItems . some ( v => v === label ) ;
0 commit comments