RevisionComparer.tsx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import React, { useState } from 'react';
  2. import { IRevisionHasPageId, pagePathUtils } from '@growi/core';
  3. import { useTranslation } from 'next-i18next';
  4. import { CopyToClipboard } from 'react-copy-to-clipboard';
  5. import {
  6. Dropdown, DropdownToggle, DropdownMenu, DropdownItem,
  7. } from 'reactstrap';
  8. import { useCurrentPagePath } from '~/stores/context';
  9. import { RevisionDiff } from '../PageHistory/RevisionDiff';
  10. import styles from './RevisionComparer.module.scss';
  11. const { encodeSpaces } = pagePathUtils;
  12. const DropdownItemContents = ({ title, contents }) => (
  13. <>
  14. <div className="h6 mt-1 mb-2"><strong>{title}</strong></div>
  15. <div className="card well mb-1 p-2">{contents}</div>
  16. </>
  17. );
  18. type RevisionComparerProps = {
  19. sourceRevision: IRevisionHasPageId
  20. targetRevision: IRevisionHasPageId
  21. currentPageId?: string
  22. }
  23. export const RevisionComparer = (props: RevisionComparerProps): JSX.Element => {
  24. const { t } = useTranslation(['translation', 'commons']);
  25. const {
  26. sourceRevision, targetRevision, currentPageId,
  27. } = props;
  28. const { data: currentPagePath } = useCurrentPagePath();
  29. const [dropdownOpen, setDropdownOpen] = useState(false);
  30. const toggleDropdown = () => {
  31. setDropdownOpen(!dropdownOpen);
  32. };
  33. const generateURL = (pathName: string) => {
  34. const { origin } = window.location;
  35. const url = new URL(pathName, origin);
  36. if (sourceRevision != null && targetRevision != null) {
  37. const urlParams = `${sourceRevision._id}...${targetRevision._id}`;
  38. url.searchParams.set('compare', urlParams);
  39. }
  40. return encodeSpaces(decodeURI(url.href));
  41. };
  42. const isNodiff = (sourceRevision == null || targetRevision == null) ? true : sourceRevision._id === targetRevision._id;
  43. if (currentPageId == null || currentPagePath == null) {
  44. return <>{ t('not_found_page.page_not_exist')}</>;
  45. }
  46. return (
  47. <div className={`${styles['revision-compare']} revision-compare`}>
  48. <div className="d-flex">
  49. <h4 className="align-self-center">{ t('page_history.comparing_revisions') }</h4>
  50. <Dropdown
  51. className="grw-copy-dropdown align-self-center ml-auto"
  52. isOpen={dropdownOpen}
  53. toggle={() => toggleDropdown()}
  54. >
  55. <DropdownToggle
  56. caret
  57. className="d-block text-muted bg-transparent btn-copy border-0 py-0"
  58. >
  59. <i className="ti ti-clipboard"></i>
  60. </DropdownToggle>
  61. <DropdownMenu positionFixed right modifiers={{ preventOverflow: { boundariesElement: undefined } }}>
  62. {/* Page path URL */}
  63. <CopyToClipboard text={generateURL(currentPagePath)}>
  64. <DropdownItem className="px-3">
  65. <DropdownItemContents title={t('copy_to_clipboard.Page URL', { ns: 'commons' })} contents={generateURL(currentPagePath)} />
  66. </DropdownItem>
  67. </CopyToClipboard>
  68. {/* Permanent Link URL */}
  69. <CopyToClipboard text={generateURL(currentPageId)}>
  70. <DropdownItem className="px-3">
  71. <DropdownItemContents title={t('copy_to_clipboard.Permanent link', { ns: 'commons' })} contents={generateURL(currentPageId)} />
  72. </DropdownItem>
  73. </CopyToClipboard>
  74. <DropdownItem divider className="my-0"></DropdownItem>
  75. </DropdownMenu>
  76. </Dropdown>
  77. </div>
  78. <div className={`revision-compare-container ${isNodiff ? 'nodiff' : ''}`}>
  79. { isNodiff
  80. ? (
  81. <span className="h3 text-muted">{t('No diff')}</span>
  82. )
  83. : (
  84. <RevisionDiff
  85. revisionDiffOpened
  86. previousRevision={sourceRevision}
  87. currentRevision={targetRevision}
  88. />
  89. )
  90. }
  91. </div>
  92. </div>
  93. );
  94. };