All files / src/shared/lib/assets buildAssetNavigatorItems.ts

92.86% Statements 52/56
77.78% Branches 21/27
100% Functions 8/8
92.45% Lines 49/53

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 1341x                                 1x         1x       9x 9x 9x   9x 12x 12x 4x       5x 5x   5x 4x     1x                   1x         5x 5x   5x 14x 7x 7x 7x 7x 8x 8x       5x 1x 1x   3x     2x                 1x     4x 9x     9x 9x   9x 16x 16x 16x 9x 9x 1x   8x               7x 5x     5x 5x                     4x    
import { join } from "path";
 
type Asset = {
  id: string;
  filename: string;
  plugin?: string;
};
 
export type FileSystemNavigatorItem<T> = {
  id: string;
  type: "file" | "folder";
  name: string;
  filename: string;
  nestLevel?: number;
  asset?: T;
};
 
const collator = new Intl.Collator(undefined, {
  numeric: true,
  sensitivity: "base",
});
 
const sortByName = <T>(
  a: FileSystemNavigatorItem<T>,
  b: FileSystemNavigatorItem<T>,
) => {
  const aSegments = a.name.split(/[\\/]/);
  const bSegments = b.name.split(/[\\/]/);
  const sharedLength = Math.min(aSegments.length, bSegments.length);
 
  for (let i = 0; i < sharedLength; i++) {
    const comparison = collator.compare(aSegments[i], bSegments[i]);
    if (comparison !== 0) {
      return comparison;
    }
  }
 
  if (aSegments.length !== bSegments.length) {
    const shorterItem = aSegments.length < bSegments.length ? a : b;
 
    if (shorterItem.type === "folder") {
      return aSegments.length - bSegments.length;
    }
 
    return aSegments.length < bSegments.length ? 1 : -1;
  }
 
  Iif (a.type !== b.type) {
    return a.type === "folder" ? -1 : 1;
  }
 
  return collator.compare(a.id, b.id);
};
 
export const buildAssetNavigatorItems = <T extends Asset>(
  assets: T[],
  openFolders: string[],
  searchTerm: string,
): FileSystemNavigatorItem<T>[] => {
  const result: FileSystemNavigatorItem<T>[] = [];
  const uniqueFolders = new Set<string>();
 
  const isVisible = (filename: string, nestLevel?: number): boolean => {
    if (nestLevel === undefined || nestLevel === 0) return true;
    const pathSegments = filename.split(/[\\/]/);
    pathSegments.pop();
    let pathCheck = "";
    return pathSegments.every((segment, index) => {
      pathCheck += (index ? "/" : "") + segment;
      return openFolders.includes(pathCheck);
    });
  };
 
  if (searchTerm.length > 0) {
    const searchTermUpperCase = searchTerm.toLocaleUpperCase();
    assets
      .filter((asset) =>
        asset.filename.toLocaleUpperCase().includes(searchTermUpperCase),
      )
      .forEach((asset) => {
        result.push({
          id: asset.id,
          type: "file",
          name: asset.filename.replace(/\.[^.]*$/, ""),
          filename: asset.filename.replace(/.*[/\\]/, ""),
          nestLevel: 0,
          asset,
        });
      });
    return result;
  }
 
  assets.slice().forEach((asset) => {
    const path = asset.plugin
      ? join("plugins", asset.plugin, asset.filename)
      : asset.filename;
    const parts = path.split(/[\\/]/);
    let currentPath = "";
 
    parts.forEach((part, index) => {
      const isLast = index === parts.length - 1;
      currentPath += (currentPath ? "/" : "") + part;
      if (isLast) {
        const nestLevel = parts.length > 1 ? parts.length - 1 : 0;
        if (!isVisible(currentPath, nestLevel)) {
          return;
        }
        result.push({
          id: asset.id,
          type: "file",
          name: currentPath.replace(/\.[^.]*$/, ""),
          filename: part,
          nestLevel,
          asset,
        });
      } else if (!uniqueFolders.has(currentPath)) {
        Iif (!isVisible(currentPath, index)) {
          return;
        }
        uniqueFolders.add(currentPath);
        result.push({
          id: currentPath,
          type: "folder",
          name: currentPath,
          filename: part,
          nestLevel: index,
        });
      }
    });
  });
 
  return result.sort(sortByName);
};