index.tsx 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import React, { useEffect, useState } from 'react';
  2. import { scrollIntoView } from '../../helpers/utility';
  3. import { Wrapper, PageNum, Img, Loading } from './styled';
  4. type Props = {
  5. pageNum: number;
  6. getPdfImage: (page: number) => Promise<string>;
  7. renderingState: string;
  8. };
  9. const index: React.FC<Props> = ({
  10. pageNum,
  11. getPdfImage,
  12. renderingState,
  13. }: Props) => {
  14. const [dataUrl, setDataUrl] = useState('');
  15. const getDataUrl = async (): Promise<any> => {
  16. const imageDataUrl = await getPdfImage(pageNum);
  17. setDataUrl(imageDataUrl);
  18. };
  19. const onClick = (): void => {
  20. const ele: HTMLElement | null = document.getElementById(`page_${pageNum}`);
  21. if (ele) {
  22. scrollIntoView(ele);
  23. }
  24. };
  25. useEffect(() => {
  26. if (renderingState === 'RENDERING') {
  27. getDataUrl();
  28. }
  29. }, [renderingState]);
  30. return (
  31. <Wrapper id={`thumbnail_${pageNum}`}>
  32. <PageNum>{pageNum}</PageNum>
  33. {renderingState === 'RENDERING' && dataUrl ? (
  34. <Img src={dataUrl} onClick={onClick} />
  35. ) : (
  36. <Loading />
  37. )}
  38. </Wrapper>
  39. );
  40. };
  41. export default index;