All files / src/components/forms EngineFieldSelect.tsx

0% Statements 0/29
0% Branches 0/22
0% Functions 0/8
0% Lines 0/27

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                                                                                                                                                                                             
import React, { useEffect, useState } from "react";
import PropTypes from "prop-types";
import { useAppSelector } from "store/hooks";
import { Select, Option, OptGroup } from "ui/form/Select";
import l10n, { L10NKey } from "shared/lib/lang/l10n";
import { useGroupedEngineFields } from "components/settings/useGroupedEngineFields";
import { EngineFieldSchema } from "store/features/engine/engineState";
import { SingleValue } from "react-select";
import { Alert } from "ui/alerts/Alert";
import styled from "styled-components";
import { pxToSubpx, pxToSubpxVelPrecise } from "shared/lib/helpers/subpixels";
 
interface EngineFieldSelectProps {
  name: string;
  value?: string;
  onChange?: (newValue: string) => void;
  showUnitsWarning?: boolean;
}
 
const notEditable = (engineField: EngineFieldSchema) =>
  engineField.cType !== "define" && engineField.type !== "label";
 
const AlertWrapper = styled.div`
  margin-top: 5px;
`;
 
const EngineFieldSelect: React.FC<EngineFieldSelectProps> = ({
  name,
  value,
  onChange,
  showUnitsWarning,
}) => {
  const groupedFields = useGroupedEngineFields();
  const engineFields = useAppSelector((state) => state.engine.fields);
  const [options, setOptions] = useState<OptGroup[]>([]);
 
  useEffect(() => {
    setOptions(
      groupedFields.map((g) => ({
        label: l10n(g.name as L10NKey),
        options: g.fields.filter(notEditable).map((f) => ({
          value: f.key,
          label: l10n(f.label as L10NKey),
        })),
      })),
    );
  }, [groupedFields]);
 
  const currentField = engineFields.find((f) => f.key === value);
 
  const currentValue = currentField && {
    value: currentField.key,
    label: l10n(currentField.label as L10NKey),
  };
 
  return (
    <>
      <Select
        name={name}
        value={currentValue}
        options={options}
        onChange={(e: SingleValue<Option>) => {
          Iif (e && onChange) {
            onChange(e.value);
          }
        }}
      />
      {showUnitsWarning &&
        currentField?.editUnits &&
        currentField.editUnits !== "px" && (
          <AlertWrapper>
            <Alert variant="warning">
              {(currentField.editUnits === "subpx" ||
                currentField.editUnits === "subpxVel" ||
                currentField.editUnits === "subpxAcc") &&
                l10n("WARNING_FIELD_UNITS_SUBPX", { multiplier: pxToSubpx(1) })}
              {(currentField.editUnits === "subpxVelPrecise" ||
                currentField.editUnits === "subpxAccPrecise") &&
                l10n("WARNING_FIELD_UNITS_SUBPX_PRECISE", {
                  multiplier: pxToSubpxVelPrecise(1),
                })}
            </Alert>
          </AlertWrapper>
        )}
    </>
  );
};
 
EngineFieldSelect.propTypes = {
  value: PropTypes.string,
  onChange: PropTypes.func,
};
 
export default EngineFieldSelect;