-
-
Notifications
You must be signed in to change notification settings - Fork 4
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
1 parent
1fdb3bf
commit 7c1aa77
Showing
14 changed files
with
196 additions
and
81 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
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 |
---|---|---|
@@ -1 +1,7 @@ | ||
export { encode, decode, type Type } from './urlstate'; | ||
export { | ||
encode, | ||
decode, | ||
type Type, | ||
useUrlEncode, | ||
useUrlState, | ||
} from './urlstate'; |
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import { encode, decode } from './encoder'; | ||
import { type Type, type JSONCompatible, typeOf } from './utils'; | ||
import { useUrlEncode } from './useUrlEncode'; | ||
export { encode, decode, typeOf, useUrlEncode }; | ||
import { useUrlState } from './useUrlState'; | ||
export { encode, decode, typeOf, useUrlEncode, useUrlState }; | ||
export type { Type, JSONCompatible }; |
This file was deleted.
Oops, something went wrong.
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,39 @@ | ||
import { useRouter, usePathname, useSearchParams } from 'next/navigation'; | ||
import React from 'react'; | ||
import { useUrlEncode } from './useUrlEncode'; | ||
import { type JSONCompatible } from './utils'; | ||
|
||
export function useUrlState<T>(defaultState?: JSONCompatible<T>) { | ||
const router = useRouter(); | ||
const pathname = usePathname(); | ||
const { parse, stringify } = useUrlEncode(defaultState); | ||
const searchParams = useSearchParams(); | ||
const [state, setState] = React.useState(() => parse(searchParams)); | ||
|
||
React.useEffect(() => { | ||
setState((curr) => { | ||
const newVal = parse(searchParams); | ||
return JSON.stringify(curr) === JSON.stringify(newVal) ? curr : newVal; | ||
}); | ||
}, [parse, searchParams]); | ||
|
||
const updateState = React.useCallback( | ||
(value: typeof state | ((currState: typeof state) => typeof state)) => { | ||
typeof value === 'function' | ||
? setState((curr) => value(curr)) | ||
: setState(value); | ||
}, | ||
[], | ||
); | ||
|
||
const updateUrl = React.useCallback( | ||
(value: typeof state | ((currState: typeof state) => typeof state)) => { | ||
typeof value === 'function' | ||
? router.push(`${pathname}?${stringify(value(state))}`) | ||
: router.push(`${pathname}?${stringify(value)}`); | ||
}, | ||
[pathname, router, stringify, state], | ||
); | ||
|
||
return { updateUrl, updateState, state }; | ||
} |
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
Oops, something went wrong.