diff --git a/utils/exampleFileUtils.ts b/utils/exampleFileUtils.ts index 3b1c9fb..7b65669 100644 --- a/utils/exampleFileUtils.ts +++ b/utils/exampleFileUtils.ts @@ -90,18 +90,23 @@ export const setFirstFileOpen = (directoryInfo: DirectoryInfo): [DirectoryInfo, return [directoryInfoResult, openedFileInfo]; }; -export const setFileOpen = (directoryInfo: DirectoryInfo, targetFile: string): [DirectoryInfo, FileInfo | null] => { - const cloneDirectoryInfo = cloneDeep(directoryInfo); - let _openedFileInfo: FileInfo | null = null; - cloneDirectoryInfo.children.forEach((child, i) => { +export const setFileOpen = (directoryInfo: DirectoryInfo, targetFilePath: string): [DirectoryInfo, FileInfo | null] => { + const clonedDirectoryInfo = cloneDeep(directoryInfo); + const openedFileInfo = findOpenedFileInfo(clonedDirectoryInfo, targetFilePath); + return [clonedDirectoryInfo, openedFileInfo]; +}; + +const findOpenedFileInfo = (directoryInfo: DirectoryInfo, targetFilePath: string): FileInfo | null => { + let opendFileInfo: FileInfo | null = null; + directoryInfo.children.forEach((child, i) => { if (child.isFile) { - child.isOpen = child.path === targetFile; - _openedFileInfo = child.path === targetFile ? child : _openedFileInfo; + child.isOpen = child.path === targetFilePath; + if (child.isOpen) { + opendFileInfo = child; + } } else { - const [childInfo, openedFileInfo] = setFileOpen(child, targetFile); - cloneDirectoryInfo.children[i] = childInfo; - _openedFileInfo = openedFileInfo; + opendFileInfo = opendFileInfo ?? findOpenedFileInfo(child, targetFilePath); } }); - return [cloneDirectoryInfo, _openedFileInfo]; -}; + return opendFileInfo; +}