Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix multiple console errors when login and in onboarding welcome video #46854

Merged
Merged
4 changes: 2 additions & 2 deletions src/components/FeatureTrainingModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ function FeatureTrainingModal({
<View style={[styles.mh100, isMediumOrLargerScreenWidth && styles.welcomeVideoNarrowLayout, safeAreaPaddingBottomStyle]}>
<View style={isMediumOrLargerScreenWidth ? {padding: MODAL_PADDING} : {paddingHorizontal: MODAL_PADDING}}>{renderIllustration()}</View>
<View style={[styles.mt5, styles.mh5]}>
{title && description && (
{!!title && !!description && (
<View style={[isMediumOrLargerScreenWidth ? [styles.gap1, styles.mb8] : [styles.mb10]]}>
<Text style={[styles.textHeadlineH1]}>{title}</Text>
<Text style={styles.textSupporting}>{description}</Text>
Expand All @@ -220,7 +220,7 @@ function FeatureTrainingModal({
onInputChange={toggleWillShowAgain}
/>
)}
{helpText && (
{!!helpText && (
<Button
large
style={[styles.mb3]}
Expand Down
5 changes: 4 additions & 1 deletion src/components/VideoPlayer/BaseVideoPlayer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -292,12 +292,15 @@ function BaseVideoPlayer({
updateCurrentlyPlayingURL(url);
}, [shouldPlay, updateCurrentlyPlayingURL, url]);

useEffect(() => {
videoPlayerRef.current?.setStatusAsync({volume: 0});
}, []);

return (
<>
{/* We need to wrap the video component in a component that will catch unhandled pointer events. Otherwise, these
events will bubble up the tree, and it will cause unexpected press behavior. */}
<PressableWithoutFeedback
accessibilityRole="button"
accessible={false}
style={[styles.cursorDefault, style]}
>
Expand Down
26 changes: 15 additions & 11 deletions src/components/VideoPlayerContexts/PlaybackContext.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import type {AVPlaybackStatusToSet} from 'expo-av';
import type {AVPlaybackStatus, AVPlaybackStatusToSet} from 'expo-av';
import React, {useCallback, useContext, useEffect, useMemo, useRef, useState} from 'react';
import type {View} from 'react-native';
import type {VideoWithOnFullScreenUpdate} from '@components/VideoPlayer/types';
import useCurrentReportID from '@hooks/useCurrentReportID';
import usePrevious from '@hooks/usePrevious';
import type ChildrenProps from '@src/types/utils/ChildrenProps';
import type {PlaybackContext, StatusCallback} from './types';

Expand All @@ -15,7 +16,9 @@ function PlaybackContextProvider({children}: ChildrenProps) {
const [originalParent, setOriginalParent] = useState<View | HTMLDivElement | null>(null);
const currentVideoPlayerRef = useRef<VideoWithOnFullScreenUpdate | null>(null);
const {currentReportID} = useCurrentReportID() ?? {};
const prevCurrentReportID = usePrevious(currentReportID);
const videoResumeTryNumberRef = useRef<number>(0);
const playVideoPromiseRef = useRef<Promise<AVPlaybackStatus>>();

const pauseVideo = useCallback(() => {
currentVideoPlayerRef.current?.setStatusAsync?.({shouldPlay: false});
Expand All @@ -31,7 +34,7 @@ function PlaybackContextProvider({children}: ChildrenProps) {
if ('durationMillis' in status && status.durationMillis === status.positionMillis) {
newStatus.positionMillis = 0;
}
currentVideoPlayerRef.current?.setStatusAsync(newStatus);
playVideoPromiseRef.current = currentVideoPlayerRef.current?.setStatusAsync(newStatus);
});
}, [currentVideoPlayerRef]);

Expand Down Expand Up @@ -73,21 +76,22 @@ function PlaybackContextProvider({children}: ChildrenProps) {
);

const resetVideoPlayerData = useCallback(() => {
videoResumeTryNumberRef.current = 0;
stopVideo();
setCurrentlyPlayingURL(null);
setSharedElement(null);
setOriginalParent(null);
currentVideoPlayerRef.current = null;
unloadVideo();
(playVideoPromiseRef.current ?? Promise.resolve()).then(stopVideo).finally(() => {
videoResumeTryNumberRef.current = 0;
setCurrentlyPlayingURL(null);
setSharedElement(null);
setOriginalParent(null);
currentVideoPlayerRef.current = null;
unloadVideo();
});
}, [stopVideo, unloadVideo]);

useEffect(() => {
if (!currentReportID) {
if (!currentReportID || !prevCurrentReportID || currentReportID === '-1' || prevCurrentReportID === '-1') {
return;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @bernhardoj it appears that you applied 2 solutions in your proposal or error 7th , didn't you?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. The first one waits until the play promise is completed. But on Android mWeb (if I remember the platform correctly), the play is never triggered anymore after the video is stopped. So,

  1. play is called
  2. resetVideoPlayer is called because currentReportID changes from an empty string to something
  3. There is no play called anymore like in other platforms, so the video is completely stopped and (ref) cleared.

Copy link
Contributor

@hoangzinh hoangzinh Aug 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hey @bernhardoj do you mean the checking isPlaying in 1st option is not useful in Android mWeb? Or does it never return isPlaying status in Android mWeb?

Copy link
Contributor

@hoangzinh hoangzinh Aug 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, at this line only (not this error log), we only need to check !currentReportID || currentReportID === '-1' is enough, what do you think? And do you think we should make -1 as a constant?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, what I mean is that, when we open the video, the play video is called multiple times.

const playVideo = useCallback(() => {
currentVideoPlayerRef.current?.getStatusAsync?.().then((status) => {
const newStatus: AVPlaybackStatusToSet = {shouldPlay: true};
if ('durationMillis' in status && status.durationMillis === status.positionMillis) {
newStatus.positionMillis = 0;
}
currentVideoPlayerRef.current?.setStatusAsync(newStatus);
});
}, [currentVideoPlayerRef]);

And then resetVideoPlayerData is also called once. On other platforms, the order of call looks like this:
Screenshot 2024-08-07 at 22 19 53

In mWeb Android, it's
play
play
...
reset
[no more call]

But I just retested it and the call order is similar to other platforms too.

we only need to check !currentReportID || currentReportID === '-1' is enough, what do you think?

Still, we need to prevent the reset to be called, so we need to check for prevCurrentReportID too.

And do you think we should make -1 as a constant?

I don't have a strong opinion about this one. There is -1 and '-1' and we use this everywhere on the app without const. Maybe we can have a new cleanup issue to create the const and replace it everywhere in the app.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay cool. Can you leave a comment explaining why we have to check both current and prev, also another comment in line 79? I think it's useful to back-read later.

(playVideoPromiseRef.current ?? Promise.resolve()).then(stopVideo).finally(() => {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added comment

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @bernhardoj

}
resetVideoPlayerData();
}, [currentReportID, resetVideoPlayerData]);
}, [currentReportID, prevCurrentReportID, resetVideoPlayerData]);

const contextValue = useMemo(
() => ({
Expand Down
2 changes: 1 addition & 1 deletion src/libs/actions/Session/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -677,7 +677,7 @@ function clearSignInData() {
*/
function resetHomeRouteParams() {
Navigation.isNavigationReady().then(() => {
const routes = navigationRef.current?.getState().routes;
const routes = navigationRef.current?.getState()?.routes;
const homeRoute = routes?.find((route) => route.name === SCREENS.HOME);

const emptyParams: Record<string, undefined> = {};
Expand Down
Loading