Skip to content

Commit

Permalink
localStorage implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Soxasora committed Jan 9, 2025
1 parent 34e39f8 commit dfa3e07
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions components/notifications-filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
{
Expand Down Expand Up @@ -78,7 +89,11 @@ export function NotificationsFilter ({ onClose }) {
export default function NotificationsHeader () {
const showModal = useShowModal()
const router = useRouter()
const hasActiveFilters = router.query.inc?.length

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

return (
<div className='d-flex align-items-center gap-2'>
Expand Down

0 comments on commit dfa3e07

Please sign in to comment.