Revision.tsx 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import React from 'react';
  2. import type { IRevisionHasId } from '@growi/core';
  3. import { returnPathForURL } from '@growi/core/dist/utils/path-utils';
  4. import { UserPicture } from '@growi/ui/dist/components';
  5. import { useTranslation } from 'next-i18next';
  6. import Link from 'next/link';
  7. import urljoin from 'url-join';
  8. import UserDate from '../User/UserDate';
  9. import { Username } from '../User/Username';
  10. import styles from './Revision.module.scss';
  11. type RevisionProps = {
  12. revision: IRevisionHasId,
  13. isLatestRevision: boolean,
  14. hasDiff: boolean,
  15. currentPageId: string
  16. currentPagePath: string
  17. onClose: () => void,
  18. }
  19. export const Revision = (props: RevisionProps): JSX.Element => {
  20. const { t } = useTranslation();
  21. const {
  22. revision, isLatestRevision, hasDiff, onClose, currentPageId, currentPagePath,
  23. } = props;
  24. const renderSimplifiedNodiff = (revision: IRevisionHasId) => {
  25. const author = revision.author;
  26. const pic = (typeof author === 'object') ? <UserPicture user={author} size="sm" /> : <></>;
  27. return (
  28. <div className={`${styles['revision-history-main']} ${styles['revision-history-main-nodiff']}
  29. revision-history-main revision-history-main-nodiff my-1 d-flex align-items-center`}
  30. >
  31. <div className="picture-container">
  32. { pic }
  33. </div>
  34. <div className="ms-3">
  35. <span className="text-muted small">
  36. <UserDate dateTime={revision.createdAt} /> {t('No diff')}
  37. </span>
  38. </div>
  39. </div>
  40. );
  41. };
  42. const renderFull = (revision: IRevisionHasId) => {
  43. const author = revision.author;
  44. const pic = (typeof author === 'object') ? <UserPicture user={author} size="lg" /> : <></>;
  45. return (
  46. <div className={`${styles['revision-history-main']} revision-history-main d-flex`}>
  47. <div className="picture-container">
  48. { pic }
  49. </div>
  50. <div className="ms-2">
  51. <div className="revision-history-author mb-1">
  52. <strong><Username user={author}></Username></strong>
  53. { isLatestRevision && <span className="badge bg-info ms-2">Latest</span> }
  54. </div>
  55. <div className="mb-1">
  56. <UserDate dateTime={revision.createdAt} />
  57. <br className="d-xl-none d-block" />
  58. <Link
  59. href={urljoin(returnPathForURL(currentPagePath, currentPageId), `?revisionId=${revision._id}`)}
  60. className="ms-xl-3"
  61. onClick={onClose}
  62. prefetch={false}
  63. >
  64. <i className="icon-login"></i> {t('Go to this version')}
  65. </Link>
  66. </div>
  67. </div>
  68. </div>
  69. );
  70. };
  71. if (!hasDiff) {
  72. return renderSimplifiedNodiff(revision);
  73. }
  74. return renderFull(revision);
  75. };