lbry-desktop/ui/effects/use-get-thumbnail.js

52 lines
1.4 KiB
JavaScript
Raw Permalink Normal View History

// @flow
import React from 'react';
import { generateStreamUrl } from 'util/web';
2020-01-13 20:12:27 +01:00
export default function useGetThumbnail(
uri: string,
claim: ?Claim,
streamingUrl: ?string,
getFile: string => void,
shouldHide: boolean
) {
2020-06-03 19:49:44 +02:00
let thumbnailToUse;
// $FlowFixMe
const isImage = claim && claim.value && claim.value.stream_type === 'image';
// $FlowFixMe
const isFree = claim && claim.value && (!claim.value.fee || Number(claim.value.fee.amount) <= 0);
2020-06-03 19:49:44 +02:00
const thumbnailInClaim = claim && claim.value && claim.value.thumbnail && claim.value.thumbnail.url;
// @if TARGET='web'
if (thumbnailInClaim) {
thumbnailToUse = thumbnailInClaim;
} else if (claim && isImage && isFree) {
2020-01-24 17:25:31 +01:00
thumbnailToUse = generateStreamUrl(claim.name, claim.claim_id);
}
// @endif
// @if TARGET='app'
2020-06-03 19:49:44 +02:00
thumbnailToUse = thumbnailInClaim;
//
// Temporarily disabled until we can call get with "save_blobs: off"
//
// React.useEffect(() => {
// if (hasClaim && isImage && isFree) {
// if (streamingUrl) {
// setThumbnail(streamingUrl);
// } else if (!shouldHide) {
// getFile(uri);
// }
// }
// }, [hasClaim, isFree, isImage, streamingUrl, uri, shouldHide]);
// @endif
2020-06-03 19:49:44 +02:00
const [thumbnail, setThumbnail] = React.useState(thumbnailToUse);
React.useEffect(() => {
setThumbnail(thumbnailToUse);
}, [thumbnailToUse]);
return thumbnail;
}