All files / src/components/world NavigatorVariables.tsx

0% Statements 0/49
0% Branches 0/12
0% Functions 0/16
0% Lines 0/46

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                                                                                                                                                                                                                                                                       
import React, { FC, useCallback, useEffect, useState } from "react";
import { variableSelectors } from "store/features/entities/entitiesState";
import { FlatList } from "ui/lists/FlatList";
import editorActions from "store/features/editor/editorActions";
import { Variable } from "shared/lib/entities/entitiesTypes";
import { allVariables } from "renderer/lib/variables";
import { EntityListItem } from "ui/lists/EntityListItem";
import { globalVariableDefaultName } from "shared/lib/variables/variableNames";
import { useAppDispatch, useAppSelector } from "store/hooks";
import { MenuItem } from "ui/menu/Menu";
import l10n from "shared/lib/lang/l10n";
import entitiesActions from "store/features/entities/entitiesActions";
 
interface NavigatorVariablesProps {
  height: number;
  searchTerm: string;
}
 
interface NavigatorItem {
  id: string;
  name: string;
}
 
const variableToNavigatorItem = (
  variable: Variable | undefined,
  variableCode: string
): NavigatorItem => ({
  id: variableCode,
  name: variable?.name
    ? variable.name
    : globalVariableDefaultName(variableCode),
});
 
const collator = new Intl.Collator(undefined, {
  numeric: true,
  sensitivity: "base",
});
 
const sortByName = (a: NavigatorItem, b: NavigatorItem) => {
  return collator.compare(a.name, b.name);
};
 
export const NavigatorVariables: FC<NavigatorVariablesProps> = ({
  height,
  searchTerm,
}) => {
  const [items, setItems] = useState<NavigatorItem[]>([]);
  const variablesLookup = useAppSelector((state) =>
    variableSelectors.selectEntities(state)
  );
  const entityId = useAppSelector((state) => state.editor.entityId);
  const editorType = useAppSelector((state) => state.editor.type);
  const selectedId = editorType === "variable" ? entityId : "";
  const dispatch = useAppDispatch();
 
  useEffect(() => {
    const searchTermUpperCase = searchTerm.toLocaleUpperCase();
    setItems(
      allVariables
        .map((value) => variableToNavigatorItem(variablesLookup[value], value))
        .filter(
          (value) =>
            searchTermUpperCase.length === 0 ||
            value.name.toLocaleUpperCase().includes(searchTermUpperCase)
        )
        .sort(sortByName)
    );
  }, [searchTerm, variablesLookup]);
 
  const setSelectedId = (id: string) => {
    dispatch(editorActions.selectVariable({ variableId: id }));
  };
 
  const [renameId, setRenameId] = useState("");
 
  const listenForRenameStart = useCallback(
    (e: KeyboardEvent) => {
      Iif (e.key === "Enter") {
        setRenameId(selectedId);
      }
    },
    [selectedId]
  );
 
  const onRenameComplete = useCallback(
    (name: string) => {
      Iif (renameId) {
        dispatch(
          entitiesActions.renameVariable({
            variableId: renameId,
            name,
          })
        );
      }
      setRenameId("");
    },
    [dispatch, renameId]
  );
 
  const onRenameCancel = useCallback(() => {
    setRenameId("");
  }, []);
 
  const renderContextMenu = useCallback((item: NavigatorItem) => {
    return [
      <MenuItem key="rename" onClick={() => setRenameId(item.id)}>
        {l10n("FIELD_RENAME")}
      </MenuItem>,
    ];
  }, []);
 
  return (
    <FlatList
      selectedId={selectedId}
      items={items}
      setSelectedId={setSelectedId}
      height={height}
      onKeyDown={listenForRenameStart}
      children={({ item }) => (
        <EntityListItem
          type="variable"
          item={item}
          rename={renameId === item.id}
          onRename={onRenameComplete}
          onRenameCancel={onRenameCancel}
          renderContextMenu={renderContextMenu}
        />
      )}
    />
  );
};