Revision.tsx 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import React from 'react';
  2. import { IRevisionHasId, pathUtils } from '@growi/core';
  3. import { UserPicture } from '@growi/ui';
  4. import { useTranslation } from 'next-i18next';
  5. import Link from 'next/link';
  6. import urljoin from 'url-join';
  7. import UserDate from '../User/UserDate';
  8. import { Username } from '../User/Username';
  9. import styles from './Revision.module.scss';
  10. type RevisionProps = {
  11. revision: IRevisionHasId,
  12. currentPageId: string,
  13. currentPagePath: string,
  14. isLatestRevision: boolean,
  15. hasDiff: boolean,
  16. onClose: () => void,
  17. }
  18. export const Revision = (props: RevisionProps): JSX.Element => {
  19. const { t } = useTranslation();
  20. const {
  21. revision, currentPageId, currentPagePath, isLatestRevision, hasDiff, onClose,
  22. } = props;
  23. const { returnPathForURL } = pathUtils;
  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 href={urljoin(returnPathForURL(currentPagePath, currentPageId), `?revisionId=${revision._id}`)} prefetch={false}>
  58. <a className="ml-xl-3" onClick={onClose}>
  59. <i className="icon-login"></i> {t('Go to this version')}
  60. </a>
  61. </Link>
  62. </div>
  63. </div>
  64. </div>
  65. );
  66. };
  67. if (!hasDiff) {
  68. return renderSimplifiedNodiff(revision);
  69. }
  70. return renderFull(revision);
  71. };