From 35ca73f531200eca5d39e1acdcb2516dd4bb58b8 Mon Sep 17 00:00:00 2001 From: PalmDevs Date: Fri, 18 Jul 2025 18:26:17 +0700 Subject: [PATCH] fix(lib/discord/modules/settings): refactor and get updated state before replacing in `refreshSettingsOverviewScreen` --- lib/discord/src/modules/settings/index.ts | 69 ++++++++++++----------- 1 file changed, 36 insertions(+), 33 deletions(-) diff --git a/lib/discord/src/modules/settings/index.ts b/lib/discord/src/modules/settings/index.ts index 0af682e..09f6a5f 100644 --- a/lib/discord/src/modules/settings/index.ts +++ b/lib/discord/src/modules/settings/index.ts @@ -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) - .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) + index: settingsState!.routes.length - 1, + routes: settingsState!.routes, + } as PartialState>) }) } 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).OVERVIEW + ) }