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

0% Statements 0/42
0% Branches 0/25
0% Functions 0/9
0% Lines 0/40

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                                                                                                                                                                                                                                                                                                     
import React, { FC, useMemo, useState } from "react";
import editorActions from "store/features/editor/editorActions";
import {
  TriggerPrefabNormalized,
  ScriptEventNormalized,
  TriggerNormalized,
} from "shared/lib/entities/entitiesTypes";
import { LockIcon, LockOpenIcon } from "ui/icons/Icons";
import ScriptEditorDropdownButton from "components/script/ScriptEditorDropdownButton";
import ScriptEditor from "components/script/ScriptEditor";
import { StickyTabs, TabBar } from "ui/tabs/Tabs";
import { Button } from "ui/buttons/Button";
import { ScriptEditorContext } from "components/script/ScriptEditorContext";
import l10n from "shared/lib/lang/l10n";
import { useAppDispatch, useAppSelector } from "store/hooks";
import type { ScriptEditorCtx } from "shared/lib/scripts/context";
 
interface TriggerPrefabEditorScriptsProps {
  prefab: TriggerPrefabNormalized;
  trigger?: TriggerNormalized;
  sceneId?: string;
  isInstance?: boolean;
}
 
interface ScriptHandler {
  value: ScriptEventNormalized[];
  onChange: (newValue: ScriptEventNormalized[]) => void;
}
 
interface ScriptHandlers {
  trigger: ScriptHandler;
  leave: ScriptHandler;
}
 
type TriggerScriptKey = "script" | "leaveScript";
 
type DefaultTab = "trigger" | "leave";
 
const getScriptKey = (tab: DefaultTab): TriggerScriptKey => {
  Iif (tab === "trigger") {
    return "script";
  }
  Iif (tab === "leave") {
    return "leaveScript";
  }
  return "script";
};
 
export const TriggerPrefabEditorScripts: FC<TriggerPrefabEditorScriptsProps> =
  ({ prefab, trigger, sceneId, isInstance }) => {
    const lockScriptEditor = useAppSelector(
      (state) => state.editor.lockScriptEditor
    );
 
    const scriptTabs: Record<DefaultTab, string> = useMemo(
      () => ({
        trigger: l10n("SIDEBAR_ON_ENTER"),
        leave: l10n("SIDEBAR_ON_LEAVE"),
      }),
      []
    );
 
    const tabs = useMemo(() => Object.keys(scriptTabs), [scriptTabs]);
 
    const lastScriptTab = useAppSelector((state) => state.editor.lastScriptTab);
 
    const initialTab = tabs.includes(lastScriptTab) ? lastScriptTab : tabs[0];
 
    const [scriptMode, setScriptMode] = useState<keyof ScriptHandlers>(
      initialTab as keyof ScriptHandlers
    );
 
    const dispatch = useAppDispatch();
 
    const onChangeScriptMode = (mode: keyof ScriptHandlers) => {
      setScriptMode(mode);
      dispatch(editorActions.setScriptTab(mode));
    };
 
    const onToggleLockScriptEditor = () => {
      dispatch(editorActions.setLockScriptEditor(!lockScriptEditor));
    };
 
    const scriptKey = getScriptKey(scriptMode);
 
    const scriptCtx: ScriptEditorCtx = useMemo(
      () => ({
        type: "prefab",
        entityType: "triggerPrefab",
        entityId: prefab.id,
        sceneId: sceneId ?? "",
        scriptKey,
        instanceId: trigger?.id,
      }),
      [prefab.id, sceneId, scriptKey, trigger?.id]
    );
 
    Iif (!prefab) {
      return <div />;
    }
 
    const lockButton = (
      <Button
        size="small"
        variant={lockScriptEditor ? "primary" : "transparent"}
        onClick={onToggleLockScriptEditor}
        title={
          lockScriptEditor
            ? l10n("FIELD_UNLOCK_SCRIPT_EDITOR")
            : l10n("FIELD_LOCK_SCRIPT_EDITOR")
        }
      >
        {lockScriptEditor ? <LockIcon /> : <LockOpenIcon />}
      </Button>
    );
 
    const scriptButton = (
      <ScriptEditorDropdownButton
        value={prefab[scriptKey]}
        type="triggerPrefab"
        entityId={prefab.id}
        scriptKey={scriptKey}
      />
    );
 
    return (
      <>
        <StickyTabs top={isInstance ? 38 : undefined}>
          <TabBar
            value={scriptMode}
            values={scriptTabs}
            onChange={onChangeScriptMode}
            buttons={
              <>
                {lockButton}
                {scriptButton}
              </>
            }
          />
        </StickyTabs>
        <ScriptEditorContext.Provider value={scriptCtx}>
          <ScriptEditor value={prefab[scriptKey]} />
        </ScriptEditorContext.Provider>
      </>
    );
  };