From 220d8666ec2718ee1f9ea002456dfc382589aa81 Mon Sep 17 00:00:00 2001
From: gaber <a.gaber0093@gmail.com>
Date: Tue, 2 Apr 2024 18:01:20 +0200
Subject: [PATCH] update getPartialStateDiff.ts fullscreen diff

---
 .../AppNavigator/getPartialStateDiff.ts       | 18 ++++++++---
 .../Navigation/getTopmostFullScreenRoute.ts   | 30 +++++++++++++++++++
 2 files changed, 44 insertions(+), 4 deletions(-)
 create mode 100644 src/libs/Navigation/getTopmostFullScreenRoute.ts

diff --git a/src/libs/Navigation/AppNavigator/getPartialStateDiff.ts b/src/libs/Navigation/AppNavigator/getPartialStateDiff.ts
index b0825b4b299..5732cb93f19 100644
--- a/src/libs/Navigation/AppNavigator/getPartialStateDiff.ts
+++ b/src/libs/Navigation/AppNavigator/getPartialStateDiff.ts
@@ -1,5 +1,6 @@
 import getTopmostBottomTabRoute from '@libs/Navigation/getTopmostBottomTabRoute';
 import getTopmostCentralPaneRoute from '@libs/Navigation/getTopmostCentralPaneRoute';
+import getTopmostFullScreenRoute from '@libs/Navigation/getTopmostFullScreenRoute';
 import type {Metainfo} from '@libs/Navigation/linkingConfig/getAdaptedStateFromPath';
 import type {NavigationPartialRoute, RootStackParamList, State} from '@libs/Navigation/types';
 import NAVIGATORS from '@src/NAVIGATORS';
@@ -73,10 +74,19 @@ function getPartialStateDiff(state: State<RootStackParamList>, templateState: St
     // This one is heuristic and may need to be improved if we will be able to navigate from modal screen with full screen in background to another modal screen with full screen in background.
     // For now this simple check is enough.
     if (metainfo.isFullScreenNavigatorMandatory) {
-        const stateTopmostFullScreen = state.routes.filter((route) => route.name === NAVIGATORS.FULL_SCREEN_NAVIGATOR).at(-1);
-        const templateStateTopmostFullScreen = templateState.routes.filter((route) => route.name === NAVIGATORS.FULL_SCREEN_NAVIGATOR).at(-1) as NavigationPartialRoute;
-        if (!stateTopmostFullScreen && templateStateTopmostFullScreen) {
-            diff[NAVIGATORS.FULL_SCREEN_NAVIGATOR] = templateStateTopmostFullScreen;
+        const stateTopmostFullScreen = getTopmostFullScreenRoute(state);
+        const templateStateTopmostFullScreen = getTopmostFullScreenRoute(templateState);
+        const fullScreenDiff = templateState.routes.filter((route) => route.name === NAVIGATORS.FULL_SCREEN_NAVIGATOR).at(-1) as NavigationPartialRoute;
+
+        if (
+            // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
+            (!stateTopmostFullScreen && templateStateTopmostFullScreen) ||
+            (stateTopmostFullScreen &&
+                templateStateTopmostFullScreen &&
+                stateTopmostFullScreen.name !== templateStateTopmostFullScreen.name &&
+                !shallowCompare(stateTopmostFullScreen.params, templateStateTopmostFullScreen.params))
+        ) {
+            diff[NAVIGATORS.FULL_SCREEN_NAVIGATOR] = fullScreenDiff;
         }
     }
 
diff --git a/src/libs/Navigation/getTopmostFullScreenRoute.ts b/src/libs/Navigation/getTopmostFullScreenRoute.ts
new file mode 100644
index 00000000000..25c74ea0ce6
--- /dev/null
+++ b/src/libs/Navigation/getTopmostFullScreenRoute.ts
@@ -0,0 +1,30 @@
+import NAVIGATORS from '@src/NAVIGATORS';
+import type {FullScreenName, NavigationPartialRoute, RootStackParamList, State} from './types';
+
+// Get the name of topmost fullscreen route in the navigation stack.
+function getTopmostFullScreenRoute(state: State<RootStackParamList>): NavigationPartialRoute<FullScreenName> | undefined {
+    if (!state) {
+        return;
+    }
+
+    const topmostFullScreenRoute = state.routes.filter((route) => route.name === NAVIGATORS.FULL_SCREEN_NAVIGATOR).at(-1);
+
+    if (!topmostFullScreenRoute) {
+        return;
+    }
+
+    if (!!topmostFullScreenRoute.params && 'screen' in topmostFullScreenRoute.params) {
+        return {name: topmostFullScreenRoute.params.screen as FullScreenName, params: topmostFullScreenRoute.params.params};
+    }
+
+    if (!topmostFullScreenRoute.state) {
+        return;
+    }
+
+    // There will be at least one route in the fullscreen navigator.
+    const {name, params} = topmostFullScreenRoute.state.routes.at(-1) as NavigationPartialRoute<FullScreenName>;
+
+    return {name, params};
+}
+
+export default getTopmostFullScreenRoute;
