-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(url-state): try implement use-url-state hook
- Loading branch information
Showing
7 changed files
with
149 additions
and
59 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
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
4 changes: 2 additions & 2 deletions
4
packages/playground/src/contexts/statement-context-provider.tsx
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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { useUrlState } from '@/hooks/use-url-state' | ||
import { useCallback } from 'react' | ||
|
||
type ExampleUrlState = Partial< | ||
Record<'example' | 'with_select' | 'theme', string> | ||
> | ||
|
||
export function useExampleUrlState() { | ||
const [queryParams, setQueryParams] = useUrlState<ExampleUrlState>() | ||
|
||
// example | ||
const example = queryParams.example | ||
const setExample = useCallback((v: string) => { | ||
setQueryParams({ example: v }) | ||
}, []) | ||
|
||
// theme | ||
const theme = queryParams.theme || 'oneDark' | ||
const setTheme = useCallback((v: string) => { | ||
setQueryParams({ theme: v }) | ||
}, []) | ||
|
||
// with_select | ||
const withSelect = !!queryParams.with_select | ||
|
||
return { example, setExample, theme, setTheme, withSelect } | ||
} |
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,94 @@ | ||
import { createContext, useContext, useMemo, useState } from 'react' | ||
|
||
type UrlStateCtxValue = { | ||
urlSearch: string | ||
setUrlSearch: (v: string) => void | ||
} | ||
|
||
const UrlStateContext = createContext<UrlStateCtxValue | null>(null) | ||
|
||
const useUrlStateContext = () => { | ||
const context = useContext(UrlStateContext) | ||
|
||
if (!context) { | ||
throw new Error('useUrlStateContext must be used within a provider') | ||
} | ||
|
||
return context | ||
} | ||
|
||
type UrlStateProviderOptions = { | ||
getUrlParams: () => string | ||
updateUrlParams: (p: string) => void | ||
} | ||
|
||
const defaultOptions: UrlStateProviderOptions = { | ||
getUrlParams() { | ||
const url = new URL(window.location.href) | ||
return url.search | ||
}, | ||
updateUrlParams(p) { | ||
const url = new URL(window.location.href) | ||
window.history.replaceState({}, '', `${url.pathname}?${p}`) | ||
} | ||
} | ||
|
||
export function UrlStateProvider(props: { | ||
children: React.ReactNode | ||
options?: UrlStateProviderOptions | ||
}) { | ||
const opt = props.options || defaultOptions | ||
const [urlSearch, _setUrlSearch] = useState(opt.getUrlParams()) | ||
|
||
const ctxValue = useMemo( | ||
() => ({ | ||
urlSearch, | ||
setUrlSearch: (v: string) => { | ||
opt.updateUrlParams(v) | ||
_setUrlSearch(v) | ||
} | ||
}), | ||
[urlSearch] | ||
) | ||
|
||
return ( | ||
<UrlStateContext.Provider value={ctxValue}> | ||
{props.children} | ||
</UrlStateContext.Provider> | ||
) | ||
} | ||
|
||
//---------------------- | ||
|
||
type UrlState = Record<string, string | undefined> | ||
type UrlStateObj<T extends UrlState = UrlState> = Partial<{ | ||
[key in keyof T]: string | ||
}> | ||
|
||
export function useUrlState<T extends UrlState = UrlState>(): [ | ||
UrlStateObj<T>, | ||
(s: UrlStateObj<T>) => void | ||
] { | ||
const { urlSearch, setUrlSearch } = useUrlStateContext() | ||
|
||
const searchParams = new URLSearchParams(urlSearch) | ||
const paramsObj: any = {} | ||
searchParams.forEach((v, k) => { | ||
paramsObj[k] = v | ||
}) | ||
const queryParams = paramsObj as UrlStateObj<T> | ||
|
||
function setQueryParams(s: UrlStateObj<T>) { | ||
const searchParams = new URLSearchParams(urlSearch) | ||
Object.keys(s).forEach((k) => { | ||
if (s[k]) { | ||
searchParams.set(k, s[k]) | ||
} else { | ||
searchParams.delete(k) | ||
} | ||
}) | ||
setUrlSearch(searchParams.toString()) | ||
} | ||
|
||
return [queryParams, setQueryParams] as const | ||
} |