|
1 | 1 | import {useNavigation} from '@react-navigation/native'; |
2 | 2 | import {useCallback, useEffect, useRef} from 'react'; |
3 | | -import Navigation from '@navigation/Navigation'; |
4 | | -import ROUTES from '@src/ROUTES'; |
| 3 | +import Navigation, {navigationRef} from '@navigation/Navigation'; |
| 4 | +import SCREENS from '@src/SCREENS'; |
| 5 | +import useResponsiveLayout from './useResponsiveLayout'; |
5 | 6 |
|
6 | 7 | /** |
7 | | - * Hook that returns a function to check if the currently active route remains the same as the last known route. |
8 | | - * The last known route reference is updated every time the component experiences a 'blur' event, |
9 | | - * except when opening an attachments modal, which is treated as an exception and does not trigger a reference update. |
| 8 | + * Hook that returns a function to check if the currently active route remains the same as the route on which the component was initially rendered. |
| 9 | + * The `isOnInitialRenderedRouteRef` is set to `false` every time the component experiences a 'blur' event, |
| 10 | + * except when opening an attachments modal, which is treated as an exception and does not trigger a reference update because the attachments modal display overlap and we want to use shared VideoPlayer. |
10 | 11 | * |
11 | | - * @return Function that checks if the last known route matches the currently active route. |
| 12 | + * @return Function that checks if the route where the component was initially rendered matches the current active route. |
12 | 13 | */ |
13 | | -function useCheckIfRouteHasRemainedUnchanged(): () => boolean { |
14 | | - const lastKnownRouteRef = useRef<string | undefined>(undefined); |
| 14 | +function useCheckIfRouteHasRemainedUnchanged(videoUrl: string) { |
| 15 | + // Determines whether the component is still rendered on the initially rendered route. |
| 16 | + const isOnInitialRenderedRouteRef = useRef<boolean | undefined>(undefined); |
15 | 17 | const navigation = useNavigation(); |
| 18 | + const {shouldUseNarrowLayout, isInNarrowPaneModal} = useResponsiveLayout(); |
16 | 19 |
|
17 | | - // Function to compare the last known route with the current active route |
| 20 | + /** |
| 21 | + * Return true only when on the initially rendered route or the video is currently playing in attachment modal. |
| 22 | + */ |
18 | 23 | const hasRouteRemainedUnchanged = useCallback(() => { |
19 | | - return lastKnownRouteRef.current === Navigation.getActiveRouteWithoutParams(); |
20 | | - }, []); |
| 24 | + if (navigation.isFocused()) { |
| 25 | + return true; |
| 26 | + } |
| 27 | + |
| 28 | + // If navigating away from the initially rendered route or attachment route |
| 29 | + if (!isOnInitialRenderedRouteRef.current) { |
| 30 | + return false; |
| 31 | + } |
| 32 | + |
| 33 | + // If on AttachmentModal, only play when the source parameters match videoUrl ensures correct play VideoPlayer share for this one |
| 34 | + const currentRoute = navigationRef.getCurrentRoute(); |
| 35 | + if ( |
| 36 | + currentRoute?.name === SCREENS.ATTACHMENTS && |
| 37 | + currentRoute?.params && |
| 38 | + 'source' in currentRoute.params && |
| 39 | + currentRoute.params.source === videoUrl && |
| 40 | + // Because the video player is shared only on large screens |
| 41 | + // Allow in RHP in cases where we're in RHP on the Search page |
| 42 | + (!shouldUseNarrowLayout || isInNarrowPaneModal) |
| 43 | + ) { |
| 44 | + return true; |
| 45 | + } |
21 | 46 |
|
22 | | - // Initialize the initial route when navigation is ready |
| 47 | + return false; |
| 48 | + }, [shouldUseNarrowLayout, isInNarrowPaneModal, videoUrl, navigation]); |
| 49 | + |
| 50 | + // Initialize and check if starting with the attachment modal |
23 | 51 | useEffect(() => { |
24 | 52 | Navigation.isNavigationReady().then(() => { |
25 | | - if (lastKnownRouteRef.current !== undefined) { |
| 53 | + if (isOnInitialRenderedRouteRef.current !== undefined) { |
26 | 54 | return; |
27 | 55 | } |
28 | 56 |
|
29 | | - lastKnownRouteRef.current = Navigation.getActiveRouteWithoutParams(); |
| 57 | + const route = navigationRef.getCurrentRoute(); |
| 58 | + // If the app is launched with the attachment route, it will always remain on the report screen. |
| 59 | + // Thus, it can be considered as still being on the rendered route. |
| 60 | + isOnInitialRenderedRouteRef.current = navigation.isFocused() || route?.name === SCREENS.ATTACHMENTS; |
30 | 61 | }); |
| 62 | + // eslint-disable-next-line react-compiler/react-compiler |
| 63 | + // eslint-disable-next-line react-hooks/exhaustive-deps |
31 | 64 | }, []); |
32 | 65 |
|
33 | | - // Update the route reference on 'blur' events, except when opening attachments modal |
34 | 66 | useEffect(() => { |
35 | | - return navigation.addListener('blur', () => { |
36 | | - const currentRoute = Navigation.getActiveRouteWithoutParams(); |
37 | | - if (currentRoute === `/${ROUTES.ATTACHMENTS.route}`) { |
| 67 | + const unsubscribeFocus = navigation.addListener('focus', () => { |
| 68 | + isOnInitialRenderedRouteRef.current = true; |
| 69 | + }); |
| 70 | + |
| 71 | + const unsubscribeBlur = navigation.addListener('blur', () => { |
| 72 | + const route = navigationRef.getCurrentRoute(); |
| 73 | + |
| 74 | + if (route?.name === SCREENS.ATTACHMENTS) { |
38 | 75 | // Skip route update when attachment modal is opened |
39 | 76 | return; |
40 | 77 | } |
41 | | - lastKnownRouteRef.current = currentRoute; |
| 78 | + |
| 79 | + isOnInitialRenderedRouteRef.current = false; |
42 | 80 | }); |
| 81 | + |
| 82 | + return () => { |
| 83 | + unsubscribeFocus(); |
| 84 | + unsubscribeBlur(); |
| 85 | + }; |
43 | 86 | }, [navigation]); |
44 | 87 |
|
45 | 88 | return hasRouteRemainedUnchanged; |
|
0 commit comments