forked from ohcnetwork/care_fe
-
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.
App history provider (ohcnetwork#4592)
* Implement useHistory and Providers for its API * Upgrade `PageTitle`'s goBack * `HistoryAPIProvider` for `AppRouter` * reset history if sidebar item clicked
- Loading branch information
1 parent
28deee4
commit b7af79f
Showing
8 changed files
with
93 additions
and
16 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,38 @@ | ||
import { useLocationChange } from "raviger"; | ||
import { createContext, ReactNode, useState } from "react"; | ||
|
||
export const HistoryContext = createContext<string[]>([]); | ||
// eslint-disable-next-line @typescript-eslint/no-empty-function | ||
export const ResetHistoryContext = createContext(() => {}); | ||
|
||
export const HistoryAPIProvider = (props: { children: ReactNode }) => { | ||
const [history, setHistory] = useState<string[]>([]); | ||
|
||
useLocationChange( | ||
(newLocation) => { | ||
setHistory((history) => { | ||
if (history.length && newLocation.fullPath === history[0]) | ||
// Ignore push if navigate to same path (for some weird unknown reasons?) | ||
return history; | ||
|
||
if (history.length > 1 && newLocation.fullPath === history[1]) | ||
// Pop current path if navigate back to previous path | ||
return history.slice(1); | ||
|
||
// Otherwise just push the current path | ||
return [newLocation.fullPath, ...history]; | ||
}); | ||
}, | ||
{ onInitial: true } | ||
); | ||
|
||
const resetHistory = () => setHistory((history) => history.slice(0, 1)); | ||
|
||
return ( | ||
<HistoryContext.Provider value={history}> | ||
<ResetHistoryContext.Provider value={resetHistory}> | ||
{props.children} | ||
</ResetHistoryContext.Provider> | ||
</HistoryContext.Provider> | ||
); | ||
}; |
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,26 @@ | ||
import { navigate } from "raviger"; | ||
import { useContext } from "react"; | ||
import { | ||
HistoryContext, | ||
ResetHistoryContext, | ||
} from "../../CAREUI/misc/HistoryAPIProvider"; | ||
|
||
export default function useAppHistory() { | ||
const history = useContext(HistoryContext); | ||
const resetHistory = useContext(ResetHistoryContext); | ||
|
||
const goBack = (fallbackUrl?: string) => { | ||
if (history.length > 1) | ||
// Navigate to history present in the app navigation history stack. | ||
return navigate(history[1]); | ||
|
||
if (fallbackUrl) | ||
// Otherwise, use provided fallback url if provided. | ||
return navigate(fallbackUrl); | ||
|
||
// Otherwise, fallback to browser's go back behaviour. | ||
window.history.back(); | ||
}; | ||
|
||
return { history, resetHistory, goBack }; | ||
} |
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