@@ -12,19 +12,21 @@ import useResponsiveLayout from './useResponsiveLayout';
1212 * @return Function that checks if the route where the component was rendered matches the currently active route.
1313 */
1414function 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+ // Determines whether the component is still rendered on the initial rendered route.
16+ const isOnInitialRenderedRouteRef = useRef < boolean | undefined > ( undefined ) ;
1817 const navigation = useNavigation ( ) ;
1918 const { shouldUseNarrowLayout, isInNarrowPaneModal} = useResponsiveLayout ( ) ;
2019
20+ /**
21+ * Return true only when on the initial rendered route or on the attachment modal route whose source parameter equals videoUrl.
22+ */
2123 const hasRouteRemainedUnchanged = useCallback ( ( ) => {
2224 if ( navigation . isFocused ( ) ) {
2325 return true ;
2426 }
2527
26- // If navigating outside the rendered route and attachment modal route
27- if ( ! hasOnRenderedRouteRef . current ) {
28+ // If navigating away from the initial rendered route or attachment route
29+ if ( ! isOnInitialRenderedRouteRef . current ) {
2830 return false ;
2931 }
3032
@@ -35,8 +37,8 @@ function useCheckIfRouteHasRemainedUnchanged(videoUrl: string) {
3537 currentRoute ?. params &&
3638 'source' in currentRoute . params &&
3739 currentRoute . params . source === videoUrl &&
38- // Because the video player is shared only on desktop
39- // Allow in RHP
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
4042 ( ! shouldUseNarrowLayout || isInNarrowPaneModal )
4143 ) {
4244 return true ;
@@ -48,23 +50,24 @@ function useCheckIfRouteHasRemainedUnchanged(videoUrl: string) {
4850 // Initialize and check if starting with the attachment modal
4951 useEffect ( ( ) => {
5052 Navigation . isNavigationReady ( ) . then ( ( ) => {
51- if ( hasOnRenderedRouteRef . current !== undefined ) {
53+ if ( isOnInitialRenderedRouteRef . current !== undefined ) {
5254 return ;
5355 }
5456
5557 const route = navigationRef . getCurrentRoute ( ) ;
56- // If the app is opened via the attachment route, it will always remain on the report screen.
58+ // If the app is launched with the attachment route, it will always remain on the report screen.
5759 // Thus, it can be considered as still being on the rendered route.
58- hasOnRenderedRouteRef . current = route ?. name === SCREENS . ATTACHMENTS ;
60+ isOnInitialRenderedRouteRef . current = route ?. name === SCREENS . ATTACHMENTS ;
5961 } ) ;
6062 } , [ ] ) ;
6163
62- // Update the route reference on 'blur' events, except when opening attachments modal
6364 useEffect ( ( ) => {
65+ // When the 'focus' event is emitted, it means we've returned to the initial rendered route
6466 const unsubscribeFocus = navigation . addListener ( 'focus' , ( ) => {
65- hasOnRenderedRouteRef . current = true ;
67+ isOnInitialRenderedRouteRef . current = true ;
6668 } ) ;
6769
70+ // Update the state indicating we've left the initial rendered route on 'blur' events, except when opening the attachments modal
6871 const unsubscribeBlur = navigation . addListener ( 'blur' , ( ) => {
6972 const route = navigationRef . getCurrentRoute ( ) ;
7073
@@ -73,7 +76,7 @@ function useCheckIfRouteHasRemainedUnchanged(videoUrl: string) {
7376 return ;
7477 }
7578
76- hasOnRenderedRouteRef . current = false ;
79+ isOnInitialRenderedRouteRef . current = false ;
7780 } ) ;
7881
7982 return ( ) => {
0 commit comments