feat: support items filtering by tags on UI

This commit is contained in:
Onikiienko Bogdan
2022-11-14 23:13:00 +02:00
committed by Bastian
parent 8d1ddfc4e3
commit f54d49372e
16 changed files with 457 additions and 15 deletions

View File

@@ -111,3 +111,29 @@ const addItemToRing = (ring: Item[] = [], item: Item) => [...ring, item];
export const getFirstLetter = (item: Item) =>
item.title.substr(0, 1).toUpperCase();
export type Tag = string;
export const filteredOnly = (items: Item[], tags: Tag[] | string) => {
return items.filter((item: Item) => {
const { tags: itemTags } = item;
if (typeof itemTags === "undefined") {
return false;
}
if (Array.isArray(tags)) {
return tags.every(tag => itemTags.includes(tag));
}
return itemTags.includes(tags);
});
}
export const getTags = (items: Item[]): Tag[] => {
const tags: Tag[] = items.reduce((acc: Tag[], item: Item) => {
return !item.tags ? acc : acc.concat(item.tags);
}, []).sort();
return Array.from(new Set(tags));
};