-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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 #40016 from software-mansion-labs/fix/refresh-page…
…-with-workspace Add useActiveWorkspaceFromNavigationState
- Loading branch information
Showing
8 changed files
with
70 additions
and
51 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import {useNavigationState} from '@react-navigation/native'; | ||
import Log from '@libs/Log'; | ||
import type {BottomTabNavigatorParamList} from '@libs/Navigation/types'; | ||
import SCREENS from '@src/SCREENS'; | ||
|
||
/** | ||
* Get the currently selected policy ID stored in the navigation state. This hook should only be called only from screens in BottomTab. | ||
* Differences between this hook and useActiveWorkspace: | ||
* - useActiveWorkspaceFromNavigationState reads the active workspace id directly from the navigation state, it's a bit slower than useActiveWorkspace and it can be called only from BottomTabScreens. | ||
* It allows to read a value of policyID immediately after the update. | ||
* - useActiveWorkspace allows to read the current policyID anywhere, it's faster because it doesn't require searching in the navigation state. | ||
*/ | ||
function useActiveWorkspaceFromNavigationState() { | ||
// The last policyID value is always stored in the last route in BottomTabNavigator. | ||
const activeWorkpsaceID = useNavigationState<BottomTabNavigatorParamList, string | undefined>((state) => { | ||
// SCREENS.HOME is a screen located in the BottomTabNavigator, if it's not in state.routeNames it means that this hook was called from a screen in another navigator. | ||
if (!state.routeNames.includes(SCREENS.HOME)) { | ||
Log.warn('useActiveWorkspaceFromNavigationState should be called only from BottomTab screens'); | ||
} | ||
|
||
return state.routes.at(-1)?.params?.policyID; | ||
}); | ||
|
||
return activeWorkpsaceID; | ||
} | ||
|
||
export default useActiveWorkspaceFromNavigationState; |
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