DeleteCommentModal.tsx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import React from 'react';
  2. import { isPopulated } from '@growi/core';
  3. import { UserPicture } from '@growi/ui/dist/components';
  4. import { format } from 'date-fns';
  5. import {
  6. Button, Modal, ModalHeader, ModalBody, ModalFooter,
  7. } from 'reactstrap';
  8. import { ICommentHasId } from '../../interfaces/comment';
  9. import { Username } from '../User/Username';
  10. import styles from './DeleteCommentModal.module.scss';
  11. export type DeleteCommentModalProps = {
  12. isShown: boolean,
  13. comment: ICommentHasId | null,
  14. errorMessage: string,
  15. cancelToDelete: () => void,
  16. confirmToDelete: () => void,
  17. }
  18. export const DeleteCommentModal = (props: DeleteCommentModalProps): JSX.Element => {
  19. const {
  20. isShown, comment, errorMessage, cancelToDelete, confirmToDelete,
  21. } = props;
  22. const headerContent = () => {
  23. if (comment == null || isShown === false) {
  24. return <></>;
  25. }
  26. return (
  27. <span>
  28. <span className="material-symbols-outlined">delete_forever</span>
  29. Delete comment?
  30. </span>
  31. );
  32. };
  33. const bodyContent = () => {
  34. if (comment == null || isShown === false) {
  35. return <></>;
  36. }
  37. // the threshold for omitting body
  38. const OMIT_BODY_THRES = 400;
  39. const commentDate = format(new Date(comment.createdAt), 'yyyy/MM/dd HH:mm');
  40. const creator = isPopulated(comment.creator) ? comment.creator : undefined;
  41. let commentBody = comment.comment;
  42. if (commentBody.length > OMIT_BODY_THRES) { // omit
  43. commentBody = `${commentBody.substr(0, OMIT_BODY_THRES)}...`;
  44. }
  45. const commentBodyElement = <span style={{ whiteSpace: 'pre-wrap' }}>{commentBody}</span>;
  46. return (
  47. <>
  48. <UserPicture user={creator} size="xs" /> <strong><Username user={creator}></Username></strong> wrote on {commentDate}:
  49. <p className="card custom-card comment-body mt-2 p-2">{commentBodyElement}</p>
  50. </>
  51. );
  52. };
  53. const footerContent = () => {
  54. if (comment == null || isShown === false) {
  55. return <></>;
  56. }
  57. return (
  58. <>
  59. <span className="text-danger">{errorMessage}</span>&nbsp;
  60. <Button onClick={cancelToDelete}>Cancel</Button>
  61. <Button color="danger" onClick={confirmToDelete}>
  62. <span className="material-symbols-outlined">delete_forever</span>
  63. Delete
  64. </Button>
  65. </>
  66. );
  67. };
  68. return (
  69. <Modal isOpen={isShown} toggle={cancelToDelete} className={`${styles['page-comment-delete-modal']}`}>
  70. <ModalHeader tag="h4" toggle={cancelToDelete} className="bg-danger text-light">
  71. {headerContent()}
  72. </ModalHeader>
  73. <ModalBody>
  74. {bodyContent()}
  75. </ModalBody>
  76. <ModalFooter>
  77. {footerContent()}
  78. </ModalFooter>
  79. </Modal>
  80. );
  81. };