lbry-desktop/src/ui/component/youtubeTransferStatus/view.jsx

118 lines
4 KiB
React
Raw Normal View History

// @flow
import * as React from 'react';
import Button from 'component/button';
import ClaimPreview from 'component/claimPreview';
import Card from 'component/common/card';
type Props = {
youtubeChannels: Array<any>,
ytImportPending: boolean,
claimChannels: () => void,
updateUser: () => void,
checkYoutubeTransfer: () => void,
videosImported: ?Array<number>, // [currentAmountImported, totalAmountToImport]
};
2019-09-27 20:56:15 +02:00
const NOT_TRANSFERRED = 'not_transferred';
const PENDING_TRANSFER = 'pending_transfer';
const COMPLETED_TRANSFER = 'completed_transfer';
export default function YoutubeTransferStatus(props: Props) {
const { youtubeChannels, ytImportPending, claimChannels, videosImported, checkYoutubeTransfer, updateUser } = props;
const hasChannels = youtubeChannels && youtubeChannels.length;
2019-09-30 21:52:53 +02:00
const transferEnabled = youtubeChannels.some(status => status.transferable);
const hasPendingTransfers = youtubeChannels.some(status => status.transfer_state === PENDING_TRANSFER);
const hasCompleteTransfers = youtubeChannels.some(status => status.transfer_state === COMPLETED_TRANSFER);
console.log('?', hasChannels && (hasPendingTransfers || (!hasPendingTransfers && !hasCompleteTransfers)));
let total;
let complete;
2019-09-30 21:52:53 +02:00
if (hasPendingTransfers && videosImported) {
complete = videosImported[0];
total = videosImported[1];
}
function getMessage(channel) {
const { transferable, transfer_state: transferState, sync_status: syncStatus } = channel;
if (!transferable) {
switch (transferState) {
2019-09-27 20:56:15 +02:00
case NOT_TRANSFERRED:
return syncStatus[0].toUpperCase() + syncStatus.slice(1);
case PENDING_TRANSFER:
return __('Transfer in progress');
case COMPLETED_TRANSFER:
return __('Completed transfer');
}
} else {
return __('Ready to transfer');
}
}
React.useEffect(() => {
// If a channel is transferrable, theres nothing to check
2019-09-30 21:52:53 +02:00
if (hasPendingTransfers) {
checkYoutubeTransfer();
let interval = setInterval(() => {
checkYoutubeTransfer();
updateUser();
}, 60 * 1000);
return () => {
clearInterval(interval);
};
}
2019-09-30 21:52:53 +02:00
}, [hasPendingTransfers, checkYoutubeTransfer, updateUser]);
return (
hasChannels &&
2019-09-30 21:52:53 +02:00
(hasPendingTransfers || transferEnabled) && (
<div>
<Card
2019-09-27 20:56:15 +02:00
title={youtubeChannels.length > 1 ? __('Your YouTube Channels') : __('Your YouTube Channel')}
subtitle={
<span>
2019-09-30 21:52:53 +02:00
{hasPendingTransfers
2019-09-27 20:56:15 +02:00
? __('Your videos are currently being transferred. There is nothing else for you to do.')
: __('Your videos are ready to be transferred.')}
</span>
}
body={
<section>
2019-09-27 20:56:15 +02:00
{youtubeChannels.map((channel, index) => {
const { lbry_channel_name: channelName, channel_claim_id: claimId } = channel;
const url = `lbry://${channelName}#${claimId}`;
const transferState = getMessage(channel);
return (
2019-09-27 20:56:15 +02:00
<div key={url} className="card--inline">
<ClaimPreview uri={url} actions={<span className="help">{transferState}</span>} properties={''} />
</div>
);
})}
{videosImported && (
<div className="section help">
{complete} / {total} {__('videos transferred')}
</div>
)}
</section>
}
actions={
2019-09-30 21:52:53 +02:00
transferEnabled && (
<div className="card__actions">
2019-09-30 21:52:53 +02:00
<Button
button="primary"
disabled={ytImportPending}
onClick={claimChannels}
label={youtubeChannels.length > 1 ? __('Claim Channels') : __('Claim Channel')}
/>
<Button button="link" label={__('Learn more')} href="https://lbry.com/faq/youtube#transfer" />
</div>
)
}
/>
</div>
)
);
}