All files / src/shared/lib/entities buildSceneNavigatorItems.ts

0% Statements 0/73
0% Branches 0/19
0% Functions 0/16
0% Lines 0/67

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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182                                                                                                                                                                                                                                                                                                                                                                           
import {
  ActorNormalized,
  SceneNormalized,
  TriggerNormalized,
} from "./entitiesTypes";
import { actorName, sceneName, triggerName } from "./entitiesHelpers";
 
type Entity = {
  id: string;
  name: string;
};
 
export type SceneNavigatorItem = {
  id: string;
  name: string;
  filename: string;
  nestLevel?: number;
  labelColor?: string;
} & (
  | {
      type: "folder";
    }
  | {
      type: "scene";
      scene: SceneNormalized;
    }
  | {
      type: "actor";
      actor: ActorNormalized;
      sceneId: string;
    }
  | {
      type: "trigger";
      trigger: TriggerNormalized;
      sceneId: string;
    }
);
 
const collator = new Intl.Collator(undefined, {
  numeric: true,
  sensitivity: "base",
});
 
const sortByName = (a: Entity, b: Entity) => {
  return collator.compare(a.name, b.name);
};
 
export const sceneParentFolders = (scene: SceneNormalized): string[] => {
  const parts = scene.name.split(/[/\\]/).slice(0, -1);
  const folders: string[] = [];
  while (parts.length > 0) {
    folders.push(parts.join("/"));
    folders.push(parts.join("\\"));
    parts.pop();
  }
  return folders;
};
 
export const scenesInFolder = (
  folder: string,
  scenes: SceneNormalized[]
): SceneNormalized[] => {
  const regex = new RegExp("^" + folder.split(/[/\\]/).join("[/\\\\]"));
  return scenes.filter((scene) => scene.name.match(regex));
};
 
export const buildSceneNavigatorItems = (
  scenes: SceneNormalized[],
  actorsLookup: Record<string, ActorNormalized>,
  triggersLookup: Record<string, TriggerNormalized>,
  openFolders: string[],
  searchTerm: string
): SceneNavigatorItem[] => {
  const result: SceneNavigatorItem[] = [];
  const uniqueFolders = new Set<string>();
 
  const isVisible = (filename: string, nestLevel?: number): boolean => {
    Iif (searchTerm.length > 0) return true;
    Iif (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);
    });
  };
 
  const addScene = (scene: SceneNormalized, nestLevel: number) => {
    result.push({
      id: scene.id,
      type: "scene",
      name: scene.name,
      filename: scene.name.replace(/.*[/\\]/, ""),
      nestLevel,
      labelColor: scene.labelColor,
      scene,
    });
    Iif (!openFolders.includes(scene.id)) {
      return;
    }
    scene.actors.forEach((actorId) => {
      const actor = actorsLookup[actorId];
      Iif (actor) {
        const name = actorName(actor, scene.actors.indexOf(actorId));
        result.push({
          id: actor.id,
          type: "actor",
          name,
          filename: name,
          nestLevel: nestLevel + 1,
          actor,
          sceneId: scene.id,
        });
      }
    });
 
    scene.triggers.forEach((triggerId) => {
      const trigger = triggersLookup[triggerId];
      Iif (trigger) {
        const name = triggerName(trigger, scene.triggers.indexOf(triggerId));
        result.push({
          id: trigger.id,
          type: "trigger",
          name,
          filename: name,
          nestLevel: nestLevel + 1,
          trigger,
          sceneId: scene.id,
        });
      }
    });
  };
 
  Iif (searchTerm.length > 0) {
    const searchTermUpperCase = searchTerm.toLocaleUpperCase();
    scenes
      .map((scene, index) => ({ ...scene, name: sceneName(scene, index) }))
      .filter((s) => s.name.toLocaleUpperCase().includes(searchTermUpperCase))
      .sort(sortByName)
      .forEach((scene) => {
        addScene(scene, 0);
      });
    return result;
  }
 
  scenes
    .map((scene, index) => ({ ...scene, name: sceneName(scene, index) }))
    .sort(sortByName)
    .forEach((scene) => {
      const path = scene.name;
      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;
          Iif (!isVisible(currentPath, nestLevel)) {
            return;
          }
          addScene(scene, nestLevel);
        } else Iif (!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;
};