2022-03-17 11:53:18 +01:00
|
|
|
// @flow
|
|
|
|
import { getThumbnailCdnUrl } from 'util/thumbnail';
|
|
|
|
import React from 'react';
|
|
|
|
// $FlowFixMe cannot resolve ...
|
|
|
|
import FileRenderPlaceholder from 'static/img/fileRenderPlaceholder.png';
|
|
|
|
|
2022-06-16 06:04:52 +02:00
|
|
|
export default function useGetPoster(claimThumbnail: ?string, containerRef: any) {
|
2022-03-17 11:53:18 +01:00
|
|
|
const [thumbnail, setThumbnail] = React.useState(FileRenderPlaceholder);
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
2022-06-14 15:43:41 +02:00
|
|
|
if (!claimThumbnail) {
|
|
|
|
return setThumbnail(FileRenderPlaceholder);
|
|
|
|
}
|
2022-03-17 11:53:18 +01:00
|
|
|
|
|
|
|
const timer = setTimeout(() => {
|
|
|
|
let newThumbnail = claimThumbnail;
|
|
|
|
|
2022-06-14 15:43:41 +02:00
|
|
|
// generate the thumbnail url served by the cdn
|
|
|
|
if (containerRef.current?.parentElement?.offsetWidth) {
|
2022-03-17 11:53:18 +01:00
|
|
|
const w = containerRef.current.parentElement.offsetWidth;
|
|
|
|
newThumbnail = getThumbnailCdnUrl({ thumbnail: newThumbnail, width: w, height: w });
|
|
|
|
}
|
|
|
|
|
2022-06-14 15:43:41 +02:00
|
|
|
// update new thumbnail in state
|
2022-03-17 11:53:18 +01:00
|
|
|
if (newThumbnail !== thumbnail) {
|
|
|
|
setThumbnail(newThumbnail);
|
|
|
|
}
|
|
|
|
}, 200);
|
|
|
|
|
|
|
|
return () => clearTimeout(timer);
|
|
|
|
}, [claimThumbnail, containerRef, thumbnail]);
|
|
|
|
|
|
|
|
return thumbnail;
|
|
|
|
}
|