Skip to content

Commit

Permalink
fix: prevent fileinfo being overwritten with null when selecting a file
Browse files Browse the repository at this point in the history
  • Loading branch information
blurfx committed Oct 20, 2023
1 parent a4c5042 commit ba02940
Showing 1 changed file with 21 additions and 12 deletions.
33 changes: 21 additions & 12 deletions utils/exampleFileUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,18 +90,27 @@ 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;
for (const child of directoryInfo.children) {
if (opendFileInfo != null) {
break;
}
if (child.isFile) {
child.isOpen = child.path === targetFile;
_openedFileInfo = child.path === targetFile ? child : _openedFileInfo;
child.isOpen = child.path === targetFilePath;
if (child.isOpen) {
opendFileInfo = child;
break;
}
} else {
const [childInfo, openedFileInfo] = setFileOpen(child, targetFile);
cloneDirectoryInfo.children[i] = childInfo;
_openedFileInfo = openedFileInfo;
opendFileInfo = opendFileInfo ?? findOpenedFileInfo(child, targetFilePath);
}
});
return [cloneDirectoryInfo, _openedFileInfo];
};
}
return opendFileInfo;
}

0 comments on commit ba02940

Please sign in to comment.