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 | import React, { FC, useCallback, useEffect, useState } from "react"; import { constantSelectors } from "store/features/entities/entitiesState"; import { FlatList } from "ui/lists/FlatList"; import editorActions from "store/features/editor/editorActions"; import { EntityListItem } from "ui/lists/EntityListItem"; import { useAppDispatch, useAppSelector } from "store/hooks"; import { MenuDivider, MenuItem } from "ui/menu/Menu"; import l10n from "shared/lib/lang/l10n"; import entitiesActions from "store/features/entities/entitiesActions"; import { Constant } from "shared/lib/resources/types"; import { constantName } from "shared/lib/entities/entitiesHelpers"; import { FlexRow, FlexGrow } from "ui/spacing/Spacing"; import styled from "styled-components"; interface NavigatorConstantsProps { height: number; searchTerm: string; } interface NavigatorItem { id: string; name: string; } const constantToNavigatorItem = ( constant: Constant, index: number ): NavigatorItem => ({ id: constant.id, name: constantName(constant, index), }); const collator = new Intl.Collator(undefined, { numeric: true, sensitivity: "base", }); const sortByName = (a: NavigatorItem, b: NavigatorItem) => { return collator.compare(a.name, b.name); }; const ConstantValueLabel = styled.span` opacity: 0.5; `; export const NavigatorConstants: FC<NavigatorConstantsProps> = ({ height, searchTerm, }) => { const constants = useAppSelector(constantSelectors.selectAll); const constantsLookup = useAppSelector(constantSelectors.selectEntities); const [items, setItems] = useState<NavigatorItem[]>([]); const entityId = useAppSelector((state) => state.editor.entityId); const editorType = useAppSelector((state) => state.editor.type); const selectedId = editorType === "constant" ? entityId : ""; const dispatch = useAppDispatch(); useEffect(() => { const searchTermUpperCase = searchTerm.toLocaleUpperCase(); setItems( constants .map((constant, index) => constantToNavigatorItem(constant, index)) .filter( (value) => searchTermUpperCase.length === 0 || value.name.toLocaleUpperCase().includes(searchTermUpperCase) ) .sort(sortByName) ); }, [searchTerm, constants]); const setSelectedId = (id: string) => { dispatch(editorActions.selectConstant({ constantId: 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.renameConstant({ constantId: 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>, <MenuDivider key="div-delete" />, <MenuItem key="delete" onClick={() => dispatch(entitiesActions.removeConstant({ constantId: item.id })) } > {l10n("MENU_DELETE_CONSTANT")} </MenuItem>, ]; }, [dispatch] ); const renderLabel = useCallback( (item: NavigatorItem) => { return ( <FlexRow> <FlexGrow style={{ overflow: "hidden", flexShrink: 0 }}> {item.name} </FlexGrow> <ConstantValueLabel> {constantsLookup[item.id]?.value ?? 0} </ConstantValueLabel> </FlexRow> ); }, [constantsLookup] ); return ( <FlatList selectedId={selectedId} items={items} setSelectedId={setSelectedId} height={height} onKeyDown={listenForRenameStart} children={({ item }) => ( <EntityListItem type="constant" item={item} rename={renameId === item.id} onRename={onRenameComplete} onRenameCancel={onRenameCancel} renderContextMenu={renderContextMenu} renderLabel={renderLabel} /> )} /> ); }; |