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 44bf370
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions utils/exampleFileUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit 44bf370

Please sign in to comment.