Fix: Add recursive search max depth (20)

This commit is contained in:
Daniel Hougaard
2024-04-12 13:28:03 +02:00
parent d734a3f6f4
commit 75267987fc
+11 -4
View File
@@ -92,7 +92,8 @@ const buildHierarchy = (folders: TSecretFolders[]): FolderMap => {
const generatePaths = ( const generatePaths = (
map: FolderMap, map: FolderMap,
parentId: string = "null", parentId: string = "null",
basePath: string = "" basePath: string = "",
currentDepth: number = 0
): { path: string; folderId: string }[] => { ): { path: string; folderId: string }[] => {
const children = map[parentId || "null"] || []; const children = map[parentId || "null"] || [];
let paths: { path: string; folderId: string }[] = []; let paths: { path: string; folderId: string }[] = [];
@@ -105,13 +106,19 @@ const generatePaths = (
// eslint-disable-next-line no-nested-ternary // eslint-disable-next-line no-nested-ternary
const currPath = basePath === "" ? (isRootFolder ? "/" : `/${child.name}`) : `${basePath}/${child.name}`; const currPath = basePath === "" ? (isRootFolder ? "/" : `/${child.name}`) : `${basePath}/${child.name}`;
// Add the current path
paths.push({ paths.push({
path: currPath, path: currPath,
folderId: child.id folderId: child.id
}); // Add the current path });
// Recursively generate paths for children, passing down the formatted pathh // We make sure that the max depth doesn't exceed 20.
const childPaths = generatePaths(map, child.id, currPath); // We do this to make as a "circuit break", basically to ensure that we can't encounter any potential memory leaks.
if (currentDepth >= 20) {
return;
}
// Recursively generate paths for children, passing down the formatted path
const childPaths = generatePaths(map, child.id, currPath, currentDepth + 1);
paths = paths.concat( paths = paths.concat(
childPaths.map((p) => ({ childPaths.map((p) => ({
path: p.path, path: p.path,