Feat: blocks should only search on singular labels (#2479)

* Blocks now only search on singular labels

* Add types to getBlockLabel params

---------

Co-authored-by: Jarrod Flesch <jarrodmflesch@gmail.com>
This commit is contained in:
Martin Choutka
2023-04-25 17:40:50 +02:00
committed by GitHub
parent a684b8f8bf
commit 8670d387d6

View File

@@ -7,11 +7,21 @@ import { Drawer } from '../../../../elements/Drawer';
import { getTranslation } from '../../../../../../utilities/getTranslation';
import { ThumbnailCard } from '../../../../elements/ThumbnailCard';
import DefaultBlockImage from '../../../../graphics/DefaultBlockImage';
import { i18n } from 'i18next';
import { Block } from '../../../../../../fields/config/types';
import './index.scss';
const baseClass = 'blocks-drawer';
const getBlockLabel = (block: Block, i18n: i18n) => {
if (typeof block.labels.singular === 'string') return block.labels.singular.toLowerCase();
if (typeof block.labels.singular === 'object') {
return getTranslation(block.labels.singular, i18n).toLowerCase();
}
return '';
};
export const BlocksDrawer: React.FC<Props> = (props) => {
const {
blocks,
@@ -33,13 +43,15 @@ export const BlocksDrawer: React.FC<Props> = (props) => {
}, [isModalOpen]);
useEffect(() => {
const searchTermToUse = searchTerm.toLowerCase();
const matchingBlocks = blocks.reduce((matchedBlocks, block) => {
if (block.slug.toLowerCase().indexOf(searchTerm.toLowerCase()) !== -1) matchedBlocks.push(block);
const blockLabel = getBlockLabel(block, i18n);
if (blockLabel.includes(searchTermToUse)) matchedBlocks.push(block);
return matchedBlocks;
}, []);
setFilteredBlocks(matchingBlocks);
}, [searchTerm, blocks]);
}, [searchTerm, blocks, i18n]);
return (
<Drawer