-
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
Matteo Miotello
committed
Jan 12, 2024
1 parent
3d37fd1
commit 5a3293d
Showing
8 changed files
with
128 additions
and
40 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
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,21 @@ | ||
import { useContext } from "react"; | ||
import { ShellyConfig } from ".."; | ||
import { ShellyContext } from "../Provider/ShellyProvider"; | ||
|
||
const useShellyContext = (): ShellyConfig => { | ||
return useContext( ShellyContext ); | ||
}; | ||
|
||
export const useNavigate = (): ( path: string ) => void => { | ||
const config = useShellyContext(); | ||
|
||
if ( !config.navigateCallback ) { | ||
throw Error( 'navigateCallback must be provided in ShellyProvider.' ); | ||
} | ||
|
||
return (path) => { | ||
if ( config.navigateCallback ) { | ||
config.navigateCallback()( path ); | ||
} | ||
}; | ||
}; |
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,74 @@ | ||
import { useCallback, useEffect, useState } from "react"; | ||
|
||
type ParamsObject = { [key: string]: string } | ||
|
||
type QueryParamsResult = [ | ||
params: URLSearchParams, | ||
setQueryParams: (params: URLSearchParams | ParamsObject) => void | ||
] | ||
|
||
const isEqual = (prev: URLSearchParams, params: URLSearchParams) => { | ||
let equals = true; | ||
|
||
prev.sort(); | ||
params.sort(); | ||
|
||
if ( params.toString() !== prev.toString() ) { | ||
return false; | ||
} | ||
|
||
prev.forEach((val, key) => { | ||
if (val !== params.get(key)) { | ||
equals = false; | ||
} | ||
}); | ||
|
||
return equals; | ||
}; | ||
|
||
export const useQueryParams = (): QueryParamsResult => { | ||
const [params, setParams] = useState<URLSearchParams>(new URLSearchParams); | ||
|
||
useEffect(() => { | ||
if (!document) { | ||
return; | ||
} | ||
|
||
const url = new URL(document.URL); | ||
const queryParams = url.searchParams; | ||
|
||
if (isEqual(queryParams, params)) { | ||
return; | ||
} | ||
|
||
setParams(queryParams); | ||
}, [document.URL]); | ||
|
||
const setQueryParams = useCallback((newParams: URLSearchParams | ParamsObject) => { | ||
if (!window) { | ||
return; | ||
} | ||
|
||
if ( !(newParams instanceof URLSearchParams) ) { | ||
newParams = new URLSearchParams( newParams ); | ||
} | ||
|
||
newParams.sort(); | ||
params.sort(); | ||
|
||
if ( params.toString() === newParams.toString() ) { | ||
return; | ||
} | ||
|
||
const url = new URL(document.URL ); | ||
url.search = newParams.toString(); | ||
window.history.replaceState( {}, 'replace', url.href ); | ||
|
||
setParams( newParams ); | ||
}, [params]); | ||
|
||
return [ | ||
params, | ||
setQueryParams | ||
]; | ||
}; |
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