Skip to content

Commit

Permalink
add filter to search global
Browse files Browse the repository at this point in the history
  • Loading branch information
six7 committed Sep 14, 2024
1 parent 417df53 commit a541e6e
Showing 1 changed file with 24 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
activeTokenSetSelector,
collapsedTokenSetsSelector,
editProhibitedSelector,
tokenFilterSelector,
tokensSelector,
usedTokenSetSelector,
} from '@/selectors';
Expand Down Expand Up @@ -55,16 +56,36 @@ export default function TokenSetTree({
const collapsed = useSelector(collapsedTokenSetsSelector);
const externalItems = useMemo(() => tokenSetListToTree(tokenSets), [tokenSets]);
const [items, setItems] = React.useState<TreeItem[]>(externalItems);
const tokenFilter = useSelector(tokenFilterSelector);

const debouncedOnReorder = React.useMemo(() => debounce(onReorder, 500), [onReorder]);

const filteredSetItems = React.useMemo(() => {
const tokenSetsContainingItemsThatMatchFilter = new Set<string>();

Object.entries(tokens).forEach(([setName, setContent]) => {
if (setContent.some((token) => token.name.toLowerCase().includes(tokenFilter.toLowerCase()))
|| setName.toLowerCase().includes(tokenFilter.toLowerCase())) {
// Add the matching set and all its parent folders
let currentPath = setName;
while (currentPath) {
tokenSetsContainingItemsThatMatchFilter.add(currentPath);
currentPath = currentPath.split('/').slice(0, -1).join('/');
}
}
});

return externalItems.filter((item) => tokenSetsContainingItemsThatMatchFilter.has(item.path));
}, [externalItems, tokenFilter, tokens]);

React.useEffect(() => {
// Compare saved tokenSet order with GUI tokenSet order and update the tokenSet if there is a difference
if (!isEqual(Object.keys(tokens), externalItems.filter(({ isLeaf }) => isLeaf).map(({ path }) => path))) {
debouncedOnReorder(externalItems.filter(({ isLeaf }) => isLeaf).map(({ path }) => path));
debouncedOnReorder(filteredSetItems.filter(({ isLeaf }) => isLeaf).map(({ path }) => path));
}
setItems(externalItems);
}, [externalItems, tokens, debouncedOnReorder]);
// Filter externalItems based on tokenFilter. Filter on the children as well as the name of the set
setItems(filteredSetItems);
}, [externalItems, tokens, debouncedOnReorder, tokenFilter, filteredSetItems]);

const determineCheckedState = useCallback((item: TreeItem) => {
if (item.isLeaf) {
Expand Down

0 comments on commit a541e6e

Please sign in to comment.