2018-03-26 23:32:43 +02:00
|
|
|
// @flow
|
2019-08-13 07:35:13 +02:00
|
|
|
import * as ICONS from 'constants/icons';
|
2019-08-14 05:04:08 +02:00
|
|
|
import React, { useState, useEffect } from 'react';
|
2019-08-13 07:35:13 +02:00
|
|
|
import Button from 'component/button';
|
2018-03-26 23:32:43 +02:00
|
|
|
import classnames from 'classnames';
|
2018-07-05 04:49:12 +02:00
|
|
|
import LoadingScreen from 'component/common/loading-screen';
|
2019-08-02 08:28:14 +02:00
|
|
|
import FileRender from 'component/fileRender';
|
2019-08-13 07:35:13 +02:00
|
|
|
import UriIndicator from 'component/uriIndicator';
|
2019-09-27 20:56:15 +02:00
|
|
|
import usePersistedState from 'effects/use-persisted-state';
|
|
|
|
import usePrevious from 'effects/use-previous';
|
2020-01-06 19:32:35 +01:00
|
|
|
import { FILE_WRAPPER_CLASS } from 'component/layoutWrapperFile/view';
|
2019-08-13 07:35:13 +02:00
|
|
|
import Draggable from 'react-draggable';
|
|
|
|
import Tooltip from 'component/common/tooltip';
|
2019-10-14 00:28:12 +02:00
|
|
|
import { onFullscreenChange } from 'util/full-screen';
|
2019-12-18 06:27:08 +01:00
|
|
|
import useIsMobile from 'effects/use-is-mobile';
|
2018-05-16 21:34:38 +02:00
|
|
|
|
2018-03-26 23:32:43 +02:00
|
|
|
type Props = {
|
2019-08-02 08:28:14 +02:00
|
|
|
mediaType: string,
|
2019-11-26 20:08:34 +01:00
|
|
|
contentType: string,
|
2020-01-06 21:57:49 +01:00
|
|
|
isText: boolean,
|
2018-03-26 23:32:43 +02:00
|
|
|
isLoading: boolean,
|
2019-08-02 08:28:14 +02:00
|
|
|
isPlaying: boolean,
|
2019-08-06 05:25:33 +02:00
|
|
|
fileInfo: FileListItem,
|
2018-03-26 23:32:43 +02:00
|
|
|
uri: string,
|
2019-08-02 08:28:14 +02:00
|
|
|
obscurePreview: boolean,
|
2019-04-03 06:17:00 +02:00
|
|
|
insufficientCredits: boolean,
|
2019-08-02 08:28:14 +02:00
|
|
|
isStreamable: boolean,
|
|
|
|
thumbnail?: string,
|
|
|
|
streamingUrl?: string,
|
2019-08-13 07:35:13 +02:00
|
|
|
floatingPlayer: boolean,
|
|
|
|
pageUri: ?string,
|
|
|
|
title: ?string,
|
|
|
|
floatingPlayerEnabled: boolean,
|
|
|
|
clearPlayingUri: () => void,
|
2019-09-18 20:41:20 +02:00
|
|
|
triggerAnalyticsView: (string, number) => Promise<any>,
|
2019-08-15 16:26:20 +02:00
|
|
|
claimRewards: () => void,
|
2018-03-26 23:32:43 +02:00
|
|
|
};
|
2017-04-23 11:56:50 +02:00
|
|
|
|
2019-08-02 08:28:14 +02:00
|
|
|
export default function FileViewer(props: Props) {
|
|
|
|
const {
|
|
|
|
isPlaying,
|
|
|
|
fileInfo,
|
|
|
|
uri,
|
|
|
|
streamingUrl,
|
|
|
|
isStreamable,
|
2019-08-13 07:35:13 +02:00
|
|
|
pageUri,
|
|
|
|
title,
|
|
|
|
clearPlayingUri,
|
|
|
|
floatingPlayerEnabled,
|
2019-08-14 05:04:08 +02:00
|
|
|
triggerAnalyticsView,
|
2019-08-15 16:26:20 +02:00
|
|
|
claimRewards,
|
2019-09-17 21:03:54 +02:00
|
|
|
mediaType,
|
2019-11-26 20:08:34 +01:00
|
|
|
contentType,
|
2020-01-06 21:57:49 +01:00
|
|
|
isText,
|
2019-08-02 08:28:14 +02:00
|
|
|
} = props;
|
2019-12-18 06:27:08 +01:00
|
|
|
const isMobile = useIsMobile();
|
2019-08-14 05:04:08 +02:00
|
|
|
const [playTime, setPlayTime] = useState();
|
2019-08-13 07:35:13 +02:00
|
|
|
const [fileViewerRect, setFileViewerRect] = usePersistedState('inline-file-viewer:rect');
|
|
|
|
const [position, setPosition] = usePersistedState('floating-file-viewer:position', {
|
|
|
|
x: -25,
|
|
|
|
y: window.innerHeight - 400,
|
|
|
|
});
|
2019-08-02 08:28:14 +02:00
|
|
|
|
2019-08-13 07:35:13 +02:00
|
|
|
const inline = pageUri === uri;
|
2019-11-28 17:27:30 +01:00
|
|
|
const forceVideo = ['application/x-ext-mkv', 'video/x-matroska'].includes(contentType);
|
2019-11-26 20:08:34 +01:00
|
|
|
const webStreamOnly = contentType === 'application/pdf' || mediaType === 'text';
|
|
|
|
const isReadyToPlay =
|
2019-11-28 17:27:30 +01:00
|
|
|
(IS_WEB && (isStreamable || webStreamOnly || forceVideo)) ||
|
|
|
|
((isStreamable || forceVideo) && streamingUrl) ||
|
|
|
|
(fileInfo && fileInfo.completed);
|
2019-08-02 08:28:14 +02:00
|
|
|
const loadingMessage =
|
|
|
|
!isStreamable && fileInfo && fileInfo.blobs_completed >= 1 && (!fileInfo.download_path || !fileInfo.written_bytes)
|
|
|
|
? __("It looks like you deleted or moved this file. We're rebuilding it now. It will only take a few seconds.")
|
|
|
|
: __('Loading');
|
|
|
|
|
2019-08-14 05:04:08 +02:00
|
|
|
const previousUri = usePrevious(uri);
|
|
|
|
const isNewView = uri && previousUri !== uri && isPlaying;
|
2019-09-17 21:20:55 +02:00
|
|
|
const [hasRecordedView, setHasRecordedView] = useState(false);
|
2019-08-14 05:04:08 +02:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (isNewView) {
|
|
|
|
setPlayTime(Date.now());
|
|
|
|
}
|
|
|
|
}, [isNewView, uri]);
|
|
|
|
|
|
|
|
useEffect(() => {
|
2019-09-17 21:20:55 +02:00
|
|
|
if (playTime && isReadyToPlay && !hasRecordedView) {
|
2019-08-14 05:04:08 +02:00
|
|
|
const timeToStart = Date.now() - playTime;
|
2019-09-18 20:41:20 +02:00
|
|
|
triggerAnalyticsView(uri, timeToStart).then(() => {
|
|
|
|
claimRewards();
|
2020-02-04 07:02:39 +01:00
|
|
|
setHasRecordedView(false); // This is a terrible variable name, rename this
|
2019-09-18 20:41:20 +02:00
|
|
|
setPlayTime(null);
|
|
|
|
});
|
2019-08-14 05:04:08 +02:00
|
|
|
}
|
2019-09-17 21:20:55 +02:00
|
|
|
}, [setPlayTime, triggerAnalyticsView, isReadyToPlay, hasRecordedView, playTime, uri, claimRewards]);
|
2019-08-02 08:28:14 +02:00
|
|
|
|
|
|
|
useEffect(() => {
|
2019-08-13 07:35:13 +02:00
|
|
|
function handleResize() {
|
|
|
|
const element = document.querySelector(`.${FILE_WRAPPER_CLASS}`);
|
|
|
|
if (!element) {
|
2019-08-17 16:32:24 +02:00
|
|
|
return;
|
2018-08-03 17:54:10 +02:00
|
|
|
}
|
2019-08-13 07:35:13 +02:00
|
|
|
|
|
|
|
const rect = element.getBoundingClientRect();
|
|
|
|
// @FlowFixMe
|
|
|
|
setFileViewerRect(rect);
|
|
|
|
}
|
|
|
|
|
2019-12-31 04:09:20 +01:00
|
|
|
handleResize();
|
|
|
|
window.addEventListener('resize', handleResize);
|
|
|
|
onFullscreenChange(window, 'add', handleResize);
|
2019-12-19 21:43:49 +01:00
|
|
|
return () => {
|
|
|
|
window.removeEventListener('resize', handleResize);
|
|
|
|
onFullscreenChange(window, 'remove', handleResize);
|
|
|
|
};
|
2019-08-13 07:35:13 +02:00
|
|
|
}, [setFileViewerRect, inline]);
|
2018-08-03 17:54:10 +02:00
|
|
|
|
2019-08-14 05:04:08 +02:00
|
|
|
function handleDrag(e, ui) {
|
|
|
|
const { x, y } = position;
|
|
|
|
const newX = x + ui.deltaX;
|
|
|
|
const newY = y + ui.deltaY;
|
|
|
|
setPosition({
|
|
|
|
x: newX,
|
|
|
|
y: newY,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-09-17 21:03:54 +02:00
|
|
|
const hidePlayer =
|
2020-01-06 21:57:49 +01:00
|
|
|
isText ||
|
2020-01-06 19:32:35 +01:00
|
|
|
!isPlaying ||
|
|
|
|
!uri ||
|
|
|
|
(!inline && (isMobile || !floatingPlayerEnabled || !['audio', 'video'].includes(mediaType)));
|
2019-09-17 21:03:54 +02:00
|
|
|
|
2019-08-13 07:35:13 +02:00
|
|
|
if (hidePlayer) {
|
|
|
|
return null;
|
|
|
|
}
|
2019-08-02 08:28:14 +02:00
|
|
|
|
|
|
|
return (
|
2019-08-13 07:35:13 +02:00
|
|
|
<Draggable
|
|
|
|
onDrag={handleDrag}
|
|
|
|
defaultPosition={position}
|
|
|
|
position={inline ? { x: 0, y: 0 } : position}
|
|
|
|
bounds="parent"
|
|
|
|
disabled={inline}
|
2020-01-08 21:00:50 +01:00
|
|
|
handle=".draggable"
|
2019-08-13 07:35:13 +02:00
|
|
|
cancel=".button"
|
2019-08-02 08:28:14 +02:00
|
|
|
>
|
2019-08-13 07:35:13 +02:00
|
|
|
<div
|
|
|
|
className={classnames('content__viewer', {
|
|
|
|
'content__viewer--floating': !inline,
|
2019-11-14 18:23:42 +01:00
|
|
|
'content__viewer--inline': inline,
|
2019-08-13 07:35:13 +02:00
|
|
|
})}
|
|
|
|
style={
|
|
|
|
inline && fileViewerRect
|
|
|
|
? { width: fileViewerRect.width, height: fileViewerRect.height, left: fileViewerRect.x }
|
|
|
|
: {}
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<div
|
|
|
|
className={classnames('content__wrapper', {
|
|
|
|
'content__wrapper--floating': !inline,
|
2019-08-02 08:28:14 +02:00
|
|
|
})}
|
2019-08-13 07:35:13 +02:00
|
|
|
>
|
|
|
|
{!inline && (
|
2020-01-08 21:00:50 +01:00
|
|
|
<div className="draggable content__floating-header">
|
|
|
|
<span className="media__uri--inline">{uri}</span>
|
|
|
|
<div className="content__actions">
|
|
|
|
<Tooltip label={__('View File')}>
|
|
|
|
<Button navigate={uri} icon={ICONS.VIEW} button="primary" />
|
|
|
|
</Tooltip>
|
|
|
|
<Tooltip label={__('Close')}>
|
|
|
|
<Button onClick={clearPlayingUri} icon={ICONS.REMOVE} button="primary" />
|
|
|
|
</Tooltip>
|
|
|
|
</div>
|
2019-08-13 07:35:13 +02:00
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
|
2020-01-27 20:32:20 +01:00
|
|
|
{isReadyToPlay ? (
|
|
|
|
<FileRender currentlyFloating={!inline} uri={uri} />
|
|
|
|
) : (
|
|
|
|
<LoadingScreen status={loadingMessage} />
|
|
|
|
)}
|
2019-08-13 07:35:13 +02:00
|
|
|
{!inline && (
|
2020-01-08 21:00:50 +01:00
|
|
|
<div className="draggable content__info">
|
2020-01-30 23:25:15 +01:00
|
|
|
<div className="claim-preview__title" title={title || uri}>
|
2019-08-13 07:35:13 +02:00
|
|
|
{title || uri}
|
|
|
|
</div>
|
|
|
|
<UriIndicator link addTooltip={false} uri={uri} />
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</Draggable>
|
2019-08-02 08:28:14 +02:00
|
|
|
);
|
2017-04-23 11:56:50 +02:00
|
|
|
}
|