Revision.tsx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. <div className="picture-container">
  31. { pic }
  32. </div>
  33. <div className="ml-3">
  34. <span className="text-muted small">
  35. <UserDate dateTime={revision.createdAt} /> {t('No diff')}
  36. </span>
  37. </div>
  38. </div>
  39. );
  40. };
  41. const renderFull = (revision: IRevisionHasId) => {
  42. const author = revision.author;
  43. const pic = (typeof author === 'object') ? <UserPicture user={author} size="lg" /> : <></>;
  44. return (
  45. <div className={`${styles['revision-history-main']} revision-history-main d-flex`}>
  46. <div className="picture-container">
  47. { pic }
  48. </div>
  49. <div className="ml-2">
  50. <div className="revision-history-author mb-1">
  51. <strong><Username user={author}></Username></strong>
  52. { isLatestRevision && <span className="badge badge-info ml-2">Latest</span> }
  53. </div>
  54. <div className="mb-1">
  55. <UserDate dateTime={revision.createdAt} />
  56. <br className="d-xl-none d-block" />
  57. <Link
  58. href={urljoin(returnPathForURL(currentPagePath, currentPageId), `?revisionId=${revision._id}`)}
  59. className="ml-xl-3"
  60. onClick={onClose}
  61. prefetch={false}
  62. >
  63. <i className="icon-login"></i> {t('Go to this version')}
  64. </Link>
  65. </div>
  66. </div>
  67. </div>
  68. );
  69. };
  70. if (!hasDiff) {
  71. return renderSimplifiedNodiff(revision);
  72. }
  73. return renderFull(revision);
  74. };