|
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, |
| 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 `isFocused` is set to `false` every time the component experiences a 'blur' event, |
9 | 10 | * except when opening an attachments modal, which is treated as an exception and does not trigger a reference update. |
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 rendered matches the currently active route. |
12 | 13 | */ |
13 | | -function useCheckIfRouteHasRemainedUnchanged(): () => boolean { |
14 | | - const lastKnownRouteRef = useRef<string | undefined>(undefined); |
| 14 | +function useCheckIfRouteHasRemainedUnchanged(videoUrl: string) { |
| 15 | + // Ref stores a value determining whether the component is still rendered on the route. |
| 16 | + // If the value is false, it immediately returns false; if true, it checks whether it is in the attachment modal. |
| 17 | + const hasOnRenderedRouteRef = useRef<boolean | undefined>(undefined); |
15 | 18 | const navigation = useNavigation(); |
| 19 | + const {shouldUseNarrowLayout, isInNarrowPaneModal} = useResponsiveLayout(); |
16 | 20 |
|
17 | | - // Function to compare the last known route with the current active route |
18 | 21 | const hasRouteRemainedUnchanged = useCallback(() => { |
19 | | - return lastKnownRouteRef.current === Navigation.getActiveRouteWithoutParams(); |
20 | | - }, []); |
| 22 | + if (navigation.isFocused()) { |
| 23 | + return true; |
| 24 | + } |
| 25 | + |
| 26 | + // If navigating outside the rendered route and attachment modal route |
| 27 | + if (!hasOnRenderedRouteRef.current) { |
| 28 | + return false; |
| 29 | + } |
| 30 | + |
| 31 | + // If on AttachmentModal, only play when the source parameters match videoUrl |
| 32 | + const currentRoute = navigationRef.getCurrentRoute(); |
| 33 | + if ( |
| 34 | + currentRoute?.name === SCREENS.ATTACHMENTS && |
| 35 | + currentRoute?.params && |
| 36 | + 'source' in currentRoute.params && |
| 37 | + currentRoute.params.source === videoUrl && |
| 38 | + // Because the video player is shared only on desktop |
| 39 | + // Allow in RHP |
| 40 | + (!shouldUseNarrowLayout || isInNarrowPaneModal) |
| 41 | + ) { |
| 42 | + return true; |
| 43 | + } |
21 | 44 |
|
22 | | - // Initialize the initial route when navigation is ready |
| 45 | + return false; |
| 46 | + }, [shouldUseNarrowLayout, isInNarrowPaneModal, videoUrl, navigation]); |
| 47 | + |
| 48 | + // Initialize and check if starting with the attachment modal |
23 | 49 | useEffect(() => { |
24 | 50 | Navigation.isNavigationReady().then(() => { |
25 | | - if (lastKnownRouteRef.current !== undefined) { |
| 51 | + if (hasOnRenderedRouteRef.current !== undefined) { |
26 | 52 | return; |
27 | 53 | } |
28 | 54 |
|
29 | | - lastKnownRouteRef.current = Navigation.getActiveRouteWithoutParams(); |
| 55 | + const route = navigationRef.getCurrentRoute(); |
| 56 | + // If the app is opened via the attachment route, it will always remain on the report screen. |
| 57 | + // Thus, it can be considered as still being on the rendered route. |
| 58 | + hasOnRenderedRouteRef.current = route?.name === SCREENS.ATTACHMENTS; |
30 | 59 | }); |
31 | 60 | }, []); |
32 | 61 |
|
33 | 62 | // Update the route reference on 'blur' events, except when opening attachments modal |
34 | 63 | useEffect(() => { |
35 | | - return navigation.addListener('blur', () => { |
36 | | - const currentRoute = Navigation.getActiveRouteWithoutParams(); |
37 | | - if (currentRoute === `/${ROUTES.ATTACHMENTS.route}`) { |
| 64 | + const unsubscribeFocus = navigation.addListener('focus', () => { |
| 65 | + hasOnRenderedRouteRef.current = true; |
| 66 | + }); |
| 67 | + |
| 68 | + const unsubscribeBlur = navigation.addListener('blur', () => { |
| 69 | + const route = navigationRef.getCurrentRoute(); |
| 70 | + |
| 71 | + if (route?.name === SCREENS.ATTACHMENTS) { |
38 | 72 | // Skip route update when attachment modal is opened |
39 | 73 | return; |
40 | 74 | } |
41 | | - lastKnownRouteRef.current = currentRoute; |
| 75 | + |
| 76 | + hasOnRenderedRouteRef.current = false; |
42 | 77 | }); |
| 78 | + |
| 79 | + return () => { |
| 80 | + unsubscribeFocus(); |
| 81 | + unsubscribeBlur(); |
| 82 | + }; |
43 | 83 | }, [navigation]); |
44 | 84 |
|
45 | 85 | return hasRouteRemainedUnchanged; |
|
0 commit comments