Skip to content

Commit fad68fb

Browse files
author
Hugo Marcellin
committed
Merge main
2 parents 541cc37 + 14e64f2 commit fad68fb

File tree

4 files changed

+70
-11
lines changed

4 files changed

+70
-11
lines changed

src/components/search/search-bar.tsx

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,24 @@ import {
1717
import { Autocomplete, TextField } from '@mui/material';
1818
import { searchElementsInfos } from '../../utils/rest-api';
1919
import {
20+
ElementType,
2021
useDebounce,
2122
useSnackMessage,
22-
fetchDirectoryContent,
23-
ElementAttributes,
24-
ElementType,
23+
fetchDirectoryContent,
24+
ElementAttributes,
2525
} from '@gridsuite/commons-ui';
2626
import { Search } from '@mui/icons-material';
2727
import { useDispatch, useSelector } from 'react-redux';
2828
import { setSelectedDirectory, setTreeData } from '../../redux/actions';
2929
import { updatedTree } from '../tree-views-container';
3030
import { useIntl } from 'react-intl';
3131
import { SearchItem } from './search-item';
32-
import { ITreeData, ReduxState } from '../../redux/reducer.type';
32+
import {
33+
IDirectory,
34+
IElement,
35+
ITreeData,
36+
ReduxState,
37+
} from '../../redux/reducer.type';
3338
import { UUID } from 'crypto';
3439

3540
export const SEARCH_FETCH_TIMEOUT_MILLIS = 1000; // 1 second
@@ -116,7 +121,7 @@ export const SearchBar: FunctionComponent<SearchBarProps> = ({ inputRef }) => {
116121
);
117122

118123
const updateMapData = useCallback(
119-
(nodeId: string, children: ElementAttributes[]) => {
124+
(nodeId: string, children: IDirectory[]) => {
120125
if (!treeDataRef.current) {
121126
return;
122127
}
@@ -157,8 +162,13 @@ export const SearchBar: FunctionComponent<SearchBarProps> = ({ inputRef }) => {
157162
const elementUuidPath = matchingElement?.pathUuid.reverse();
158163
const promises = elementUuidPath.map((e: string) => {
159164
return fetchDirectoryContent(e as UUID)
160-
.then((res) => {
161-
updateMapData(e, res);
165+
.then((res: IElement[]) => {
166+
updateMapData(
167+
e,
168+
res.filter(
169+
(res) => res.type === ElementType.DIRECTORY
170+
) as IDirectory[]
171+
);
162172
})
163173
.catch((error) =>
164174
snackError({

src/components/search/search-item.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ export const HighlightedText: FunctionComponent<HighlightedTextProps> = ({
5353
text,
5454
highlight,
5555
}) => {
56-
const parts = text.split(new RegExp(`(${highlight})`, 'gi'));
56+
const escapedHighlight = highlight.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
57+
const parts = text.split(new RegExp(`(${escapedHighlight})`, 'gi'));
5758
return (
5859
<span>
5960
{parts.map((part, i) =>

src/redux/reducer.js

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import {
3939
USER_VALIDATION_ERROR,
4040
RESET_AUTHENTICATION_ROUTER_ERROR,
4141
SHOW_AUTH_INFO_LOGIN,
42+
ElementType,
4243
} from '@gridsuite/commons-ui';
4344
import { PARAM_LANGUAGE, PARAM_THEME } from '../utils/config-params';
4445

@@ -73,6 +74,12 @@ const initialState = {
7374
...paramsInitialState,
7475
};
7576

77+
const filterFromObject = (objectToFilter, filterMethod) => {
78+
return Object.fromEntries(
79+
Object.entries(objectToFilter).filter(filterMethod)
80+
);
81+
};
82+
7683
export const reducer = createReducer(initialState, (builder) => {
7784
builder.addCase(SELECT_THEME, (state, action) => {
7885
state.theme = action.theme;
@@ -160,7 +167,29 @@ export const reducer = createReducer(initialState, (builder) => {
160167
});
161168

162169
builder.addCase(TREE_DATA, (state, action) => {
163-
state.treeData = action.treeData;
170+
//TODO: remove those filters below when this file has been migrated to Typescript
171+
// it's only here to prevent regressions due to javascript not checking types
172+
173+
// filtering non DIRECTORY elements from action.treeData
174+
// used to prevent non DIRECTORY elements from appearing in left menu
175+
const filteredTreeDataRootDirectories =
176+
action.treeData.rootDirectories.filter(
177+
(element) => element.type === ElementType.DIRECTORY
178+
);
179+
180+
// action.treeData.mapData is an object looking like this : {<elementId1>: <element1>, <elementId2>: <element2>}
181+
// Object.entries changes it to an array looking like [[elementId1, element1], [elementId2, element2]], in order to make it easier to filter
182+
// Object.fromEntries will turn [[elementId1, element1], [elementId2, element2]] back to {<elementId1>: <element1>, <elementId2>: <element2>} which is the initial form
183+
const filteredTreeDataMapData = filterFromObject(
184+
action.treeData.mapData,
185+
([elementId, element]) => element.type === ElementType.DIRECTORY
186+
);
187+
188+
state.treeData = {
189+
...action.treeData,
190+
rootDirectories: filteredTreeDataRootDirectories,
191+
mapData: filteredTreeDataMapData,
192+
};
164193
});
165194

166195
builder.addCase(SELECTION_FOR_COPY, (state, action) => {

src/redux/reducer.type.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* License, v. 2.0. If a copy of the MPL was not distributed with this
55
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
66
*/
7+
import { ElementType } from '@gridsuite/commons-ui';
78
import { UUID } from 'crypto';
89
import { ElementAttributes } from '@gridsuite/commons-ui';
910

@@ -23,9 +24,27 @@ interface IUser {
2324
expires_at: number;
2425
}
2526

27+
export interface IElement {
28+
elementUuid: UUID;
29+
elementName: string;
30+
type: ElementType;
31+
owner: string;
32+
subdirectoriesCount: number;
33+
creationDate: string;
34+
lastModificationDate: string;
35+
lastModifiedBy: string;
36+
children: any[];
37+
parentUuid: null | UUID;
38+
}
39+
40+
// IDirectory is exactly an IElement, with a specific type value
41+
export type IDirectory = IElement & {
42+
type: ElementType.DIRECTORY;
43+
};
44+
2645
export interface ITreeData {
27-
rootDirectories: ElementAttributes[];
28-
mapData: Record<string, any>;
46+
rootDirectories: IDirectory[];
47+
mapData: Record<string, IDirectory>;
2948
}
3049

3150
export interface ReduxState {

0 commit comments

Comments
 (0)