Attachment.tsx 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import { IAttachmentHasId } from '@growi/core';
  2. import { UserPicture } from './User/UserPicture';
  3. type AttachmentProps = {
  4. attachment: IAttachmentHasId,
  5. inUse: boolean,
  6. onAttachmentDeleteClicked?: (attachment: IAttachmentHasId) => void,
  7. isUserLoggedIn?: boolean,
  8. };
  9. export const Attachment = (props: AttachmentProps): JSX.Element => {
  10. const {
  11. attachment, inUse, isUserLoggedIn, onAttachmentDeleteClicked,
  12. } = props;
  13. const _onAttachmentDeleteClicked = () => {
  14. if (onAttachmentDeleteClicked != null) {
  15. onAttachmentDeleteClicked(attachment);
  16. }
  17. };
  18. const formatIcon = (attachment.fileFormat.match(/image\/.+/i)) ? 'icon-picture' : 'icon-doc';
  19. const btnDownload = (isUserLoggedIn)
  20. ? (
  21. <a className="attachment-download" href={attachment.downloadPathProxied}>
  22. <i className="icon-cloud-download" />
  23. </a>
  24. )
  25. : '';
  26. const btnTrash = (isUserLoggedIn)
  27. ? (
  28. <a className="text-danger attachment-delete" onClick={_onAttachmentDeleteClicked}>
  29. <i className="icon-trash" />
  30. </a>
  31. )
  32. : '';
  33. const fileType = <span className="attachment-filetype badge badge-pill badge-secondary">{attachment.fileFormat}</span>;
  34. const fileInUse = (inUse) ? <span className="attachment-in-use badge badge-pill badge-info">In Use</span> : '';
  35. return (
  36. <div className="attachment mb-2">
  37. <span className="mr-1 attachment-userpicture">
  38. <UserPicture user={attachment.creator} size="sm"></UserPicture>
  39. </span>
  40. <a className="mr-2" href={attachment.filePathProxied} target="_blank" rel="noopener noreferrer">
  41. <i className={formatIcon}></i> {attachment.originalName}
  42. </a>
  43. <span className="mr-2">{fileType}</span>
  44. <span className="mr-2">{fileInUse}</span>
  45. <span className="mr-2">{btnDownload}</span>
  46. <span className="mr-2">{btnTrash}</span>
  47. </div>
  48. );
  49. };