All files / src/components/script/fields VariableFieldInput.tsx

96.15% Statements 25/26
73.33% Branches 33/45
80% Functions 4/5
95.83% Lines 23/24

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 1182x 2x 2x 2x           2x       2x         2x 2x 2x                     2x               3x   3x 3x 3x   3x 28x 3x   3x       3x       3x                   1x 3x   1x                                                                                              
import React from "react";
import { VariableSelect } from "components/forms/VariableSelect";
import { Alert } from "ui/alerts/Alert";
import {
  IndexedVariableInputGroup,
  VariableInputGroup,
  VariableIndexSelect,
} from "components/forms/VariableIndexSelect";
import type { ScriptEventFieldSchema } from "shared/lib/entities/entitiesTypes";
import {
  isScriptValue,
  isScriptValueVariable,
} from "shared/lib/scriptValue/types";
import {
  allowedVariableTypesForFieldType,
  variableTypeAllowsIndex,
  variableValueForType,
} from "./fieldHelpers";
import { useVariableFieldContext } from "./useVariableFieldContext";
import l10n from "shared/lib/lang/l10n";
import { FixedSpacer } from "ui/spacing/Spacing";
 
interface VariableFieldInputProps {
  id: string;
  entityId: string;
  field: ScriptEventFieldSchema;
  value: unknown;
  allowRename: boolean;
  onChange: (value: unknown) => void;
}
 
export const VariableFieldInput = ({
  id,
  entityId,
  field,
  value,
  allowRename,
  onChange,
}: VariableFieldInputProps) => {
  const fieldVariableType = field.variableType ?? "any";
  const allowedVariableTypes =
    allowedVariableTypesForFieldType(fieldVariableType);
  const { candidates } = useVariableFieldContext(entityId);
  const variableValue = isScriptValueVariable(value) ? value : undefined;
  const variableId =
    variableValue?.value ?? (typeof value === "string" ? value : "");
  const selectedCandidate = candidates.find(({ id }) => id === variableId);
  const isArray = selectedCandidate?.type === "array";
  const isArrayTooSmall =
    isArray &&
    selectedCandidate.length !== undefined &&
    field.arrayLength !== undefined &&
    selectedCandidate.length < field.arrayLength;
  const variableIndex = isScriptValue(variableValue?.index)
    ? variableValue.index
    : { type: "number" as const, value: 0 };
 
  return (
    <>
      <IndexedVariableInputGroup>
        <VariableInputGroup>
          <VariableSelect
            name={id}
            value={variableId}
            entityId={entityId}
            allowedVariableTypes={allowedVariableTypes}
            onChange={(newVariableId) => {
              const candidate = candidates.find(
                ({ id }) => id === newVariableId,
              );
              onChange(
                variableValueForType(
                  fieldVariableType,
                  newVariableId,
                  variableIndex,
                  candidate?.type === "array",
                ),
              );
            }}
            allowRename={allowRename}
          />
        </VariableInputGroup>
        {variableTypeAllowsIndex(fieldVariableType) && isArray && (
          <VariableIndexSelect
            name={`${id}_index`}
            entityId={entityId}
            value={variableIndex}
            max={
              selectedCandidate?.length
                ? selectedCandidate.length - 1
                : undefined
            }
            onChange={(newIndex) => {
              onChange({
                type: "variable",
                value: variableId,
                index: newIndex,
              });
            }}
          />
        )}
      </IndexedVariableInputGroup>
 
      {isArrayTooSmall && (
        <>
          <FixedSpacer height={5} />
          <Alert variant="warning">
            {l10n("WARNING_ARRAY_TOO_SMALL", {
              expectedLength: field.arrayLength,
              actualLength: selectedCandidate.length,
            })}
          </Alert>
        </>
      )}
    </>
  );
};