Skip to content

Commit d202e29

Browse files
committed
updated for clearer explanation
1 parent 74a23ef commit d202e29

2 files changed

Lines changed: 20 additions & 17 deletions

File tree

src/components/VideoPlayerPreview/index.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ function VideoPlayerPreview({videoUrl, thumbnailUrl, reportID, fileName, videoDi
6565
const {isOnSearch} = useSearchContext();
6666
const navigation = useNavigation();
6767

68-
// We want to play the video only when the user is on the page where it was rendered
69-
const isUserRemainOnFirstRenderRoute = useCheckIfRouteHasRemainedUnchanged(videoUrl);
68+
// We want to play the video only when the user is on the page where it was initially rendered
69+
const doesUserRemainOnFirstRenderRoute = useCheckIfRouteHasRemainedUnchanged(videoUrl);
7070

7171
// `onVideoLoaded` is passed to VideoPlayerPreview's `Video` element which is displayed only on web.
7272
// VideoReadyForDisplayEvent type is lacking srcElement, that's why it's added here
@@ -86,12 +86,12 @@ function VideoPlayerPreview({videoUrl, thumbnailUrl, reportID, fileName, videoDi
8686
}, [navigation]);
8787

8888
useEffect(() => {
89-
const isFocused = isUserRemainOnFirstRenderRoute();
89+
const isFocused = doesUserRemainOnFirstRenderRoute();
9090
if (videoUrl !== currentlyPlayingURL || reportID !== currentRouteReportID || !isFocused) {
9191
return;
9292
}
9393
setIsThumbnail(false);
94-
}, [currentlyPlayingURL, currentRouteReportID, updateCurrentURLAndReportID, videoUrl, reportID, isUserRemainOnFirstRenderRoute, isOnSearch]);
94+
}, [currentlyPlayingURL, currentRouteReportID, updateCurrentURLAndReportID, videoUrl, reportID, doesUserRemainOnFirstRenderRoute, isOnSearch]);
9595

9696
return (
9797
<View style={[styles.webViewStyles.tagStyles.video, thumbnailDimensionsStyles]}>

src/hooks/useCheckIfRouteHasRemainedUnchanged.ts

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -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
*/
1414
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+
// 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

Comments
 (0)