|
1 | 1 | import * as React from 'react'; |
2 | | -import { |
3 | | - Button, |
4 | | - Divider, |
5 | | - EmptyState, |
6 | | - EmptyStateBody, |
7 | | - EmptyStateSecondaryActions, |
8 | | - Menu, |
9 | | - MenuContent, |
10 | | - MenuGroup, |
11 | | - MenuInput, |
12 | | - MenuItem, |
13 | | - MenuList, |
14 | | - TextInput, |
15 | | - Title, |
16 | | -} from '@patternfly/react-core'; |
17 | | -import fuzzysearch from 'fuzzysearch'; |
| 2 | +import { Menu, MenuContent, MenuItem, MenuList } from '@patternfly/react-core'; |
18 | 3 | import { useTranslation } from 'react-i18next'; |
19 | 4 | import { useActivePerspective } from '@console/dynamic-plugin-sdk'; |
20 | 5 | import { useActiveCluster, usePerspectiveExtension } from '@console/shared'; |
21 | 6 | import { ACM_PERSPECTIVE_ID } from '../../consts'; |
22 | 7 | import ClusterMenuToggle from './ClusterMenuToggle'; |
23 | 8 |
|
24 | | -const NoResults: React.FC<{ |
25 | | - onClear: (event: React.MouseEvent<HTMLButtonElement, MouseEvent>) => void; |
26 | | -}> = ({ onClear }) => { |
27 | | - const { t } = useTranslation(); |
28 | | - return ( |
29 | | - <EmptyState> |
30 | | - <Title size="md" headingLevel="h4"> |
31 | | - {t('console-app~No cluster found')} |
32 | | - </Title> |
33 | | - <EmptyStateBody>{t('console-app~No results match the filter criteria.')}</EmptyStateBody> |
34 | | - <EmptyStateSecondaryActions> |
35 | | - <Button variant="link" onClick={onClear}> |
36 | | - {t('console-app~Clear filter')} |
37 | | - </Button> |
38 | | - </EmptyStateSecondaryActions> |
39 | | - </EmptyState> |
40 | | - ); |
41 | | -}; |
42 | | - |
43 | | -const ClusterFilter: React.FC<{ |
44 | | - filterRef: React.Ref<any>; |
45 | | - onFilterChange: (filterText: string) => void; |
46 | | - filterText: string; |
47 | | -}> = ({ filterText, filterRef, onFilterChange }) => { |
48 | | - const { t } = useTranslation(); |
49 | | - return ( |
50 | | - <MenuInput translate="no"> |
51 | | - <TextInput |
52 | | - autoFocus |
53 | | - placeholder={t('console-app~Find a cluster...')} |
54 | | - aria-label={t('console-app~Find a cluster...')} |
55 | | - iconVariant="search" |
56 | | - type="search" |
57 | | - value={filterText} |
58 | | - onChange={onFilterChange} |
59 | | - ref={filterRef} |
60 | | - /> |
61 | | - </MenuInput> |
62 | | - ); |
63 | | -}; |
64 | | - |
65 | 9 | const ClusterGroup: React.FC<{ |
66 | 10 | clusters: ClusterMenuItem[]; |
67 | 11 | }> = ({ clusters }) => { |
68 | 12 | const [activeCluster] = useActiveCluster(); |
69 | 13 | const [activePerspective] = useActivePerspective(); |
70 | 14 |
|
71 | 15 | return clusters.length === 0 ? null : ( |
72 | | - <MenuGroup translate="no" label="Clusters"> |
73 | | - <MenuList> |
74 | | - {clusters.map((cluster) => ( |
75 | | - <MenuItem |
76 | | - translate="no" |
77 | | - data-test-id="cluster-dropdown-item" |
78 | | - key={cluster.key} |
79 | | - itemId={cluster.key} |
80 | | - isSelected={ |
81 | | - activePerspective === ACM_PERSPECTIVE_ID |
82 | | - ? cluster.key === ACM_PERSPECTIVE_ID |
83 | | - : cluster.key === activeCluster |
84 | | - } |
85 | | - onClick={(e) => { |
86 | | - e.preventDefault(); |
87 | | - cluster.onClick(); |
88 | | - }} |
89 | | - > |
90 | | - {cluster.title} |
91 | | - </MenuItem> |
92 | | - ))} |
93 | | - </MenuList> |
94 | | - </MenuGroup> |
| 16 | + <MenuList> |
| 17 | + {clusters.map((cluster) => ( |
| 18 | + <MenuItem |
| 19 | + translate="no" |
| 20 | + data-test-id="cluster-dropdown-item" |
| 21 | + key={cluster.key} |
| 22 | + itemId={cluster.key} |
| 23 | + isSelected={ |
| 24 | + activePerspective === ACM_PERSPECTIVE_ID |
| 25 | + ? cluster.key === ACM_PERSPECTIVE_ID |
| 26 | + : cluster.key === activeCluster |
| 27 | + } |
| 28 | + onClick={(e) => { |
| 29 | + e.preventDefault(); |
| 30 | + cluster.onClick(); |
| 31 | + }} |
| 32 | + > |
| 33 | + {cluster.title} |
| 34 | + </MenuItem> |
| 35 | + ))} |
| 36 | + </MenuList> |
95 | 37 | ); |
96 | 38 | }; |
97 | 39 |
|
98 | 40 | // TODO remove multicluster |
99 | 41 | const ClusterMenu = () => { |
100 | 42 | const { t } = useTranslation(); |
101 | | - const [filterText, setFilterText] = React.useState(''); |
102 | | - const filterRef = React.useRef(null); |
103 | 43 | const menuRef = React.useRef(null); |
104 | 44 | const [activePerspective, setActivePerspective] = useActivePerspective(); |
105 | 45 | const acmPerspectiveExtension = usePerspectiveExtension(ACM_PERSPECTIVE_ID); |
@@ -139,40 +79,10 @@ const ClusterMenu = () => { |
139 | 79 | [acmPerspectiveExtension, onAllClustersClick, onClusterClick], |
140 | 80 | ); |
141 | 81 |
|
142 | | - const isOptionShown = React.useCallback( |
143 | | - (option: ClusterMenuItem): boolean => |
144 | | - fuzzysearch(filterText.toLowerCase(), option.title.toLowerCase()), |
145 | | - [filterText], |
146 | | - ); |
147 | | - |
148 | | - const filteredOptions = React.useMemo(() => optionItems.filter(isOptionShown), [ |
149 | | - isOptionShown, |
150 | | - optionItems, |
151 | | - ]); |
152 | | - |
153 | | - const emptyState: JSX.Element = |
154 | | - filteredOptions.length === 0 ? ( |
155 | | - <NoResults |
156 | | - onClear={(event) => { |
157 | | - event.preventDefault(); |
158 | | - event.stopPropagation(); |
159 | | - setFilterText(''); |
160 | | - filterRef.current?.focus(); |
161 | | - }} |
162 | | - /> |
163 | | - ) : null; |
164 | | - |
165 | 82 | const clusterMenu: JSX.Element = ( |
166 | 83 | <Menu ref={menuRef} isScrollable activeItemId={activeCluster} className="co-cluster-menu"> |
167 | 84 | <MenuContent maxMenuHeight="60vh" translate="no"> |
168 | | - <ClusterFilter |
169 | | - filterText={filterText} |
170 | | - filterRef={filterRef} |
171 | | - onFilterChange={setFilterText} |
172 | | - /> |
173 | | - <Divider /> |
174 | | - {emptyState} |
175 | | - <ClusterGroup clusters={filteredOptions} /> |
| 85 | + <ClusterGroup clusters={optionItems} /> |
176 | 86 | </MenuContent> |
177 | 87 | </Menu> |
178 | 88 | ); |
|
0 commit comments