/
Search.tsx
162 lines (150 loc) Β· 4.47 KB
/
Search.tsx
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
/* eslint-disable verdaccio/jsx-spread */
import SearchMui from '@mui/icons-material/Search';
import debounce from 'lodash/debounce';
import React, { useCallback } from 'react';
import { useTranslation } from 'react-i18next';
import { useDispatch, useSelector } from 'react-redux';
import { RouteComponentProps, withRouter } from 'react-router';
import { SearchResultWeb } from '@verdaccio/types';
import { Dispatch, RootState, useConfig } from '../../';
import AutoComplete from './AutoComplete';
import SearchItem from './SearchItem';
import { StyledInputAdornment, StyledTextField } from './styles';
const CONSTANTS = {
API_DELAY: 300,
ABORT_ERROR: 'AbortError',
};
const Search: React.FC<RouteComponentProps> = ({ history }) => {
const { t } = useTranslation();
const {
configOptions: { flags },
} = useConfig();
const searchRemote = flags?.searchRemote || false;
const { suggestions } = useSelector((state: RootState) => state.search);
const isLoading = useSelector((state: RootState) => state?.loading?.models.search);
const dispatch = useDispatch<Dispatch>();
/**
* Cancel all the requests which are in pending state.
*/
const cancelAllSearchRequests = useCallback(() => {
dispatch.search.clearRequestQueue();
dispatch.search.saveSearch({ suggestions: [] });
}, [dispatch]);
/**
* As user focuses out from input, we cancel all the request from requestList
* and set the API state parameters to default boolean values.
*/
const handleOnBlur = useCallback(
(event: React.SyntheticEvent) => {
// stops event bubbling
event.stopPropagation();
cancelAllSearchRequests();
},
[cancelAllSearchRequests]
);
/**
* When an user select any package by clicking or pressing return key.
*/
const handleClickSearch = useCallback(
(event: React.SyntheticEvent, value: SearchResultWeb, reason: string): void => {
// stops event bubbling
event.stopPropagation();
switch (reason) {
case 'selectOption':
if (searchRemote) {
// TODO: check this part
// @ts-ignore
history.push(`/-/web/detail/${value.package.name}`);
} else {
history.push(`/-/web/detail/${value.name}`);
}
break;
}
},
[history, searchRemote]
);
/**
* Fetch packages from API.
* For AbortController see: https://developer.mozilla.org/en-US/docs/Web/API/AbortController
*/
const handleFetchPackages = useCallback(
({ value }: { value: string }) => {
if (value?.trim() !== '') {
dispatch.search.clearRequestQueue();
dispatch.search.getSuggestions({ value });
}
},
[dispatch]
);
const renderInput = (params) => {
return (
<StyledTextField
{...params}
InputProps={{
...params.InputProps,
startAdornment: (
<StyledInputAdornment position="start">
<SearchMui />
</StyledInputAdornment>
),
}}
label=""
placeholder={t('search.packages')}
variant="standard"
/>
);
};
const getOptionLabel = () => {
if (searchRemote) {
return (option) => {
return option?.package?.name ?? '';
};
} else {
return (option) => {
return option?.name;
};
}
};
const renderOption = (props, option) => {
if (searchRemote) {
const item: SearchResultWeb = option.package;
const isPrivate = option?.verdaccioPrivate;
const isCached = option?.verdaccioPkgCached;
const isRemote = !isCached && !isPrivate;
return (
<SearchItem
{...props}
description={item?.description}
isCached={isCached}
isPrivate={isPrivate}
isRemote={isRemote}
name={item?.name}
version={item?.version}
/>
);
} else {
return (
<SearchItem
{...props}
description={option?.description}
name={option?.name}
version={option?.version}
/>
);
}
};
return (
<AutoComplete
getOptionLabel={getOptionLabel()}
onCleanSuggestions={handleOnBlur}
onSelectItem={handleClickSearch}
onSuggestionsFetch={debounce(handleFetchPackages, CONSTANTS.API_DELAY)}
placeholder={t('search.packages')}
renderInput={renderInput}
renderOption={renderOption}
suggestions={suggestions}
suggestionsLoading={isLoading}
/>
);
};
export default withRouter(Search);