Ref.tsx 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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. isImmutable?: boolean,
  9. };
  10. const RefSubstance = React.memo(({
  11. fileNameOrId,
  12. pagePath,
  13. isImmutable,
  14. }: Props): JSX.Element => {
  15. const refsContext = useMemo(() => {
  16. return new RefsContext('ref', pagePath, { fileNameOrId });
  17. }, [fileNameOrId, pagePath]);
  18. const { data, error, isLoading } = useSWRxRef(pagePath, fileNameOrId, isImmutable);
  19. const attachments = data != null ? [data] : [];
  20. return <AttachmentList
  21. refsContext={refsContext}
  22. isLoading={isLoading}
  23. error={error}
  24. attachments={attachments}
  25. />;
  26. });
  27. export const Ref = React.memo((props: Props): JSX.Element => {
  28. return <RefSubstance {...props} />;
  29. });
  30. export const RefImmutable = React.memo((props: Omit<Props, 'isImmutable'>): JSX.Element => {
  31. return <RefSubstance {...props} isImmutable />;
  32. });
  33. Ref.displayName = 'Ref';