diff --git a/src/assets/categories/categories.json b/src/assets/categories/categories.json index 2948280..eaa4079 100644 --- a/src/assets/categories/categories.json +++ b/src/assets/categories/categories.json @@ -3,7 +3,10 @@ "name": "audio", "sub": [ { "name": "music", "sub": ["background", "ambient"] }, - { "name": "sound effects", "sub": ["footsteps", "weapons"] }, + { + "name": "sound effects", + "sub": ["footsteps", "weapons"] + }, "voice" ] }, diff --git a/src/components/Filters/CategoryFilterPopup.tsx b/src/components/Filters/CategoryFilterPopup.tsx index 8e6fcc8..252f73d 100644 --- a/src/components/Filters/CategoryFilterPopup.tsx +++ b/src/components/Filters/CategoryFilterPopup.tsx @@ -1,10 +1,14 @@ import React, { useEffect, useState } from 'react' import { createPortal } from 'react-dom' import { Category } from 'types' -import categoriesData from './../../assets/categories/categories.json' -import { capitalizeEachWord } from 'utils' - +import { + addToUserCategories, + capitalizeEachWord, + deleteFromUserCategories +} from 'utils' +import { useLocalStorage } from 'hooks' import styles from './CategoryFilterPopup.module.scss' +import categoriesData from './../../assets/categories/categories.json' interface CategoryFilterPopupProps { categories: string[] @@ -21,6 +25,9 @@ export const CategoryFilterPopup = ({ setHierarchies, handleClose }: CategoryFilterPopupProps) => { + const [userHierarchies, setUserHierarchies] = useLocalStorage< + (string | Category)[] + >('user-hierarchies', []) const [filterCategories, setFilterCategories] = useState(categories) const [filterHierarchies, setFilterHierarchies] = useState(hierarchies) const handleApply = () => { @@ -54,15 +61,28 @@ export const CategoryFilterPopup = ({ } const handleAddNew = () => { if (inputValue) { - const values = inputValue + const value = inputValue.toLowerCase() + const values = value .trim() .split('>') .map((s) => s.trim()) - if (values.length > 1) { - setFilterHierarchies([...filterHierarchies, values.join(':')]) - } else { - setFilterCategories([...filterCategories, values[0]]) - } + + setUserHierarchies((prev) => { + addToUserCategories(prev, value) + return [...prev] + }) + + const path = values.join(':') + + // Add new hierarchy to current selection and active selection + setFilterHierarchies((prev) => { + prev.push(path) + return [...prev] + }) + setHierarchies((prev) => { + prev.push(path) + return [...prev] + }) setInputValue('') } } @@ -104,23 +124,67 @@ export const CategoryFilterPopup = ({ - {true && ( -