Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions src/components/map-filters-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,20 @@ import {
createContext,
useCallback,
useContext,
useEffect,
useMemo,
useState,
type ReactNode,
} from 'react'
import { usePathname, useRouter, useSearchParams } from 'next/navigation'
import {
REPORT_GROUPS,
type ReportCategory,
type ReportGroup,
} from '@/lib/reports/categories'
import {
allCategoriesSelected,
selectionToUrlParam,
paramToSelection,
toggleCategory,
toggleGroup,
type CategorySelection,
Expand Down Expand Up @@ -50,9 +53,29 @@ export function MapFiltersProvider({
observations: boolean
children: ReactNode
}) {
const [selection, setSelection] = useState<CategorySelection>(
allCategoriesSelected,
const router = useRouter()
const pathname = usePathname()
const searchParams = useSearchParams()

const [selection, setSelection] = useState<CategorySelection>(() =>
paramToSelection(searchParams.get('categories')),
)

useEffect(() => {
const params = new URLSearchParams(searchParams.toString())
const value = selectionToUrlParam(selection)

if (value === null) {
params.delete('categories')
} else {
params.set('categories', value)
}

const query = params.toString()
router.replace(query ? `${pathname}?${query}` : pathname, { scroll: false })
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [selection])

Comment on lines +56 to +78

@lauriechammah lauriechammah Aug 26, 2026

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keeping filter state and query params bidirectionally synchronized. Right now, filters don't truly persist when you reload the page.

Suggested change
const router = useRouter()
const pathname = usePathname()
const searchParams = useSearchParams()
const [selection, setSelection] = useState<CategorySelection>(() =>
paramToSelection(searchParams.get('categories')),
)
useEffect(() => {
const params = new URLSearchParams(searchParams.toString())
const value = selectionToUrlParam(selection)
if (value === null) {
params.delete('categories')
} else {
params.set('categories', value)
}
const query = params.toString()
router.replace(query ? `${pathname}?${query}` : pathname, { scroll: false })
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [selection])
const router = useRouter()
const pathname = usePathname()
const searchParams = useSearchParams()
const [selection, setSelection] = useState<CategorySelection>(() =>
paramToSelection(searchParams.get('categories')),
)
useEffect(() => {
const next = paramToSelection(searchParams.get('categories'))
setSelection((prev) => {
const sameSize = next.size === prev.size
const sameValues = sameSize && [...next].every((c) => prev.has(c))
return sameValues ? prev : next
})
}, [searchParams])
useEffect(() => {
const params = new URLSearchParams(searchParams.toString())
const nextValue = selectionToUrlParam(selection)
const currentValue = searchParams.get('categories')
if (nextValue === null) params.delete('categories')
else params.set('categories', nextValue)
const normalizedCurrent = currentValue ?? null
if (normalizedCurrent === nextValue) return
const query = params.toString()
router.replace(query ? `${pathname}?${query}` : pathname, { scroll: false })
}, [selection, searchParams, pathname, router])

const [heatmapVisible, setHeatmapVisible] = useState(true)
const [heatmapAvailable, setHeatmapAvailable] = useState(false)

Expand Down
29 changes: 29 additions & 0 deletions src/lib/reports/filters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,35 @@ export function selectionToParam(selection: CategorySelection): string | null {

return chosen.join(',')
}
export function selectionToUrlParam(
selection: CategorySelection,
): string | null {
if (selection.size >= REPORT_CATEGORIES.length) {
return null
}

return REPORT_CATEGORIES.filter((category) => selection.has(category)).join(
',',
)
}

export function paramToSelection(param: string | null): Set<ReportCategory> {
if (param === null) {
return allCategoriesSelected()
}

if (param === '') {
return new Set()
}

return new Set(
param
.split(',')
.filter((value): value is ReportCategory =>
REPORT_CATEGORIES.includes(value as ReportCategory),
),
)
}

export function observationCategoriesOf(
selection: CategorySelection,
Expand Down
Loading