mirror of https://github.com/mkrhere/pw2
Muthu Kumar
7 months ago
6 changed files with 99 additions and 77 deletions
@ -0,0 +1,57 @@ |
|||
import { useState } from "react"; |
|||
import useLocation from "wouter/use-location"; |
|||
|
|||
export type HookSet<T> = { |
|||
set: Set<T>; |
|||
size: number; |
|||
add: (value: T) => void; |
|||
remove: (value: T) => void; |
|||
clear: () => void; |
|||
has: (value: T) => boolean; |
|||
}; |
|||
|
|||
const flatten = (set: Set<string>, key: string) => |
|||
Array.from(set) |
|||
.map(param => `${key}=${encodeURIComponent(param)}`) |
|||
.join("&"); |
|||
|
|||
export function useSearchParams(key: string): HookSet<string> { |
|||
const [, navigate] = useLocation(); |
|||
const [params, setParams] = useState( |
|||
() => new Set(new URLSearchParams(window.location.search).getAll(key)), |
|||
); |
|||
|
|||
const add = (value: string) => { |
|||
setParams(prevSet => { |
|||
const newSet = new Set(prevSet); |
|||
newSet.add(value); |
|||
navigate("?" + flatten(newSet, key), { replace: true }); |
|||
return newSet; |
|||
}); |
|||
}; |
|||
|
|||
const remove = (value: string) => { |
|||
setParams(prevSet => { |
|||
const newSet = new Set(prevSet); |
|||
newSet.delete(value); |
|||
navigate("?" + flatten(newSet, key), { replace: true }); |
|||
return newSet; |
|||
}); |
|||
}; |
|||
|
|||
const clear = () => { |
|||
setParams(new Set()); |
|||
navigate("?", { replace: true }); |
|||
}; |
|||
|
|||
return { |
|||
set: params, |
|||
size: params.size, |
|||
add, |
|||
remove, |
|||
clear, |
|||
has: (value: string) => params.has(value), |
|||
}; |
|||
} |
|||
|
|||
export default useSearchParams; |
@ -1,41 +0,0 @@ |
|||
import { useState } from "react"; |
|||
|
|||
export type HookSet<T> = { |
|||
size: number; |
|||
set: Set<T>; |
|||
add: (value: T) => void; |
|||
remove: (value: T) => void; |
|||
clear: () => void; |
|||
has: (value: T) => boolean; |
|||
}; |
|||
|
|||
function useSet<T>(initialValues: T[] = []): HookSet<T> { |
|||
const [set, setSet] = useState(new Set(initialValues)); |
|||
|
|||
const add = (value: T) => { |
|||
setSet(prevSet => new Set([...prevSet, value])); |
|||
}; |
|||
|
|||
const remove = (value: T) => { |
|||
setSet(prevSet => { |
|||
const newSet = new Set(prevSet); |
|||
newSet.delete(value); |
|||
return newSet; |
|||
}); |
|||
}; |
|||
|
|||
const clear = () => { |
|||
setSet(new Set()); |
|||
}; |
|||
|
|||
return { |
|||
size: set.size, |
|||
set, |
|||
add, |
|||
remove, |
|||
clear, |
|||
has: (value: T) => set.has(value), |
|||
}; |
|||
} |
|||
|
|||
export default useSet; |
Loading…
Reference in new issue