All files / src/components/editors/trigger TriggerEditorScripts.tsx

0% Statements 0/49
0% Branches 0/19
0% Functions 0/13
0% Lines 0/47

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                                                                                                                                                                                                                                                                                                                                                                     
import React, { useCallback, useMemo, useState } from "react";
import ScriptEditor from "components/script/ScriptEditor";
import ScriptEditorDropdownButton from "components/script/ScriptEditorDropdownButton";
import { sceneSelectors } from "store/features/entities/entitiesState";
import editorActions from "store/features/editor/editorActions";
import {
  ScriptEventNormalized,
  TriggerNormalized,
} from "shared/lib/entities/entitiesTypes";
import { StickyTabs, TabBar } from "ui/tabs/Tabs";
import { Button } from "ui/buttons/Button";
import { LockIcon, LockOpenIcon } from "ui/icons/Icons";
import { ScriptEditorContext } from "components/script/ScriptEditorContext";
import l10n from "shared/lib/lang/l10n";
import { useAppDispatch, useAppSelector } from "store/hooks";
import { ScriptEditorCtx } from "shared/lib/scripts/context";
 
interface TriggerEditorScriptsProps {
  trigger: TriggerNormalized;
  sceneId: string;
}
 
interface ScriptHandler {
  value: ScriptEventNormalized[];
  onChange: (newValue: ScriptEventNormalized[]) => void;
}
 
interface ScriptHandlers {
  trigger: ScriptHandler;
  leave: ScriptHandler;
}
 
type TriggerScriptKey = "script" | "leaveScript";
 
type DefaultTab = "trigger" | "leave";
type PointNClickTab = "trigger";
 
const getScriptKey = (tab: DefaultTab): TriggerScriptKey => {
  Iif (tab === "trigger") {
    return "script";
  }
  Iif (tab === "leave") {
    return "leaveScript";
  }
  return "script";
};
 
export const TriggerEditorScripts = ({
  trigger,
  sceneId,
}: TriggerEditorScriptsProps) => {
  const scene = useAppSelector((state) =>
    sceneSelectors.selectById(state, sceneId)
  );
 
  const lastScriptTab = useAppSelector(
    (state) => state.editor.lastScriptTabTrigger
  );
 
  const scriptTabs: Record<DefaultTab, string> = useMemo(
    () => ({
      trigger: l10n("SIDEBAR_ON_ENTER"),
      leave: l10n("SIDEBAR_ON_LEAVE"),
    }),
    []
  );
 
  const pointNClickScriptTabs: Record<PointNClickTab, string> = useMemo(
    () => ({
      trigger: l10n("SIDEBAR_ON_INTERACT"),
    }),
    []
  );
 
  const tabs = useMemo(() => Object.keys(scriptTabs), [scriptTabs]);
 
  const initialTab = tabs.includes(lastScriptTab) ? lastScriptTab : tabs[0];
 
  const [scriptMode, setScriptMode] = useState<keyof ScriptHandlers>(
    initialTab as keyof ScriptHandlers
  );
 
  const onChangeScriptMode = (mode: keyof ScriptHandlers) => {
    setScriptMode(mode);
    dispatch(editorActions.setScriptTabTrigger(mode));
  };
 
  const scriptKey = getScriptKey(scriptMode);
 
  const lockScriptEditor = useAppSelector(
    (state) => state.editor.lockScriptEditor
  );
 
  const dispatch = useAppDispatch();
 
  const onToggleLockScriptEditor = useCallback(() => {
    dispatch(editorActions.setLockScriptEditor(!lockScriptEditor));
  }, [dispatch, lockScriptEditor]);
 
  const lockButton = useMemo(
    () => (
      <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>
    ),
    [lockScriptEditor, onToggleLockScriptEditor]
  );
 
  const scriptButton = useMemo(
    () =>
      trigger && (
        <ScriptEditorDropdownButton
          value={trigger[scriptKey]}
          type="trigger"
          entityId={trigger.id}
          scriptKey={scriptKey}
        />
      ),
    [scriptKey, trigger]
  );
 
  const scriptCtx: ScriptEditorCtx = useMemo(
    () => ({
      type: "entity",
      entityType: "trigger",
      entityId: trigger.id,
      sceneId,
      scriptKey,
    }),
    [trigger.id, sceneId, scriptKey]
  );
 
  Iif (!scene || !trigger) {
    return <div />;
  }
 
  return (
    <>
      <StickyTabs>
        {scene.type === "POINTNCLICK" ? (
          <TabBar
            values={pointNClickScriptTabs}
            buttons={
              <>
                {lockButton}
                {scriptButton}
              </>
            }
          />
        ) : (
          <TabBar
            value={scriptMode}
            values={scriptTabs}
            onChange={onChangeScriptMode}
            buttons={
              <>
                {lockButton}
                {scriptButton}
              </>
            }
          />
        )}
      </StickyTabs>
      <ScriptEditorContext.Provider value={scriptCtx}>
        <ScriptEditor value={trigger[scriptKey] || []} />
      </ScriptEditorContext.Provider>
    </>
  );
};