Skip to content
This repository has been archived by the owner on Oct 4, 2023. It is now read-only.

[C-1515] Allow repeat pushes via useNavigation inside nested navigator #2280

Merged
merged 2 commits into from
Nov 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions packages/mobile/src/hooks/useNavigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { useNavigation as useNativeNavigation } from '@react-navigation/native'
import type { NativeStackNavigationProp } from '@react-navigation/native-stack'
import { isEqual } from 'lodash'

import { getNearestStackNavigator } from 'app/utils/navigation'

export type ContextualParams = {
fromNotifications?: boolean
}
Expand Down Expand Up @@ -48,16 +50,21 @@ export function useNavigation<
const performCustomPush = useCallback(
(...config: PerformNavigationConfig<ParamList>) => {
if (!isEqual(lastNavAction.current, config)) {
;(navigation as NativeStackNavigationProp<any>).push(...config)
lastNavAction.current = config
const stackNavigator = getNearestStackNavigator(navigation)

if (stackNavigator) {
// Reset lastNavAction when the transition ends
const unsubscribe = stackNavigator.addListener(
'transitionEnd',
(e) => {
lastNavAction.current = undefined
unsubscribe()
}
)

// Reset lastNavAction when the transition ends
const unsubscribe = (
navigation as NativeStackNavigationProp<any>
).addListener('transitionEnd', (e) => {
lastNavAction.current = undefined
unsubscribe()
})
stackNavigator.push(...config)
lastNavAction.current = config
}
}
},
[navigation, lastNavAction]
Expand Down
22 changes: 21 additions & 1 deletion packages/mobile/src/utils/navigation.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import type { NavigationState } from '@react-navigation/native'
import type { Maybe } from '@audius/common'
import type { NavigationProp, NavigationState } from '@react-navigation/native'
import type { NativeStackNavigationProp } from '@react-navigation/native-stack'

/**
* Navigation state selector that selects the current route
Expand All @@ -21,3 +23,21 @@ export const getPrimaryRoute = (state: NavigationState) => {
// The route at index 2 is the primary route
return getRoutePath(state)?.[2]
}

/**
* Given a navigator, get the nearest stack navigator in the hierarchy
*/
export const getNearestStackNavigator = (
navigator: NavigationProp<any>
): Maybe<NativeStackNavigationProp<any>> => {
if (navigator.getState?.()?.type === 'stack') {
return navigator as unknown as NativeStackNavigationProp<any>
}
const parent = navigator.getParent()

if (!parent) {
return undefined
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we return navigator in this case?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's safer to have the consumer explicitly check that there is a result, let me fix up the types so that consumers are forced to check

}

return getNearestStackNavigator(parent)
}