Attachment.tsx 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import type { JSX } from 'react';
  2. import type { IAttachmentHasId } from '@growi/core';
  3. import { format } from 'date-fns/format';
  4. import { UserPicture } from './UserPicture';
  5. type AttachmentProps = {
  6. attachment: IAttachmentHasId,
  7. inUse: boolean,
  8. onAttachmentDeleteClicked?: (attachment: IAttachmentHasId) => void,
  9. isUserLoggedIn?: boolean,
  10. };
  11. export const Attachment = (props: AttachmentProps): JSX.Element => {
  12. const {
  13. attachment, inUse, isUserLoggedIn, onAttachmentDeleteClicked,
  14. } = props;
  15. const _onAttachmentDeleteClicked = () => {
  16. if (onAttachmentDeleteClicked != null) {
  17. onAttachmentDeleteClicked(attachment);
  18. }
  19. };
  20. const formatIcon = (attachment.fileFormat.match(/image\/.+/i)) ? 'image' : 'description';
  21. const btnDownload = (isUserLoggedIn)
  22. ? (
  23. <a className="attachment-download" href={attachment.downloadPathProxied}>
  24. <span className="material-symbols-outlined">cloud_download</span>
  25. </a>
  26. )
  27. : '';
  28. const btnTrash = (isUserLoggedIn)
  29. ? (
  30. <a className="text-danger attachment-delete" onClick={_onAttachmentDeleteClicked}>
  31. <span className="material-symbols-outlined">delete</span>
  32. </a>
  33. )
  34. : '';
  35. const fileType = <span className="attachment-filetype badge bg-secondary rounded-pill">{attachment.fileFormat}</span>;
  36. const fileInUse = (inUse) ? <span className="attachment-in-use badge bg-info rounded-pill">In Use</span> : '';
  37. // Should UserDate be used like PageRevisionTable ?
  38. const formatType = 'yyyy/MM/dd HH:mm:ss';
  39. const createdAt = format(new Date(attachment.createdAt), formatType);
  40. return (
  41. <div className="attachment mb-2">
  42. <span className="me-1 attachment-userpicture">
  43. <UserPicture user={attachment.creator} size="sm"></UserPicture>
  44. </span>
  45. <a className="me-2" href={attachment.filePathProxied} target="_blank" rel="noopener noreferrer">
  46. <span className="material-symbols-outlined ms-1">{formatIcon}</span> {attachment.originalName}
  47. </a>
  48. <span className="me-2">{fileType}</span>
  49. <span className="me-2">{createdAt}</span>
  50. <span className="me-2">{fileInUse}</span>
  51. <span className="me-2">{btnDownload}</span>
  52. <span className="me-2">{btnTrash}</span>
  53. </div>
  54. );
  55. };