app.jsx 7.6 KB

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