RefImg.tsx 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import React, { useMemo } from 'react';
  2. import { useSWRxRef } from '../stores/refs';
  3. import { AttachmentList } from './AttachmentList';
  4. import { RefsContext } from './util/refs-context';
  5. type Props = {
  6. fileNameOrId: string
  7. pagePath: string
  8. width?: string
  9. height?: string
  10. maxWidth?: string
  11. maxHeight?: string
  12. alt?: string
  13. isImmutable?: boolean
  14. };
  15. const RefImgSubstance = React.memo(({
  16. fileNameOrId,
  17. pagePath,
  18. width,
  19. height,
  20. maxWidth,
  21. maxHeight,
  22. alt,
  23. isImmutable,
  24. }: Props): JSX.Element => {
  25. const refsContext = useMemo(() => {
  26. const options = {
  27. fileNameOrId, width, height, maxWidth, maxHeight, alt,
  28. };
  29. return new RefsContext('refimg', pagePath, options);
  30. }, [fileNameOrId, pagePath, width, height, maxWidth, maxHeight, alt]);
  31. const { data, error, isLoading } = useSWRxRef(pagePath, fileNameOrId, isImmutable);
  32. const attachments = data != null ? [data] : [];
  33. return <AttachmentList
  34. refsContext={refsContext}
  35. isLoading={isLoading}
  36. error={error}
  37. attachments={attachments}
  38. />;
  39. });
  40. export const RefImg = React.memo((props: Props): JSX.Element => {
  41. return <RefImgSubstance {...props} />;
  42. });
  43. export const RefImgImmutable = React.memo((props: Omit<Props, 'isImmutable'>): JSX.Element => {
  44. return <RefImgSubstance {...props} isImmutable />;
  45. });
  46. RefImg.displayName = 'RefImg';