From 16462c568d36c1e55f62314e9720b428674c1006 Mon Sep 17 00:00:00 2001 From: David Walsh Date: Wed, 12 Jul 2023 16:18:28 -0500 Subject: [PATCH] WIP: Implement Network Menu Search --- app/_locales/en/messages.json | 3 + .../network-list-item/network-list-item.js | 2 + .../network-list-menu/network-list-menu.js | 58 ++++++++++++++++++- 3 files changed, 61 insertions(+), 2 deletions(-) diff --git a/app/_locales/en/messages.json b/app/_locales/en/messages.json index 567e7ca3f8b2..ae32ae9e6507 100644 --- a/app/_locales/en/messages.json +++ b/app/_locales/en/messages.json @@ -2532,6 +2532,9 @@ "noAccountsFound": { "message": "No accounts found for the given search query" }, + "noNetworksFound": { + "message": "No networks found for the given search query" + }, "noAddressForName": { "message": "No address has been set for this name." }, diff --git a/ui/components/multichain/network-list-item/network-list-item.js b/ui/components/multichain/network-list-item/network-list-item.js index 88f84f7cafe8..f478f21e61fd 100644 --- a/ui/components/multichain/network-list-item/network-list-item.js +++ b/ui/components/multichain/network-list-item/network-list-item.js @@ -53,11 +53,13 @@ export const NetworkListItem = ({ const t = useI18nContext(); const networkRef = useRef(); + /* useEffect(() => { if (networkRef.current && selected) { networkRef.current.querySelector('.mm-button-link').focus(); } }, [networkRef, selected]); + */ return ( { dispatch(setShowTestNetworks(currentlyOnTestNetwork)); } }, [dispatch, currentlyOnTestNetwork]); + const [searchQuery, setSearchQuery] = useState(''); + + let searchResults = [...nonTestNetworks]; + + if (searchQuery) { + const fuse = new Fuse(searchResults, { + threshold: 0.2, + location: 0, + distance: 100, + maxPatternLength: 32, + minMatchCharLength: 1, + shouldSort: true, + keys: ['nickname', 'chainId', 'ticker'], + }); + fuse.setCollection(searchResults); + searchResults = fuse.search(searchQuery); + } const generateMenuItems = (desiredNetworks) => { return desiredNetworks.map((network, index) => { @@ -159,8 +181,40 @@ export const NetworkListMenu = ({ onClose }) => { {t('networkMenuHeading')} <> + {nonTestNetworks.length > 4 ? ( + + setSearchQuery(e.target.value)} + clearButtonOnClick={() => setSearchQuery('')} + clearButtonProps={{ + size: Size.SM, + }} + inputProps={{ autoFocus: true }} + /> + + ) : null} - {generateMenuItems(nonTestNetworks)} + {searchResults.length === 0 && searchQuery !== '' ? ( + + {t('noNetworksFound')} + + ) : ( + generateMenuItems(searchResults) + )}