All files / src/components/editors/prefab TriggerPrefabUsesList.tsx

0% Statements 0/44
0% Branches 0/13
0% Functions 0/4
0% Lines 0/42

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                                                                                                                                                                                                                                                                                                                             
import React, { FC, useMemo } from "react";
import {
  triggerSelectors,
  sceneSelectors,
} from "store/features/entities/entitiesState";
import editorActions from "store/features/editor/editorActions";
import { FlatList } from "ui/lists/FlatList";
import { EntityListItem } from "ui/lists/EntityListItem";
import useDimensions from "react-cool-dimensions";
import styled from "styled-components";
import { SplitPaneHeader } from "ui/splitpane/SplitPaneHeader";
import l10n from "shared/lib/lang/l10n";
import { useAppDispatch, useAppSelector } from "store/hooks";
import { Button } from "ui/buttons/Button";
import { triggerName, sceneName } from "shared/lib/entities/entitiesHelpers";
import {
  TriggerNormalized,
  SceneNormalized,
} from "shared/lib/entities/entitiesTypes";
 
interface TriggerPrefabUsesListProps {
  id: string;
  onClose?: () => void;
}
 
const UsesWrapper = styled.div`
  position: absolute;
  top: 38px;
  left: 0;
  bottom: 0;
  right: 0;
  width: 100%;
`;
 
const UseMessage = styled.div`
  padding: 5px 10px;
  font-size: 11px;
`;
 
export type TriggerPrefabUse = {
  id: string;
  name: string;
} & (
  | {
      type: "scene";
      sceneId: string;
      scene: SceneNormalized;
      sceneIndex: number;
    }
  | {
      type: "trigger";
      trigger: TriggerNormalized;
      triggerIndex: number;
      sceneId: string;
      scene: SceneNormalized;
      sceneIndex: number;
    }
);
 
export const TriggerPrefabUsesList: FC<TriggerPrefabUsesListProps> = ({
  id,
  onClose,
}) => {
  const { observe, height } = useDimensions();
  const scenes = useAppSelector(sceneSelectors.selectAll);
  const triggersLookup = useAppSelector(triggerSelectors.selectEntities);
 
  const prefabUses: TriggerPrefabUse[] = useMemo(() => {
    const uses: TriggerPrefabUse[] = [];
    for (let sceneIndex = 0; sceneIndex < scenes.length; sceneIndex++) {
      const scene = scenes[sceneIndex];
      const sceneUses: TriggerPrefabUse[] = [];
 
      for (
        let triggerIndex = 0;
        triggerIndex < scene.triggers.length;
        triggerIndex++
      ) {
        const triggerId = scene.triggers[triggerIndex];
        const trigger = triggersLookup[triggerId];
        Iif (trigger?.prefabId === id) {
          const numChanges = Object.keys(trigger.prefabScriptOverrides).length;
          sceneUses.push({
            type: "trigger",
            id: trigger.id,
            name:
              numChanges === 0
                ? triggerName(trigger, triggerIndex)
                : `${triggerName(trigger, triggerIndex)} (+${l10n(
                    numChanges === 1 ? "FIELD_N_CHANGE" : "FIELD_N_CHANGES",
                    { n: numChanges }
                  )})`,
            trigger,
            triggerIndex,
            sceneId: scene.id,
            scene,
            sceneIndex: 0,
          });
        }
      }
 
      Iif (sceneUses.length > 0) {
        uses.push({
          type: "scene",
          id: scene.id,
          name: sceneName(scene, sceneIndex),
          sceneId: scene.id,
          scene,
          sceneIndex,
        });
        uses.push(...sceneUses);
      }
    }
    return uses;
  }, [triggersLookup, id, scenes]);
 
  const dispatch = useAppDispatch();
 
  const setSelectedId = (id: string, item: TriggerPrefabUse) => {
    dispatch(
      editorActions.selectTrigger({ triggerId: id, sceneId: item.sceneId })
    );
    dispatch(editorActions.setFocusSceneId(item.sceneId));
  };
 
  return (
    <UsesWrapper ref={observe}>
      <SplitPaneHeader
        collapsed={false}
        onToggle={onClose}
        buttons={
          <Button variant="transparent" size="small" onClick={onClose}>
            {l10n("MENU_EDIT_PREFAB")}
          </Button>
        }
      >
        {l10n("SIDEBAR_PREFAB_USES")}
      </SplitPaneHeader>
      {prefabUses.length > 0 ? (
        <FlatList
          items={prefabUses}
          height={height - 30}
          setSelectedId={setSelectedId}
          children={({ item }) => {
            Iif (item.type === "scene") {
              return <EntityListItem item={item} type={item.type} />;
            }
            return (
              <EntityListItem item={item} type={"trigger"} nestLevel={1} />
            );
          }}
        />
      ) : (
        <UseMessage>{l10n("FIELD_PREFAB_NOT_USED")}</UseMessage>
      )}
    </UsesWrapper>
  );
};