app.jsx 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. import React from 'react';
  2. import { DndProvider } from 'react-dnd';
  3. import { HTML5Backend } from 'react-dnd-html5-backend';
  4. import ReactDOM from 'react-dom';
  5. import { I18nextProvider } from 'react-i18next';
  6. import { SWRConfig } from 'swr';
  7. import { Provider } from 'unstated';
  8. import CommentContainer from '~/client/services/CommentContainer';
  9. import ContextExtractor from '~/client/services/ContextExtractor';
  10. import EditorContainer from '~/client/services/EditorContainer';
  11. import PageContainer from '~/client/services/PageContainer';
  12. import PageHistoryContainer from '~/client/services/PageHistoryContainer';
  13. import PersonalContainer from '~/client/services/PersonalContainer';
  14. import RevisionComparerContainer from '~/client/services/RevisionComparerContainer';
  15. import TagContainer from '~/client/services/TagContainer';
  16. import IdenticalPathPage from '~/components/IdenticalPathPage';
  17. import PrivateLegacyPages from '~/components/PrivateLegacyPages';
  18. import loggerFactory from '~/utils/logger';
  19. import { swrGlobalConfiguration } from '~/utils/swr-utils';
  20. import ErrorBoundary from '../components/ErrorBoudary';
  21. import Fab from '../components/Fab';
  22. import ForbiddenPage from '../components/ForbiddenPage';
  23. import RecentlyCreatedIcon from '../components/Icons/RecentlyCreatedIcon';
  24. import InAppNotificationPage from '../components/InAppNotification/InAppNotificationPage';
  25. import MaintenanceModeContent from '../components/MaintenanceModeContent';
  26. import PersonalSettings from '../components/Me/PersonalSettings';
  27. import MyDraftList from '../components/MyDraftList/MyDraftList';
  28. import GrowiContextualSubNavigation from '../components/Navbar/GrowiContextualSubNavigation';
  29. import GrowiSubNavigationSwitcher from '../components/Navbar/GrowiSubNavigationSwitcher';
  30. import NotFoundPage from '../components/NotFoundPage';
  31. import Page from '../components/Page';
  32. import DisplaySwitcher from '../components/Page/DisplaySwitcher';
  33. import FixPageGrantAlert from '../components/Page/FixPageGrantAlert';
  34. import RedirectedAlert from '../components/Page/RedirectedAlert';
  35. import ShareLinkAlert from '../components/Page/ShareLinkAlert';
  36. import TrashPageAlert from '../components/Page/TrashPageAlert';
  37. import PageComment from '../components/PageComment';
  38. import CommentEditorLazyRenderer from '../components/PageComment/CommentEditorLazyRenderer';
  39. import PageContentFooter from '../components/PageContentFooter';
  40. import BookmarkList from '../components/PageList/BookmarkList';
  41. import PageStatusAlert from '../components/PageStatusAlert';
  42. import PageTimeline from '../components/PageTimeline';
  43. import RecentCreated from '../components/RecentCreated/RecentCreated';
  44. import { SearchPage } from '../components/SearchPage';
  45. import Sidebar from '../components/Sidebar';
  46. import TagPage from '../components/TagPage';
  47. import TrashPageList from '../components/TrashPageList';
  48. import { appContainer, componentMappings } from './base';
  49. import { toastError } from './util/apiNotification';
  50. const logger = loggerFactory('growi:cli:app');
  51. appContainer.initContents();
  52. const { i18n } = appContainer;
  53. const socketIoContainer = appContainer.getContainer('SocketIoContainer');
  54. // create unstated container instance
  55. const pageContainer = new PageContainer(appContainer);
  56. const pageHistoryContainer = new PageHistoryContainer(appContainer, pageContainer);
  57. const revisionComparerContainer = new RevisionComparerContainer(appContainer, pageContainer);
  58. const commentContainer = new CommentContainer(appContainer);
  59. const editorContainer = new EditorContainer(appContainer);
  60. const tagContainer = new TagContainer(appContainer);
  61. const personalContainer = new PersonalContainer(appContainer);
  62. const injectableContainers = [
  63. appContainer, socketIoContainer, pageContainer, pageHistoryContainer, revisionComparerContainer,
  64. commentContainer, editorContainer, tagContainer, personalContainer,
  65. ];
  66. logger.info('unstated containers have been initialized');
  67. /**
  68. * define components
  69. * key: id of element
  70. * value: React Element
  71. */
  72. Object.assign(componentMappings, {
  73. 'grw-sidebar-wrapper': <Sidebar />,
  74. 'search-page': <SearchPage appContainer={appContainer} />,
  75. 'private-regacy-pages': <PrivateLegacyPages appContainer={appContainer} />,
  76. 'all-in-app-notifications': <InAppNotificationPage />,
  77. 'identical-path-page': <IdenticalPathPage />,
  78. // 'revision-history': <PageHistory pageId={pageId} />,
  79. 'tags-page': <TagPage />,
  80. 'grw-page-status-alert-container': <PageStatusAlert />,
  81. 'maintenance-mode-content': <MaintenanceModeContent />,
  82. 'trash-page-alert': <TrashPageAlert />,
  83. 'fix-page-grant-alert': <FixPageGrantAlert />,
  84. 'trash-page-list-container': <TrashPageList />,
  85. 'not-found-page': <NotFoundPage />,
  86. 'forbidden-page': <ForbiddenPage isLinkSharingDisabled={appContainer.config.disableLinkSharing} />,
  87. 'page-timeline': <PageTimeline />,
  88. 'personal-setting': <PersonalSettings crowi={personalContainer} />,
  89. 'my-drafts': <MyDraftList />,
  90. 'grw-fab-container': <Fab />,
  91. 'share-link-alert': <ShareLinkAlert />,
  92. 'redirected-alert': <RedirectedAlert />,
  93. });
  94. // additional definitions if data exists
  95. if (pageContainer.state.pageId != null) {
  96. Object.assign(componentMappings, {
  97. 'page-comments-list': <PageComment appContainer={appContainer} pageId={pageContainer.state.pageId} isReadOnly={false} titleAlign="left" />,
  98. 'page-comment-write': <CommentEditorLazyRenderer appContainer={appContainer} pageId={pageContainer.state.pageId} />,
  99. 'page-content-footer': <PageContentFooter
  100. createdAt={new Date(pageContainer.state.createdAt)}
  101. updatedAt={new Date(pageContainer.state.updatedAt)}
  102. creator={pageContainer.state.creator}
  103. revisionAuthor={pageContainer.state.revisionAuthor}
  104. />,
  105. 'recent-created-icon': <RecentlyCreatedIcon />,
  106. });
  107. // show the Page accessory modal when query of "compare" is requested
  108. if (revisionComparerContainer.getRevisionIDsToCompareAsParam().length > 0) {
  109. toastError('Sorry, opening PageAccessoriesModal is not implemented yet in v5.');
  110. // pageAccessoriesContainer.openPageAccessoriesModal('pageHistory');
  111. }
  112. }
  113. if (pageContainer.state.creator != null) {
  114. Object.assign(componentMappings, {
  115. 'user-created-list': <RecentCreated userId={pageContainer.state.creator._id} />,
  116. 'user-bookmark-list': <BookmarkList userId={pageContainer.state.creator._id} />,
  117. });
  118. }
  119. if (pageContainer.state.path != null) {
  120. Object.assign(componentMappings, {
  121. // eslint-disable-next-line quote-props
  122. 'page': <Page />,
  123. 'grw-subnav-container': <GrowiContextualSubNavigation isLinkSharingDisabled={appContainer.config.disableLinkSharing} />,
  124. 'grw-subnav-switcher-container': <GrowiSubNavigationSwitcher isLinkSharingDisabled={appContainer.config.disableLinkSharing} />,
  125. 'display-switcher': <DisplaySwitcher />,
  126. });
  127. }
  128. const renderMainComponents = () => {
  129. Object.keys(componentMappings).forEach((key) => {
  130. const elem = document.getElementById(key);
  131. if (elem) {
  132. ReactDOM.render(
  133. <I18nextProvider i18n={i18n}>
  134. <ErrorBoundary>
  135. <SWRConfig value={swrGlobalConfiguration}>
  136. <Provider inject={injectableContainers}>
  137. <DndProvider backend={HTML5Backend}>
  138. {componentMappings[key]}
  139. </DndProvider>
  140. </Provider>
  141. </SWRConfig>
  142. </ErrorBoundary>
  143. </I18nextProvider>,
  144. elem,
  145. );
  146. }
  147. });
  148. };
  149. // extract context before rendering main components
  150. const elem = document.getElementById('growi-context-extractor');
  151. if (elem != null) {
  152. ReactDOM.render(
  153. <SWRConfig value={swrGlobalConfiguration}>
  154. <ContextExtractor></ContextExtractor>
  155. </SWRConfig>,
  156. elem,
  157. renderMainComponents,
  158. );
  159. }
  160. else {
  161. renderMainComponents();
  162. }
  163. // initialize scrollpos-styler
  164. ScrollPosStyler.init();