AttachmentList.tsx 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import { useCallback } from 'react';
  2. import type { IAttachmentHasId } from '@growi/core';
  3. import { Attachment, LoadingSpinner } from '@growi/ui/dist/components';
  4. import { ExtractedAttachments } from './ExtractedAttachments';
  5. import { RefsContext } from './util/refs-context';
  6. import styles from './AttachmentList.module.scss';
  7. const AttachmentLink = Attachment;
  8. type Props = {
  9. refsContext: RefsContext
  10. isLoading: boolean
  11. error?: Error
  12. attachments: IAttachmentHasId[]
  13. };
  14. export const AttachmentList = ({
  15. refsContext,
  16. isLoading,
  17. error,
  18. attachments,
  19. }: Props): JSX.Element => {
  20. const renderNoAttachmentsMessage = useCallback(() => {
  21. return (
  22. <div className="text-muted">
  23. <small>
  24. <span className="material-symbols-outlined fs-5 me-1" aria-hidden="true">info</span>
  25. {
  26. refsContext.options?.prefix != null
  27. ? `${refsContext.options.prefix} and descendant pages have no attachments`
  28. : `${refsContext.pagePath} has no attachments`
  29. }
  30. </small>
  31. </div>
  32. );
  33. }, [refsContext]);
  34. const renderContents = useCallback(() => {
  35. if (isLoading) {
  36. return (
  37. <div className="text-muted">
  38. <LoadingSpinner className="me-1" />
  39. <span className="attachment-refs-blink">{refsContext.toString()}</span>
  40. </div>
  41. );
  42. }
  43. if (error != null) {
  44. return (
  45. <div className="text-warning">
  46. <span className="material-symbols-outlined me-1">warning</span>
  47. {refsContext.toString()} (-&gt; <small>{error.message}</small>)
  48. </div>
  49. );
  50. }
  51. // no attachments
  52. if (attachments.length === 0) {
  53. return renderNoAttachmentsMessage();
  54. }
  55. return (refsContext.isExtractImage)
  56. ? <ExtractedAttachments attachments={attachments} refsContext={refsContext} />
  57. : attachments.map((attachment) => {
  58. return <AttachmentLink key={attachment._id} attachment={attachment} inUse={false} />;
  59. });
  60. }, [isLoading, error, attachments, refsContext, renderNoAttachmentsMessage]);
  61. return <div className={styles['attachment-refs']}>{renderContents()}</div>;
  62. };