Attachment.tsx 2.2 KB

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