-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
103 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { atom } from 'jotai'; | ||
import { atomWithLocation } from 'jotai-location'; | ||
|
||
const locationAtom = atomWithLocation(); | ||
|
||
export const stringParam = { | ||
encode: (val: string) => val, | ||
decode: (val: string) => val, | ||
}; | ||
|
||
export const boolParam = { | ||
encode: (val: boolean) => (val ? '1' : '0'), | ||
decode: (val: string) => val === '1', | ||
}; | ||
|
||
export const numberParam = { | ||
encode: (val: number) => String(val), | ||
decode: (val: string) => Number.parseFloat(val), | ||
}; | ||
|
||
export const objParam = { | ||
encode: JSON.stringify, | ||
decode: JSON.parse, | ||
}; | ||
|
||
export const typeToParam = { | ||
string: stringParam, | ||
boolean: boolParam, | ||
number: numberParam, | ||
object: objParam, | ||
}; | ||
|
||
export const atomWithUrl = <T>( | ||
key: string, | ||
defaultValue: T, | ||
options?: { encode: (val: T) => string; decode: (val: string) => unknown }, | ||
) => { | ||
const optionsOrInferred = | ||
options ?? | ||
typeToParam[typeof defaultValue as keyof typeof typeToParam] ?? | ||
objParam; | ||
|
||
const { encode, decode } = optionsOrInferred; | ||
|
||
return atom( | ||
(get) => { | ||
const location = get(locationAtom); | ||
return location.searchParams?.has(key) | ||
? (decode(location.searchParams.get(key) ?? '') as T) | ||
: defaultValue; | ||
}, | ||
(get, set, newValue: T) => { | ||
const prev = get(locationAtom); | ||
const searchParams = new URLSearchParams(prev.searchParams); | ||
searchParams.set(key, encode(newValue)); | ||
|
||
set( | ||
// biome-ignore lint/suspicious/noExplicitAny: <really annoying> | ||
locationAtom as any, | ||
{ | ||
...prev, | ||
searchParams: searchParams, | ||
}, | ||
{ replace: false }, | ||
); | ||
}, | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.