From 7a8ed9bf948a5d73d7ff4d322a8aca2bbc8d285b Mon Sep 17 00:00:00 2001 From: Soxasora Date: Wed, 8 Jan 2025 11:34:38 +0100 Subject: [PATCH] localStorage implementation --- components/notifications-filter.js | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/components/notifications-filter.js b/components/notifications-filter.js index 20570ae04..31574033d 100644 --- a/components/notifications-filter.js +++ b/components/notifications-filter.js @@ -6,13 +6,23 @@ import { Checkbox, Form, SubmitButton } from './form' import FilterIcon from '@/svgs/equalizer-line.svg' import styles from './notifications-filter.module.css' +export const getFiltersFromInc = (inc) => { + const filters = new Set(inc?.split(',') || []) + filters.delete('') + return filters +} + +export const getSavedFilters = () => { + const savedFilters = JSON.parse(window.localStorage.getItem('notificationFilters')) + return savedFilters ? new Set(savedFilters) : new Set() +} + export function NotificationsFilter ({ onClose }) { const router = useRouter() const appliedFilters = useMemo(() => { - const filters = new Set(router.query.inc?.split(',') || []) // get filters from URL - filters.delete('') // avoid empty category - return filters + const incFilters = getFiltersFromInc(router.query.inc) + return incFilters.size ? incFilters : getSavedFilters() }, [router.query.inc]) const [filters, setFilters] = useState(appliedFilters) @@ -26,6 +36,7 @@ export function NotificationsFilter ({ onClose }) { }, []) const filterRoutePush = useCallback(() => { + window.localStorage.setItem('notificationFilters', JSON.stringify([...filters])) const incstr = [...filters].join(',') router.replace( // replace is necessary as lastChecked needs to stay to avoid re-refreshes { @@ -44,7 +55,7 @@ export function NotificationsFilter ({ onClose }) {

notifications filter

- filtering by: + filter by: {filters.size ? ` ${[...filters].join(', ')}` : ' all'}

{ + const incFilters = getFiltersFromInc(router.query.inc) + return incFilters.size > 0 + }) return (