mirror of
https://github.com/fsolla/teqo.git
synced 2026-11-27 23:03:11 +00:00
refactor(campanha): B133 simplify — extract useActiveMunicipalitySavedFilter hook
Co-authored-by: Francisco Solla <fsolla@users.noreply.github.com>
This commit is contained in:
parent
6cc17f0d4e
commit
9e2d5663ef
3 changed files with 29 additions and 34 deletions
|
|
@ -1,32 +1,15 @@
|
||||||
'use client'
|
'use client'
|
||||||
|
|
||||||
import { usePathname, useSearchParams } from 'next/navigation'
|
import { useActiveMunicipalitySavedFilter } from '@/components/campaign/shared/useMunicipalitySavedFilters'
|
||||||
import { useMemo } from 'react'
|
|
||||||
|
|
||||||
import { useMunicipalitySavedFilters } from '@/components/campaign/shared/useMunicipalitySavedFilters'
|
|
||||||
import { SetCampaignPageChrome } from '@/components/campaign/shell/CampaignPageChromeContext'
|
import { SetCampaignPageChrome } from '@/components/campaign/shell/CampaignPageChromeContext'
|
||||||
import { campaignPageChromeCatalog } from '@/lib/campaignPageChrome'
|
import { campaignPageChromeCatalog } from '@/lib/campaignPageChrome'
|
||||||
import { isSameListHref } from '@/lib/listQueryMatch'
|
|
||||||
|
|
||||||
/** A page is a position inside a recorte, not part of it (B18). */
|
|
||||||
const IGNORED_PARAMS = ['page']
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* When the current URL matches a saved municipality filter, the filter name
|
* When the current URL matches a saved municipality filter, the filter name
|
||||||
* becomes the page subtitle (B133) — the bar keeps chips as the editable recorte.
|
* becomes the page subtitle (B133) — the bar keeps chips as the editable recorte.
|
||||||
*/
|
*/
|
||||||
export const MunicipalityListPageChrome = () => {
|
export const MunicipalityListPageChrome = () => {
|
||||||
const savedFilters = useMunicipalitySavedFilters()
|
const activeFilter = useActiveMunicipalitySavedFilter()
|
||||||
const pathname = usePathname()
|
|
||||||
const searchParams = useSearchParams()
|
|
||||||
|
|
||||||
const activeFilter = useMemo(() => {
|
|
||||||
const query = searchParams.toString()
|
|
||||||
const currentHref = query ? `${pathname}?${query}` : pathname
|
|
||||||
return savedFilters.find((entry) =>
|
|
||||||
isSameListHref(currentHref, entry.href, IGNORED_PARAMS),
|
|
||||||
)
|
|
||||||
}, [pathname, searchParams, savedFilters])
|
|
||||||
|
|
||||||
if (!activeFilter) return null
|
if (!activeFilter) return null
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,19 @@
|
||||||
'use client'
|
'use client'
|
||||||
|
|
||||||
|
import { usePathname, useSearchParams } from 'next/navigation'
|
||||||
|
import { useMemo } from 'react'
|
||||||
import { useSyncExternalStore } from 'react'
|
import { useSyncExternalStore } from 'react'
|
||||||
|
|
||||||
|
import { isSameListHref } from '@/lib/listQueryMatch'
|
||||||
import {
|
import {
|
||||||
listMunicipalitySavedFilters,
|
listMunicipalitySavedFilters,
|
||||||
subscribeMunicipalitySavedFilters,
|
subscribeMunicipalitySavedFilters,
|
||||||
type MunicipalitySavedFilter,
|
type MunicipalitySavedFilter,
|
||||||
} from '@/utilities/municipality/municipalitySavedFilters'
|
} from '@/utilities/municipality/municipalitySavedFilters'
|
||||||
|
|
||||||
|
/** A page is a position inside a recorte, not part of it (B18). */
|
||||||
|
export const MUNICIPALITY_SAVED_FILTER_IGNORED_PARAMS = ['page'] as const
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Stable reference for the render that has no `localStorage` to read. Returning
|
* Stable reference for the render that has no `localStorage` to read. Returning
|
||||||
* a fresh `[]` here is the classic `useSyncExternalStore` render loop.
|
* a fresh `[]` here is the classic `useSyncExternalStore` render loop.
|
||||||
|
|
@ -26,3 +32,18 @@ export const useMunicipalitySavedFilters = (): readonly MunicipalitySavedFilter[
|
||||||
listMunicipalitySavedFilters,
|
listMunicipalitySavedFilters,
|
||||||
() => NO_SAVED_FILTERS,
|
() => NO_SAVED_FILTERS,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
/** The saved filter whose href matches the current list URL, if any (B18/B133). */
|
||||||
|
export const useActiveMunicipalitySavedFilter = (): MunicipalitySavedFilter | undefined => {
|
||||||
|
const savedFilters = useMunicipalitySavedFilters()
|
||||||
|
const pathname = usePathname()
|
||||||
|
const searchParams = useSearchParams()
|
||||||
|
|
||||||
|
return useMemo(() => {
|
||||||
|
const query = searchParams.toString()
|
||||||
|
const currentHref = query ? `${pathname}?${query}` : pathname
|
||||||
|
return savedFilters.find((entry) =>
|
||||||
|
isSameListHref(currentHref, entry.href, MUNICIPALITY_SAVED_FILTER_IGNORED_PARAMS),
|
||||||
|
)
|
||||||
|
}, [pathname, searchParams, savedFilters])
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,26 +2,24 @@
|
||||||
|
|
||||||
import { Trash2Icon } from 'lucide-react'
|
import { Trash2Icon } from 'lucide-react'
|
||||||
import Link from 'next/link'
|
import Link from 'next/link'
|
||||||
import { usePathname, useSearchParams } from 'next/navigation'
|
|
||||||
import { toast } from 'sonner'
|
import { toast } from 'sonner'
|
||||||
|
|
||||||
import { useMunicipalitySavedFilters } from '@/components/campaign/shared/useMunicipalitySavedFilters'
|
import {
|
||||||
|
useActiveMunicipalitySavedFilter,
|
||||||
|
useMunicipalitySavedFilters,
|
||||||
|
} from '@/components/campaign/shared/useMunicipalitySavedFilters'
|
||||||
import {
|
import {
|
||||||
SidebarMenuAction,
|
SidebarMenuAction,
|
||||||
SidebarMenuSub,
|
SidebarMenuSub,
|
||||||
SidebarMenuSubButton,
|
SidebarMenuSubButton,
|
||||||
SidebarMenuSubItem,
|
SidebarMenuSubItem,
|
||||||
} from '@/components/ui/Sidebar'
|
} from '@/components/ui/Sidebar'
|
||||||
import { isSameListHref } from '@/lib/listQueryMatch'
|
|
||||||
import {
|
import {
|
||||||
removeMunicipalitySavedFilter,
|
removeMunicipalitySavedFilter,
|
||||||
saveMunicipalitySavedFilter,
|
saveMunicipalitySavedFilter,
|
||||||
type MunicipalitySavedFilter,
|
type MunicipalitySavedFilter,
|
||||||
} from '@/utilities/municipality/municipalitySavedFilters'
|
} from '@/utilities/municipality/municipalitySavedFilters'
|
||||||
|
|
||||||
/** A page is a position inside a recorte, not part of it. */
|
|
||||||
const IGNORED_PARAMS = ['page']
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The saved filters of `/campanha/municipios` (B18), hung under that nav item.
|
* The saved filters of `/campanha/municipios` (B18), hung under that nav item.
|
||||||
*
|
*
|
||||||
|
|
@ -31,14 +29,7 @@ const IGNORED_PARAMS = ['page']
|
||||||
*/
|
*/
|
||||||
export const MunicipalityNavSavedFilters = ({ onNavigate }: { onNavigate: () => void }) => {
|
export const MunicipalityNavSavedFilters = ({ onNavigate }: { onNavigate: () => void }) => {
|
||||||
const savedFilters = useMunicipalitySavedFilters()
|
const savedFilters = useMunicipalitySavedFilters()
|
||||||
const pathname = usePathname()
|
const activeFilter = useActiveMunicipalitySavedFilter()
|
||||||
const searchParams = useSearchParams()
|
|
||||||
|
|
||||||
const query = searchParams.toString()
|
|
||||||
const currentHref = query ? `${pathname}?${query}` : pathname
|
|
||||||
const activeHref = savedFilters.find((entry) =>
|
|
||||||
isSameListHref(currentHref, entry.href, IGNORED_PARAMS),
|
|
||||||
)?.href
|
|
||||||
|
|
||||||
if (!savedFilters.length) return null
|
if (!savedFilters.length) return null
|
||||||
|
|
||||||
|
|
@ -75,7 +66,7 @@ export const MunicipalityNavSavedFilters = ({ onNavigate }: { onNavigate: () =>
|
||||||
return (
|
return (
|
||||||
<SidebarMenuSub aria-label="Filtros salvos de Municípios" className="-mt-0.5 py-0">
|
<SidebarMenuSub aria-label="Filtros salvos de Municípios" className="-mt-0.5 py-0">
|
||||||
{savedFilters.map((entry) => {
|
{savedFilters.map((entry) => {
|
||||||
const isActive = entry.href === activeHref
|
const isActive = entry.href === activeFilter?.href
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<SidebarMenuSubItem key={entry.href}>
|
<SidebarMenuSubItem key={entry.href}>
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue