2020-04-29 22:50:06 +02:00
|
|
|
// @flow
|
2020-10-20 19:10:02 +02:00
|
|
|
import * as RENDER_MODES from 'constants/file_render_modes';
|
2020-04-29 22:50:06 +02:00
|
|
|
import React, { useEffect } from 'react';
|
2020-10-20 19:10:02 +02:00
|
|
|
import classnames from 'classnames';
|
2020-04-29 22:50:06 +02:00
|
|
|
import Button from 'component/button';
|
|
|
|
import FileViewerEmbeddedTitle from 'component/fileViewerEmbeddedTitle';
|
|
|
|
import { useHistory } from 'react-router-dom';
|
2020-08-10 22:47:39 +02:00
|
|
|
import { useIsMobile } from 'effects/use-screensize';
|
2020-04-29 22:50:06 +02:00
|
|
|
import { formatLbryUrlForWeb } from 'util/url';
|
|
|
|
|
|
|
|
type Props = {
|
|
|
|
uri: string,
|
|
|
|
thumbnail: string,
|
|
|
|
claim: ?Claim,
|
|
|
|
doResolveUri: string => void,
|
|
|
|
doFetchCostInfoForUri: string => void,
|
2020-05-25 19:20:45 +02:00
|
|
|
costInfo: ?{ cost: number },
|
2020-04-29 22:50:06 +02:00
|
|
|
floatingPlayerEnabled: boolean,
|
2020-05-21 18:53:21 +02:00
|
|
|
doPlayUri: (string, ?boolean, ?boolean, (GetResponse) => void) => void,
|
|
|
|
doAnaltyicsPurchaseEvent: GetResponse => void,
|
2020-10-20 19:10:02 +02:00
|
|
|
parentCommentId?: string,
|
|
|
|
isMarkdownPost: boolean,
|
|
|
|
doSetPlayingUri: ({}) => void,
|
|
|
|
renderMode: string,
|
2020-04-29 22:50:06 +02:00
|
|
|
};
|
|
|
|
|
2020-05-25 19:20:45 +02:00
|
|
|
export default function EmbedPlayButton(props: Props) {
|
2020-04-29 22:50:06 +02:00
|
|
|
const {
|
|
|
|
uri,
|
|
|
|
thumbnail = '',
|
|
|
|
claim,
|
|
|
|
doResolveUri,
|
|
|
|
doFetchCostInfoForUri,
|
|
|
|
floatingPlayerEnabled,
|
2020-04-30 20:25:51 +02:00
|
|
|
doPlayUri,
|
2020-10-20 19:10:02 +02:00
|
|
|
doSetPlayingUri,
|
2020-05-21 18:53:21 +02:00
|
|
|
doAnaltyicsPurchaseEvent,
|
2020-05-25 19:20:45 +02:00
|
|
|
costInfo,
|
2020-10-20 19:10:02 +02:00
|
|
|
parentCommentId,
|
|
|
|
isMarkdownPost,
|
|
|
|
renderMode,
|
2020-04-29 22:50:06 +02:00
|
|
|
} = props;
|
2020-10-20 19:10:02 +02:00
|
|
|
const {
|
|
|
|
push,
|
|
|
|
location: { pathname },
|
|
|
|
} = useHistory();
|
2020-04-29 22:50:06 +02:00
|
|
|
const isMobile = useIsMobile();
|
|
|
|
const hasResolvedUri = claim !== undefined;
|
2020-10-20 19:10:02 +02:00
|
|
|
const hasCostInfo = costInfo !== undefined;
|
2020-05-25 19:20:45 +02:00
|
|
|
const disabled = !hasResolvedUri || !costInfo;
|
2020-10-20 19:10:02 +02:00
|
|
|
const canPlayInline = [RENDER_MODES.AUDIO, RENDER_MODES.VIDEO].includes(renderMode);
|
2020-04-29 22:50:06 +02:00
|
|
|
|
|
|
|
useEffect(() => {
|
2020-10-20 19:10:02 +02:00
|
|
|
if (!hasResolvedUri) {
|
|
|
|
doResolveUri(uri);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!hasCostInfo) {
|
|
|
|
doFetchCostInfoForUri(uri);
|
|
|
|
}
|
|
|
|
}, [uri, doResolveUri, doFetchCostInfoForUri, hasCostInfo, hasResolvedUri]);
|
2020-05-21 18:53:21 +02:00
|
|
|
|
|
|
|
function handleClick() {
|
2020-05-25 19:20:45 +02:00
|
|
|
if (disabled) {
|
2020-05-21 18:53:21 +02:00
|
|
|
return;
|
2020-04-29 22:50:06 +02:00
|
|
|
}
|
|
|
|
|
2020-10-20 19:10:02 +02:00
|
|
|
if (isMobile || !floatingPlayerEnabled || !canPlayInline) {
|
2020-04-29 22:50:06 +02:00
|
|
|
const formattedUrl = formatLbryUrlForWeb(uri);
|
|
|
|
push(formattedUrl);
|
|
|
|
} else {
|
2020-05-21 18:53:21 +02:00
|
|
|
doPlayUri(uri, undefined, undefined, fileInfo => {
|
2020-10-20 19:10:02 +02:00
|
|
|
let playingOptions: PlayingUri = { uri, pathname };
|
|
|
|
if (parentCommentId) {
|
|
|
|
playingOptions.source = 'comment';
|
|
|
|
playingOptions.commentId = parentCommentId;
|
|
|
|
} else if (isMarkdownPost) {
|
|
|
|
playingOptions.source = 'markdown';
|
|
|
|
}
|
|
|
|
|
|
|
|
doSetPlayingUri(playingOptions);
|
2020-05-21 18:53:21 +02:00
|
|
|
doAnaltyicsPurchaseEvent(fileInfo);
|
|
|
|
});
|
2020-04-29 22:50:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div
|
2020-05-25 19:20:45 +02:00
|
|
|
disabled={disabled}
|
2020-04-29 22:50:06 +02:00
|
|
|
role="button"
|
|
|
|
className="embed__inline-button"
|
|
|
|
onClick={handleClick}
|
|
|
|
style={{ backgroundImage: `url('${thumbnail.replace(/'/g, "\\'")}')` }}
|
|
|
|
>
|
|
|
|
<FileViewerEmbeddedTitle uri={uri} isInApp />
|
2020-10-20 19:10:02 +02:00
|
|
|
<Button
|
|
|
|
onClick={handleClick}
|
|
|
|
iconSize={30}
|
|
|
|
title={__('Play')}
|
|
|
|
className={classnames('button--icon', {
|
|
|
|
'button--play': canPlayInline,
|
|
|
|
'button--view': !canPlayInline,
|
|
|
|
})}
|
|
|
|
disabled={disabled}
|
|
|
|
/>
|
2020-04-29 22:50:06 +02:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|