2020-01-06 19:32:35 +01:00
|
|
|
// @flow
|
|
|
|
import React, { useState, useEffect } from 'react';
|
2020-04-01 20:43:50 +02:00
|
|
|
import LoadingScreen from 'component/common/loading-screen';
|
2020-04-25 10:24:42 +02:00
|
|
|
import { NON_STREAM_MODES } from 'constants/file_render_modes';
|
2020-01-06 19:32:35 +01:00
|
|
|
|
2021-06-11 08:06:29 +02:00
|
|
|
const FileRender = React.lazy(() => import('component/fileRender' /* webpackChunkName: "fileRender" */));
|
|
|
|
|
2020-01-06 19:32:35 +01:00
|
|
|
type Props = {
|
|
|
|
isPlaying: boolean,
|
|
|
|
fileInfo: FileListItem,
|
|
|
|
uri: string,
|
2020-04-01 20:43:50 +02:00
|
|
|
renderMode: string,
|
2020-01-06 19:32:35 +01:00
|
|
|
streamingUrl?: string,
|
|
|
|
triggerAnalyticsView: (string, number) => Promise<any>,
|
|
|
|
claimRewards: () => void,
|
2021-05-24 10:52:30 +02:00
|
|
|
costInfo: any,
|
|
|
|
claimWasPurchased: boolean,
|
2020-01-06 19:32:35 +01:00
|
|
|
};
|
|
|
|
|
2020-04-01 20:43:50 +02:00
|
|
|
export default function FileRenderInline(props: Props) {
|
2021-05-24 10:52:30 +02:00
|
|
|
const {
|
|
|
|
isPlaying,
|
|
|
|
fileInfo,
|
|
|
|
uri,
|
|
|
|
streamingUrl,
|
|
|
|
triggerAnalyticsView,
|
|
|
|
claimRewards,
|
|
|
|
renderMode,
|
|
|
|
costInfo,
|
|
|
|
claimWasPurchased,
|
|
|
|
} = props;
|
2020-01-06 19:32:35 +01:00
|
|
|
const [playTime, setPlayTime] = useState();
|
2021-05-24 10:52:30 +02:00
|
|
|
const isFree = !costInfo || (costInfo.cost !== undefined && costInfo.cost === 0);
|
2020-04-25 10:24:42 +02:00
|
|
|
const isReadyToView = fileInfo && fileInfo.completed;
|
|
|
|
const isReadyToPlay = streamingUrl || isReadyToView;
|
|
|
|
|
|
|
|
// Render if any source is ready
|
|
|
|
let renderContent = isReadyToPlay;
|
|
|
|
|
|
|
|
// Force non-streaming content to wait for download
|
|
|
|
if (NON_STREAM_MODES.includes(renderMode)) {
|
|
|
|
renderContent = isReadyToView;
|
|
|
|
}
|
2020-01-06 19:32:35 +01:00
|
|
|
|
|
|
|
useEffect(() => {
|
2020-04-08 23:01:14 +02:00
|
|
|
if (isPlaying) {
|
2020-01-06 19:32:35 +01:00
|
|
|
setPlayTime(Date.now());
|
|
|
|
}
|
2020-04-08 23:01:14 +02:00
|
|
|
}, [isPlaying, setPlayTime, uri]);
|
2020-01-06 19:32:35 +01:00
|
|
|
|
|
|
|
useEffect(() => {
|
2020-04-14 01:48:11 +02:00
|
|
|
/*
|
|
|
|
note: code can currently double fire with videoViewer logic if a video is rendered by FileRenderInline (currently this never happens)
|
|
|
|
*/
|
2020-04-08 23:01:14 +02:00
|
|
|
if (playTime && isReadyToPlay) {
|
2020-01-06 19:32:35 +01:00
|
|
|
const timeToStart = Date.now() - playTime;
|
2020-04-08 23:01:14 +02:00
|
|
|
|
2020-01-06 19:32:35 +01:00
|
|
|
triggerAnalyticsView(uri, timeToStart).then(() => {
|
|
|
|
claimRewards();
|
|
|
|
setPlayTime(null);
|
|
|
|
});
|
|
|
|
}
|
2020-04-08 23:01:14 +02:00
|
|
|
}, [setPlayTime, claimRewards, triggerAnalyticsView, isReadyToPlay, playTime, uri]);
|
2020-01-06 19:32:35 +01:00
|
|
|
|
2020-04-01 20:43:50 +02:00
|
|
|
if (!isPlaying) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-05-24 10:52:30 +02:00
|
|
|
if (!isFree && !claimWasPurchased) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-06-11 08:06:29 +02:00
|
|
|
return renderContent ? (
|
|
|
|
<React.Suspense fallback={null}>
|
|
|
|
<FileRender uri={uri} />
|
|
|
|
</React.Suspense>
|
|
|
|
) : (
|
|
|
|
<LoadingScreen isDocument />
|
|
|
|
);
|
2020-01-06 19:32:35 +01:00
|
|
|
}
|