// WF-P8 S01 — query-parameter plumbing for the board filter bar. // // Both board routes (`/projects` and `/projects/pipelines/:id`) drive the same // screen, so the mapping "facet option ↔ query parameter" lives here instead of // being copied into each route and drifting. import { useSearchParams } from "react-router"; import type { BoardFacet, BoardFacetChip } from "@hivegrid/view-models"; /** Query parameter backing each facet. Mirrors parseBoardFacetParams. */ const FACET_PARAM: Record = { owner: "owner", customerType: "kundentyp", tags: "tags", period: "zeitraum", value: "wert", }; /** Every facet holds a list of values — the period one unions its ranges. */ const MULTI_FACETS: ReadonlySet = new Set(["owner", "customerType", "tags", "period", "value"]); /** * Every parameter a filter reset has to drop. `page` is in the list because a * narrowed list can sit on page 4; keeping that after a reset would show an * empty page instead of the unfiltered first one. */ const FILTER_PARAMS = ["q", "onlyMine", "owner", "kundentyp", "tags", "zeitraum", "von", "bis", "wert", "page"] as const; function readList(params: URLSearchParams, key: string): string[] { const raw = params.get(key); return raw ? raw.split(",").filter(Boolean) : []; } function writeList(next: URLSearchParams, key: string, values: readonly string[]): void { if (values.length > 0) next.set(key, values.join(",")); else next.delete(key); } export function useBoardFacetParams() { const [searchParams, setSearchParams] = useSearchParams(); /** * Any change to the filter changes which rows exist, so the paged list goes * back to page 1 instead of landing on a page the new result no longer has. */ function commit(next: URLSearchParams) { next.delete("page"); setSearchParams(next); } function setParam(key: string, value: string) { const next = new URLSearchParams(searchParams); if (value) next.set(key, value); else next.delete(key); commit(next); } function toggleFacet(facet: BoardFacet["key"], value: string) { const key = FACET_PARAM[facet]; const next = new URLSearchParams(searchParams); if (MULTI_FACETS.has(facet)) { const current = readList(next, key); writeList(next, key, current.includes(value) ? current.filter((entry) => entry !== value) : [...current, value]); } else { // Single-select: clicking the active option clears it. if (next.get(key) === value) next.delete(key); else next.set(key, value); // The custom bounds belong to the custom option only. if (next.get(key) !== "custom") { next.delete("von"); next.delete("bis"); } } commit(next); } function removeChip(chip: BoardFacetChip) { const next = new URLSearchParams(searchParams); if (chip.facet === "onlyMine") next.delete("onlyMine"); else if (chip.facet === "q") next.delete("q"); else if (MULTI_FACETS.has(chip.facet)) { const key = FACET_PARAM[chip.facet]; writeList( next, key, readList(next, key).filter((entry) => entry !== chip.value) ); } else { next.delete(FACET_PARAM[chip.facet]); next.delete("von"); next.delete("bis"); } commit(next); } function setPeriodRange(from: string, to: string) { const next = new URLSearchParams(searchParams); next.set("zeitraum", "custom"); if (from) next.set("von", from); else next.delete("von"); if (to) next.set("bis", to); else next.delete("bis"); commit(next); } /** Drop every filter in one navigation — pipeline and demo toggle survive. */ function resetFilters() { const next = new URLSearchParams(searchParams); for (const key of FILTER_PARAMS) next.delete(key); commit(next); } return { setParam, toggleFacet, removeChip, setPeriodRange, resetFilters }; }