PageStatusAlert.jsx 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import { parseISO, format } from 'date-fns';
  4. import { withTranslation } from 'react-i18next';
  5. import AppContainer from '~/client/services/AppContainer';
  6. import PageContainer from '~/client/services/PageContainer';
  7. import { withUnstatedContainers } from './UnstatedUtils';
  8. /**
  9. *
  10. * @author Yuki Takei <yuki@weseek.co.jp>
  11. *
  12. * @export
  13. * @class PageStatusAlert
  14. * @extends {React.Component}
  15. */
  16. class PageStatusAlert extends React.Component {
  17. constructor(props) {
  18. super(props);
  19. this.state = {
  20. };
  21. this.getContentsForSomeoneEditingAlert = this.getContentsForSomeoneEditingAlert.bind(this);
  22. this.getContentsForRevisionOutdated = this.getContentsForRevisionOutdated.bind(this);
  23. this.getContentsForDraftExistsAlert = this.getContentsForDraftExistsAlert.bind(this);
  24. this.getContentsForUpdatedAlert = this.getContentsForUpdatedAlert.bind(this);
  25. this.onClickResolveConflict = this.onClickResolveConflict.bind(this);
  26. }
  27. refreshPage() {
  28. window.location.reload();
  29. }
  30. onClickResolveConflict() {
  31. const { pageContainer, appContainer } = this.props;
  32. const pageEditor = appContainer.getComponentInstance('PageEditor');
  33. const markdownOnEdit = pageEditor.getMarkdown();
  34. pageContainer.setState({
  35. isConflictingOnSave: true,
  36. isConflictDiffModalOpen: true,
  37. revisionsOnConflict: {
  38. request: {
  39. revisionId: '',
  40. revisionBody: markdownOnEdit,
  41. createdAt: format(new Date(), 'yyyy/MM/dd HH:mm:ss'),
  42. userName: this.props.appContainer.currentUser.username,
  43. userImgPath: this.props.appContainer.currentUser.imageUrlCached,
  44. },
  45. origin: {
  46. revisionId: pageContainer.state.revisionId,
  47. revisionBody: pageContainer.state.markdown,
  48. createdAt: pageContainer.state.updatedAt.toString(),
  49. userName: pageContainer.state.creator.username,
  50. userImgPath: pageContainer.state.creator.imageUrlCached,
  51. },
  52. latest: {
  53. revisionId: pageContainer.state.remoteRevisionId,
  54. revisionBody: pageContainer.state.remoteRevisionBody,
  55. createdAt: format(new Date(pageContainer.state.remoteRevisionUpdateAt), 'yyyy/MM/dd HH:mm:ss'),
  56. userName: pageContainer.state.lastUpdateUsername.toString(),
  57. userImgPath: pageContainer.state.lastUpdateUserImagePath,
  58. },
  59. },
  60. });
  61. }
  62. getContentsForSomeoneEditingAlert() {
  63. const { t } = this.props;
  64. return [
  65. ['bg-success', 'd-hackmd-none'],
  66. <>
  67. <i className="icon-fw icon-people"></i>
  68. {t('hackmd.someone_editing')}
  69. </>,
  70. <a href="#hackmd" className="btn btn-outline-white">
  71. <i className="fa fa-fw fa-file-text-o mr-1"></i>
  72. Open HackMD Editor
  73. </a>,
  74. ];
  75. }
  76. getContentsForRevisionOutdated() {
  77. const { t, pageContainer, appContainer } = this.props;
  78. const pageEditor = appContainer.getComponentInstance('PageEditor');
  79. const isEditScreen = pageEditor != null;
  80. return [
  81. ['bg-warning', 'd-hackmd-none'],
  82. <>
  83. <i className="icon-fw icon-pencil"></i>
  84. {t('modal_resolve_conflict.file_conflicting_with_newer_remote')}
  85. </>,
  86. <>
  87. <button type="button" onClick={() => this.refreshPage()} className="btn btn-outline-white mr-4">
  88. <i className="icon-fw icon-reload mr-1"></i>
  89. {t('Load latest')}
  90. </button>
  91. {isEditScreen
  92. && (
  93. <button
  94. type="button"
  95. onClick={this.onClickResolveConflict}
  96. className="btn btn-outline-white"
  97. >
  98. <i className="fa fa-fw fa-file-text-o mr-1"></i>
  99. {t('modal_resolve_conflict.resolve_conflict')}
  100. </button>
  101. )
  102. }
  103. </>,
  104. ];
  105. }
  106. getContentsForDraftExistsAlert(isRealtime) {
  107. const { t } = this.props;
  108. return [
  109. ['bg-success', 'd-hackmd-none'],
  110. <>
  111. <i className="icon-fw icon-pencil"></i>
  112. {t('hackmd.this_page_has_draft')}
  113. </>,
  114. <a href="#hackmd" className="btn btn-outline-white">
  115. <i className="fa fa-fw fa-file-text-o mr-1"></i>
  116. Open HackMD Editor
  117. </a>,
  118. ];
  119. }
  120. getContentsForUpdatedAlert() {
  121. const { t } = this.props;
  122. const label1 = t('edited this page');
  123. const label2 = t('Load latest');
  124. return [
  125. ['bg-warning'],
  126. <>
  127. <i className="icon-fw icon-bulb"></i>
  128. {this.props.pageContainer.state.lastUpdateUsername} {label1}
  129. </>,
  130. <a href="#" className="btn btn-outline-white" onClick={this.refreshPage}>
  131. <i className="icon-fw icon-reload mr-1"></i>
  132. {label2}
  133. </a>,
  134. ];
  135. }
  136. render() {
  137. const {
  138. revisionId, revisionIdHackmdSynced, remoteRevisionId, hasDraftOnHackmd, isHackmdDraftUpdatingInRealtime, isConflictingOnSave,
  139. } = this.props.pageContainer.state;
  140. const pageEditor = this.props.appContainer.getComponentInstance('PageEditor');
  141. let markdownOnEdit = '';
  142. // when page mode is not view
  143. if (pageEditor != null) {
  144. markdownOnEdit = pageEditor.getMarkdown();
  145. }
  146. const isRevisionOutdated = revisionId !== remoteRevisionId;
  147. const isHackmdDocumentOutdated = revisionIdHackmdSynced !== remoteRevisionId;
  148. const isConflictOnEdit = isRevisionOutdated && (markdownOnEdit !== this.props.pageContainer.state.markdown);
  149. let getContentsFunc = null;
  150. // when conflicting on save
  151. if (isConflictingOnSave || isConflictOnEdit) {
  152. getContentsFunc = this.getContentsForRevisionOutdated;
  153. }
  154. // when remote revision is newer than both
  155. else if (isHackmdDocumentOutdated && isRevisionOutdated) {
  156. getContentsFunc = this.getContentsForUpdatedAlert;
  157. }
  158. // when someone editing with HackMD
  159. else if (isHackmdDraftUpdatingInRealtime) {
  160. getContentsFunc = this.getContentsForSomeoneEditingAlert;
  161. }
  162. // when the draft of HackMD is newest
  163. else if (hasDraftOnHackmd) {
  164. getContentsFunc = this.getContentsForDraftExistsAlert;
  165. }
  166. // do not render anything
  167. else {
  168. return null;
  169. }
  170. const [additionalClasses, label, btn] = getContentsFunc();
  171. return (
  172. <div className={`card grw-page-status-alert text-white fixed-bottom animated fadeInUp faster ${additionalClasses.join(' ')}`}>
  173. <div className="card-body">
  174. <p className="card-text grw-card-label-container">
  175. {label}
  176. </p>
  177. <p className="card-text grw-card-btn-container">
  178. {btn}
  179. </p>
  180. </div>
  181. </div>
  182. );
  183. }
  184. }
  185. /**
  186. * Wrapper component for using unstated
  187. */
  188. const PageStatusAlertWrapper = withUnstatedContainers(PageStatusAlert, [AppContainer, PageContainer]);
  189. PageStatusAlert.propTypes = {
  190. t: PropTypes.func.isRequired, // i18next
  191. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  192. pageContainer: PropTypes.instanceOf(PageContainer).isRequired,
  193. };
  194. export default withTranslation()(PageStatusAlertWrapper);