|
| 1 | +/** |
| 2 | + * Copyright (c) 2024, RTE (http://www.rte-france.com) |
| 3 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 4 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 6 | + */ |
| 7 | +import React, { useCallback, useEffect, useRef, useState } from 'react'; |
| 8 | +import { Autocomplete, TextField } from '@mui/material'; |
| 9 | +import { |
| 10 | + fetchDirectoryContent, |
| 11 | + searchElementsInfos, |
| 12 | +} from '../../utils/rest-api'; |
| 13 | +import { useDebounce, useSnackMessage } from '@gridsuite/commons-ui'; |
| 14 | +import { Search } from '@mui/icons-material'; |
| 15 | +import { useDispatch, useSelector } from 'react-redux'; |
| 16 | +import { setSelectedDirectory, setTreeData } from '../../redux/actions'; |
| 17 | +import { updatedTree } from '../tree-views-container'; |
| 18 | +import { useIntl } from 'react-intl'; |
| 19 | +import SearchItem from './search-item'; |
| 20 | + |
| 21 | +export const SEARCH_FETCH_TIMEOUT_MILLIS = 1000; // 1 second |
| 22 | + |
| 23 | +export const SearchBar = ({ inputRef }) => { |
| 24 | + const dispatch = useDispatch(); |
| 25 | + const { snackError } = useSnackMessage(); |
| 26 | + const [elementsFound, setElementsFound] = useState([]); |
| 27 | + const [inputValue, onInputChange] = useState(''); |
| 28 | + const lastSearchTermRef = useRef(''); |
| 29 | + const [loading, setLoading] = useState(false); |
| 30 | + const treeData = useSelector((state) => state.treeData); |
| 31 | + const treeDataRef = useRef(); |
| 32 | + const intl = useIntl(); |
| 33 | + treeDataRef.current = treeData; |
| 34 | + const searchMatchingEquipments = useCallback( |
| 35 | + (searchTerm) => { |
| 36 | + lastSearchTermRef.current = searchTerm; |
| 37 | + searchTerm && |
| 38 | + searchElementsInfos(searchTerm) |
| 39 | + .then((infos) => { |
| 40 | + if (infos.length) { |
| 41 | + setElementsFound(infos); |
| 42 | + } else { |
| 43 | + setElementsFound([]); |
| 44 | + } |
| 45 | + }) |
| 46 | + .catch((error) => { |
| 47 | + snackError({ |
| 48 | + messageTxt: error.message, |
| 49 | + headerId: 'elementsSearchingError', |
| 50 | + }); |
| 51 | + }); |
| 52 | + }, |
| 53 | + [snackError] |
| 54 | + ); |
| 55 | + |
| 56 | + const debouncedSearchMatchingElements = useDebounce( |
| 57 | + searchMatchingEquipments, |
| 58 | + SEARCH_FETCH_TIMEOUT_MILLIS |
| 59 | + ); |
| 60 | + |
| 61 | + const handleChangeInput = useCallback( |
| 62 | + (searchTerm) => { |
| 63 | + onInputChange(searchTerm); |
| 64 | + searchTerm && setLoading(true); |
| 65 | + debouncedSearchMatchingElements(searchTerm); |
| 66 | + }, |
| 67 | + [debouncedSearchMatchingElements] |
| 68 | + ); |
| 69 | + |
| 70 | + useEffect(() => { |
| 71 | + elementsFound !== undefined && setLoading(false); |
| 72 | + }, [elementsFound]); |
| 73 | + |
| 74 | + const renderOptionItem = useCallback( |
| 75 | + (props, option) => { |
| 76 | + const matchingElement = elementsFound.find( |
| 77 | + (element) => element.id === option.id |
| 78 | + ); |
| 79 | + return ( |
| 80 | + <SearchItem |
| 81 | + matchingElement={matchingElement} |
| 82 | + inputValue={inputValue} |
| 83 | + {...props} |
| 84 | + /> |
| 85 | + ); |
| 86 | + }, |
| 87 | + [elementsFound, inputValue] |
| 88 | + ); |
| 89 | + |
| 90 | + const updateMapData = useCallback( |
| 91 | + (nodeId, children) => { |
| 92 | + let [newRootDirectories, newMapData] = updatedTree( |
| 93 | + treeDataRef.current.rootDirectories, |
| 94 | + treeDataRef.current.mapData, |
| 95 | + nodeId, |
| 96 | + children |
| 97 | + ); |
| 98 | + dispatch( |
| 99 | + setTreeData({ |
| 100 | + rootDirectories: newRootDirectories, |
| 101 | + mapData: newMapData, |
| 102 | + }) |
| 103 | + ); |
| 104 | + }, |
| 105 | + [dispatch] |
| 106 | + ); |
| 107 | + |
| 108 | + const handleDispatchDirectory = useCallback( |
| 109 | + (elementUuidPath) => { |
| 110 | + const selectedDirectory = |
| 111 | + treeDataRef.current.mapData[elementUuidPath]; |
| 112 | + |
| 113 | + dispatch(setSelectedDirectory(selectedDirectory)); |
| 114 | + }, |
| 115 | + [dispatch] |
| 116 | + ); |
| 117 | + |
| 118 | + const handleMatchingElement = useCallback( |
| 119 | + (data) => { |
| 120 | + if (data !== undefined) { |
| 121 | + const matchingElement = elementsFound.find( |
| 122 | + (element) => element === data |
| 123 | + ); |
| 124 | + const elementUuidPath = matchingElement?.pathUuid.reverse(); |
| 125 | + |
| 126 | + const promises = elementUuidPath.map((e) => { |
| 127 | + return fetchDirectoryContent(e) |
| 128 | + .then((res) => { |
| 129 | + updateMapData(e, res); |
| 130 | + }) |
| 131 | + .catch((error) => |
| 132 | + snackError({ |
| 133 | + messageTxt: error.message, |
| 134 | + headerId: 'pathRetrievingError', |
| 135 | + }) |
| 136 | + ); |
| 137 | + }); |
| 138 | + |
| 139 | + Promise.all(promises).then(() => { |
| 140 | + const lastElement = elementUuidPath.pop(); |
| 141 | + handleDispatchDirectory(lastElement); |
| 142 | + }); |
| 143 | + } |
| 144 | + }, |
| 145 | + [elementsFound, updateMapData, handleDispatchDirectory, snackError] |
| 146 | + ); |
| 147 | + |
| 148 | + return ( |
| 149 | + <> |
| 150 | + <Autocomplete |
| 151 | + sx={{ width: '50%', marginLeft: '14%' }} |
| 152 | + freeSolo |
| 153 | + size="small" |
| 154 | + disableClearable={false} |
| 155 | + forcePopupIcon={false} |
| 156 | + clearOnBlur |
| 157 | + autoHighlight={true} |
| 158 | + isOptionEqualToValue={(option, value) => option.id === value.id} |
| 159 | + inputValue={inputValue} |
| 160 | + onInputChange={(_, data) => handleChangeInput(data)} |
| 161 | + onChange={handleMatchingElement} |
| 162 | + key={(option) => option.id} |
| 163 | + options={loading ? [] : elementsFound} |
| 164 | + getOptionLabel={(option) => option.name} |
| 165 | + loading={loading} |
| 166 | + renderOption={renderOptionItem} |
| 167 | + renderInput={(params) => ( |
| 168 | + <TextField |
| 169 | + autoFocus={true} |
| 170 | + {...params} |
| 171 | + inputRef={inputRef} |
| 172 | + placeholder={intl.formatMessage({ |
| 173 | + id: 'searchPlaceholder', |
| 174 | + })} |
| 175 | + variant="outlined" |
| 176 | + InputProps={{ |
| 177 | + ...params.InputProps, |
| 178 | + startAdornment: ( |
| 179 | + <React.Fragment> |
| 180 | + <Search /> |
| 181 | + {params.InputProps.startAdornment} |
| 182 | + </React.Fragment> |
| 183 | + ), |
| 184 | + }} |
| 185 | + /> |
| 186 | + )} |
| 187 | + /> |
| 188 | + </> |
| 189 | + ); |
| 190 | +}; |
| 191 | + |
| 192 | +export default SearchBar; |
0 commit comments