|
| 1 | +import { useId } from 'react' |
| 2 | +import { Icon } from './Icon' |
| 3 | +import { Link, useSearchParams } from 'react-router' |
| 4 | + |
| 5 | +/**************************************** |
| 6 | + Filter Link |
| 7 | + *****************************************/ |
| 8 | + |
| 9 | +type Props = { |
| 10 | + children: React.ReactNode |
| 11 | + url: string |
| 12 | + filter: string |
| 13 | + value: string |
| 14 | +} |
| 15 | + |
| 16 | +export function FilterLink({ children, url, filter, value }: Props) { |
| 17 | + const [search] = useSearchParams() |
| 18 | + |
| 19 | + // The current URL |
| 20 | + const urlValue = search.get(filter)?.toLowerCase().split(',') |
| 21 | + const on = Array.isArray(urlValue) && urlValue.includes(value.toLowerCase()) |
| 22 | + |
| 23 | + // The next URL |
| 24 | + const nextSearch = new URLSearchParams(search.toString()) |
| 25 | + const valuesFiltered = Array.isArray(urlValue) ? urlValue.filter((v) => v && v !== value) : [] |
| 26 | + |
| 27 | + if (on) { |
| 28 | + nextSearch.set(filter, valuesFiltered.join(',')) |
| 29 | + } else { |
| 30 | + nextSearch.set(filter, valuesFiltered.concat(value).join(',')) |
| 31 | + } |
| 32 | + |
| 33 | + return <FilterLinkUI on={on} to={`${url}?${nextSearch.toString()}`} children={children} /> |
| 34 | +} |
| 35 | + |
| 36 | +/**************************************** |
| 37 | + Filter Link All |
| 38 | +*****************************************/ |
| 39 | + |
| 40 | +export function FilterLinkAll({ children, url, filter }: Omit<Props, 'value'>) { |
| 41 | + const [search] = useSearchParams() |
| 42 | + const on = [null, ''].includes(search.get(filter)) |
| 43 | + const nextSearch = new URLSearchParams(search.toString()) |
| 44 | + nextSearch.delete(filter) |
| 45 | + |
| 46 | + return <FilterLinkUI on={on} to={`${url}?${nextSearch.toString()}`} children={children} /> |
| 47 | +} |
| 48 | + |
| 49 | +/**************************************** |
| 50 | + UI for both the Filter Links Above |
| 51 | + *****************************************/ |
| 52 | + |
| 53 | +type FilterLinkUIProps = { |
| 54 | + children: React.ReactNode |
| 55 | + on: boolean |
| 56 | + to: string |
| 57 | +} |
| 58 | + |
| 59 | +function FilterLinkUI({ children, on, to }: FilterLinkUIProps) { |
| 60 | + const id = useId() |
| 61 | + return ( |
| 62 | + <Link to={to} className="block"> |
| 63 | + <input id={id} type="checkbox" className="hidden" /> |
| 64 | + <span className="text-brandColor mr-2"> |
| 65 | + {on ? <Icon name="checkboxOn" /> : <Icon name="checkboxOff" />} |
| 66 | + </span> |
| 67 | + <label htmlFor={id} className="cursor-pointer"> |
| 68 | + {children} |
| 69 | + </label> |
| 70 | + </Link> |
| 71 | + ) |
| 72 | +} |
| 73 | + |
| 74 | +// // The current URL |
| 75 | +// const urlValue = search.get(filter)?.toLowerCase() |
| 76 | +// const on = urlValue === value.toLowerCase() |
| 77 | + |
| 78 | +// // The next URL |
| 79 | +// const nextSearch = new URLSearchParams(search.toString()) |
| 80 | +// nextSearch.set(filter, value) |
0 commit comments