@@ -157,17 +157,17 @@ export function NavSections() {
157157 onOpenChange = { ( open ) =>
158158 setEditingSectionId ( open ? editingSectionId : null )
159159 }
160- section = { findSectionById ( sections , editingSectionId ) }
160+ section = { findSectionByIdGuaranteed ( sections , editingSectionId ) }
161161 onSubmit = { handleSectionEdit }
162162 />
163163 ) }
164- { movingSectionId && (
164+ { sections && sections . length > 0 && movingSectionId && (
165165 < MoveSectionDialog
166166 isOpened = { ! ! movingSectionId }
167167 onOpenChange = { ( open ) =>
168168 setMovingSectionId ( open ? movingSectionId : null )
169169 }
170- section = { findSectionById ( sections , movingSectionId ) }
170+ section = { findSectionByIdGuaranteed ( sections , movingSectionId ) }
171171 onSubmit = { handleSectionMove }
172172 />
173173 ) }
@@ -378,24 +378,34 @@ function SectionsSkeleton() {
378378 ) ;
379379}
380380
381- function findSectionById (
381+ function findSectionByIdGuaranteed (
382382 sections : SectionResponse [ ] | undefined ,
383383 id : string
384384) : SectionResponse {
385- if ( ! sections ) throw new Error ( "Sections are undefined" ) ;
385+ if ( ! sections || sections . length === 0 )
386+ throw new Error ( "Sections are undefined or empty" ) ;
386387
388+ const found = findSectionByIdRecursive ( sections , id ) ;
389+ if ( ! found ) throw new Error ( "Section with id " + id + " not found" ) ;
390+ return found ;
391+ }
392+
393+ function findSectionByIdRecursive (
394+ sections : SectionResponse [ ] ,
395+ id : string
396+ ) : SectionResponse | null {
387397 for ( const section of sections ) {
388398 if ( section . id === id ) {
389399 return section ;
390400 }
391401
392402 if ( section . subsections ) {
393- const found = findSectionById ( section . subsections , id ) ;
403+ const found = findSectionByIdRecursive ( section . subsections , id ) ;
394404 if ( found ) {
395405 return found ;
396406 }
397407 }
398408 }
399409
400- throw new Error ( "Section with id " + id + " not found" ) ;
410+ return null ;
401411}
0 commit comments