fix(lib/discord/modules/settings): refactor and get updated state before replacing in refreshSettingsOverviewScreen
This commit is contained in:
parent
5543030b57
commit
35ca73f531
1 changed files with 36 additions and 33 deletions
|
|
@ -110,31 +110,17 @@ const { CommonActions, StackActions } = ReactNavigationNative
|
|||
*/
|
||||
export async function refreshSettingsOverviewScreen(renavigate?: boolean) {
|
||||
const navigation = RootNavigationRef.getRootNavigationRef()
|
||||
if (!navigation.isReady()) return false
|
||||
|
||||
const state = navigation.getRootState()
|
||||
if (!navigation.isReady()) return
|
||||
|
||||
let state = navigation.getRootState()
|
||||
// State with SettingsOverviewScreen
|
||||
const settingsState = findInTree(
|
||||
state,
|
||||
(node): node is NavigationState =>
|
||||
Array.isArray(node.routes) &&
|
||||
node.routes[0]?.name ===
|
||||
(Constants.UserSettingsSections as Record<string, string>)
|
||||
.OVERVIEW,
|
||||
)
|
||||
|
||||
let settingsState = findInTree(state, isNavigationSettingsState)
|
||||
// We're currently not on the settings screen, so we don't need to reset
|
||||
if (!settingsState) return false
|
||||
if (!settingsState) return
|
||||
|
||||
if (renavigate) {
|
||||
const mainState = findInTree(
|
||||
state,
|
||||
(node): node is NavigationState =>
|
||||
Array.isArray(node.routes) && node.routes.length > 1,
|
||||
)
|
||||
|
||||
if (!mainState) return false
|
||||
const mainState = findInTree(state, isNavigationMainState)
|
||||
if (!mainState) return
|
||||
|
||||
navigation.dispatch({
|
||||
...CommonActions.goBack(),
|
||||
|
|
@ -146,22 +132,39 @@ export async function refreshSettingsOverviewScreen(renavigate?: boolean) {
|
|||
// Wait for navigation to complete and reset to the settings state
|
||||
requestAnimationFrame(() => {
|
||||
navigation.reset({
|
||||
index: settingsState.routes.length - 1,
|
||||
routes: settingsState.routes,
|
||||
} as PartialState<typeof settingsState>)
|
||||
index: settingsState!.routes.length - 1,
|
||||
routes: settingsState!.routes,
|
||||
} as PartialState<NonNullable<typeof settingsState>>)
|
||||
})
|
||||
} else {
|
||||
const {
|
||||
key: target,
|
||||
routes: [{ name, key: source }],
|
||||
} = settingsState
|
||||
requestAnimationFrame(() => {
|
||||
// Get updated state
|
||||
state = navigation.getRootState()
|
||||
settingsState = findInTree(state, isNavigationSettingsState)
|
||||
if (!settingsState) return
|
||||
|
||||
navigation.dispatch({
|
||||
...StackActions.replace(name),
|
||||
source,
|
||||
target,
|
||||
const {
|
||||
key: target,
|
||||
routes: [{ name, key: source }],
|
||||
} = settingsState
|
||||
|
||||
navigation.dispatch({
|
||||
...StackActions.replace(name),
|
||||
source,
|
||||
target,
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
function isNavigationMainState(state: any): state is NavigationState {
|
||||
return Array.isArray(state.routes) && state.routes.length > 1
|
||||
}
|
||||
|
||||
function isNavigationSettingsState(state: any): state is NavigationState {
|
||||
return (
|
||||
Array.isArray(state.routes) &&
|
||||
state.routes[0]?.name ===
|
||||
(Constants.UserSettingsSections as Record<string, string>).OVERVIEW
|
||||
)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue