-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #296 from VKCOM/pavelnikitin/feature/routing-block…
…er/MA-12851 MA-12851: Add blocker in routeNavigator
- Loading branch information
Showing
5 changed files
with
107 additions
and
61 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 |
---|---|---|
@@ -1,7 +1,8 @@ | ||
export const STATE_KEY_SHOW_MODAL = 'showModal'; | ||
export const STATE_KEY_SHOW_POPOUT = 'showPopout'; | ||
export const STATE_KEY_BLOCK_FORWARD_NAVIGATION = 'blockForward'; | ||
export const NAVIGATION_BLOCKER_KEY = 'vk-mini-app-navigation-block'; | ||
|
||
export const SEARCH_PARAM_INFLATE = 'inflate'; | ||
|
||
export const UNIVERSAL_URL = '*' | ||
export const UNIVERSAL_URL = '*'; |
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,50 +1,60 @@ | ||
import { useCallback, useEffect, useState } from 'react'; | ||
import { Blocker, BlockerFunction, Router } from '@remix-run/router'; | ||
import { STATE_KEY_BLOCK_FORWARD_NAVIGATION, STATE_KEY_SHOW_MODAL, STATE_KEY_SHOW_POPOUT } from '../const'; | ||
import { useEffect } from 'react'; | ||
import { BlockerFunction, Router } from '@remix-run/router'; | ||
import { | ||
STATE_KEY_BLOCK_FORWARD_NAVIGATION, | ||
STATE_KEY_SHOW_MODAL, | ||
STATE_KEY_SHOW_POPOUT, | ||
} from '../const'; | ||
import { ViewHistory } from '../services/ViewHistory'; | ||
import { RouteNavigator } from '../services/RouteNavigator.type'; | ||
|
||
let blockerId = 0; | ||
const processedKeys: string[] = []; | ||
|
||
export function useBlockForwardToModals(router: Router, viewHistory: ViewHistory): Blocker { | ||
const [blockerKey] = useState(() => String(++blockerId)); | ||
export function useBlockForwardToModals( | ||
router: Router, | ||
viewHistory: ViewHistory, | ||
routeNavigator: RouteNavigator, | ||
) { | ||
useEffect(() => { | ||
const blockerFunction: BlockerFunction = ({ historyAction, nextLocation }) => { | ||
const isPopForward = viewHistory.isPopForward(historyAction, nextLocation.key); | ||
const blockEnabled = isPopForward && nextLocation.key !== 'default'; | ||
return Boolean(blockEnabled && nextLocation.state?.[STATE_KEY_BLOCK_FORWARD_NAVIGATION]); | ||
}; | ||
const unbblocker = routeNavigator.block(blockerFunction); | ||
|
||
const blockerFunction = useCallback<BlockerFunction>(({ historyAction, nextLocation }) => { | ||
const isPopForward = viewHistory.isPopForward(historyAction, nextLocation.key); | ||
const blockEnabled = isPopForward && nextLocation.key !== 'default'; | ||
return Boolean(blockEnabled && nextLocation.state?.[STATE_KEY_BLOCK_FORWARD_NAVIGATION]); | ||
}, [viewHistory]); | ||
return () => unbblocker() | ||
}, [routeNavigator, viewHistory]); | ||
|
||
const blocker = router.getBlocker(blockerKey, blockerFunction); | ||
|
||
useEffect( | ||
() => { | ||
router.subscribe((state) => { | ||
const key = state.location.key; | ||
const isPopBackward = viewHistory.isPopBackward(state.historyAction, key); | ||
if (isPopBackward && state.location.state?.[STATE_KEY_BLOCK_FORWARD_NAVIGATION] && !processedKeys.includes(key)) { | ||
processedKeys.push(key); | ||
const replaceState = { ...window.history.state }; | ||
if (replaceState.usr?.[STATE_KEY_SHOW_MODAL]) { | ||
replaceState.usr = { ...replaceState.usr }; | ||
delete replaceState.usr?.[STATE_KEY_SHOW_MODAL]; | ||
delete replaceState.usr?.[STATE_KEY_BLOCK_FORWARD_NAVIGATION]; | ||
} | ||
if (replaceState.usr?.[STATE_KEY_SHOW_POPOUT]) { | ||
replaceState.usr = { ...replaceState.usr }; | ||
delete replaceState.usr?.[STATE_KEY_SHOW_POPOUT]; | ||
delete replaceState.usr?.[STATE_KEY_BLOCK_FORWARD_NAVIGATION]; | ||
} | ||
window.history.replaceState(replaceState, ''); | ||
router.navigate(-1).then(() => processedKeys.splice(processedKeys.findIndex((name) => name === key), 1)); | ||
useEffect(() => { | ||
router.subscribe((state) => { | ||
const key = state.location.key; | ||
const isPopBackward = viewHistory.isPopBackward(state.historyAction, key); | ||
if ( | ||
isPopBackward && | ||
state.location.state?.[STATE_KEY_BLOCK_FORWARD_NAVIGATION] && | ||
!processedKeys.includes(key) | ||
) { | ||
processedKeys.push(key); | ||
const replaceState = { ...window.history.state }; | ||
if (replaceState.usr?.[STATE_KEY_SHOW_MODAL]) { | ||
replaceState.usr = { ...replaceState.usr }; | ||
delete replaceState.usr?.[STATE_KEY_SHOW_MODAL]; | ||
delete replaceState.usr?.[STATE_KEY_BLOCK_FORWARD_NAVIGATION]; | ||
} | ||
}); | ||
|
||
// Cleanup on unmount | ||
return () => router.deleteBlocker(blockerKey); | ||
}, | ||
[router, blockerKey, viewHistory], | ||
); | ||
|
||
return blocker; | ||
if (replaceState.usr?.[STATE_KEY_SHOW_POPOUT]) { | ||
replaceState.usr = { ...replaceState.usr }; | ||
delete replaceState.usr?.[STATE_KEY_SHOW_POPOUT]; | ||
delete replaceState.usr?.[STATE_KEY_BLOCK_FORWARD_NAVIGATION]; | ||
} | ||
window.history.replaceState(replaceState, ''); | ||
router.navigate(-1).then(() => | ||
processedKeys.splice( | ||
processedKeys.findIndex((name) => name === key), | ||
1, | ||
), | ||
); | ||
} | ||
}); | ||
}, [router, viewHistory]); | ||
} |
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