1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 09:52:26 +01:00
metamask-extension/ui/helpers/utils/settings-search.js
Hassan Malik 308c6e4fe3
Add snaps view search (#14419)
* add snaps view search

* add snaps view search

* fix to add in settings constants

* removed spread

* remove unused import

* add fencing

* more fencing

* ternary refactor, updated settings tests

* refactor
2022-04-19 11:08:09 -04:00

83 lines
2.4 KiB
JavaScript

/* eslint-disable require-unicode-regexp */
import { SETTINGS_CONSTANTS } from '../constants/settings';
let settingsRoutes;
export function getSettingsRoutes() {
if (settingsRoutes) {
return settingsRoutes;
}
settingsRoutes = SETTINGS_CONSTANTS.filter((routeObject) =>
routeObject.featureFlag ? process.env[routeObject.featureFlag] : true,
);
return settingsRoutes;
}
function getFilteredSettingsRoutes(t, tabMessage) {
return getSettingsRoutes().filter((routeObject) => {
return routeObject.tabMessage(t) === tabMessage;
});
}
export function getNumberOfSettingsInSection(t, tabMessage) {
return getFilteredSettingsRoutes(t, tabMessage).length;
}
export function handleSettingsRefs(t, tabMessage, settingsRefs) {
const settingsSearchJsonFiltered = getFilteredSettingsRoutes(t, tabMessage);
const settingsRefsIndex = settingsSearchJsonFiltered.findIndex(
(routeObject) =>
routeObject.route.substring(1) === window.location.hash.substring(1),
);
if (settingsRefsIndex === -1) {
return;
}
const settingsRef =
settingsSearchJsonFiltered.length === 1
? settingsRefs
: settingsRefs[settingsRefsIndex];
if (settingsRef?.current) {
settingsRef.current.scrollIntoView({
behavior: 'smooth',
});
settingsRef.current.focus();
const historySettingsUrl = window.location.hash.split('#')[1];
window.location.hash = historySettingsUrl;
}
}
function colorText(menuElement, regex) {
if (menuElement !== null) {
let elemText = menuElement.innerHTML;
elemText = elemText.replace('&', '&');
elemText = elemText.replace(
/(<span style="background:#ffd33d">|<\/span>)/gim,
'',
);
menuElement.innerHTML = elemText.replace(
regex,
'<span style="background:#ffd33d">$&</span>',
);
}
}
export function highlightSearchedText() {
const searchElem = document.getElementById('search-settings');
const searchRegex = new RegExp(searchElem.value, 'gi');
const results = document.querySelectorAll(
'.settings-page__header__search__list__item',
);
[...results].forEach((element) => {
const menuTabElement = element.querySelector(
'.settings-page__header__search__list__item__tab',
);
const menuSectionElement = element.querySelector(
'.settings-page__header__search__list__item__section',
);
colorText(menuTabElement, searchRegex);
colorText(menuSectionElement, searchRegex);
});
}