@@ -20,7 +20,7 @@ import { type FC, useEffect, useState } from "react";
20
20
import { Helmet } from "react-helmet-async" ;
21
21
import { useMutation , useQuery , useQueryClient } from "react-query" ;
22
22
import { useNavigate , useParams , useSearchParams } from "react-router-dom" ;
23
- import { type FileTree , traverse } from "utils/filetree" ;
23
+ import { type FileTree , existsFile , traverse } from "utils/filetree" ;
24
24
import { pageTitle } from "utils/page" ;
25
25
import { TarReader , TarWriter } from "utils/tar" ;
26
26
import { createTemplateVersionFileTree } from "utils/templateVersion" ;
@@ -88,9 +88,8 @@ export const TemplateVersionEditorPage: FC = () => {
88
88
useState < TemplateVersion > ( ) ;
89
89
90
90
// File navigation
91
- // It can be undefined when a selected file is deleted
92
- const activePath : string | undefined =
93
- searchParams . get ( "path" ) ?? findEntrypointFile ( fileTree ?? { } ) ;
91
+ const activePath = getActivePath ( searchParams , fileTree || { } ) ;
92
+
94
93
const onActivePathChange = ( path : string | undefined ) => {
95
94
if ( path ) {
96
95
searchParams . set ( "path" , path ) ;
@@ -392,4 +391,15 @@ export const findEntrypointFile = (fileTree: FileTree): string | undefined => {
392
391
return initialFile ;
393
392
} ;
394
393
394
+ export const getActivePath = (
395
+ searchParams : URLSearchParams ,
396
+ fileTree : FileTree ,
397
+ ) : string | undefined => {
398
+ const selectedPath = searchParams . get ( "path" ) ;
399
+ if ( selectedPath && existsFile ( selectedPath , fileTree ) ) {
400
+ return selectedPath ;
401
+ }
402
+ return findEntrypointFile ( fileTree ) ;
403
+ } ;
404
+
395
405
export default TemplateVersionEditorPage ;
0 commit comments