DeleteCommentModal.tsx 2.8 KB

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