GrowiContextualSubNavigation.tsx 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. import React, { useCallback } from 'react';
  2. import PropTypes from 'prop-types';
  3. import { useTranslation } from 'react-i18next';
  4. import { DropdownItem } from 'reactstrap';
  5. import { withUnstatedContainers } from '../UnstatedUtils';
  6. import EditorContainer from '~/client/services/EditorContainer';
  7. import {
  8. EditorMode, useDrawerMode, useEditorMode, useIsDeviceSmallerThanMd, useIsAbleToShowPageManagement, useIsAbleToShowTagLabel,
  9. useIsAbleToShowPageEditorModeManager, useIsAbleToShowPageAuthors, usePageAccessoriesModal, PageAccessoriesModalContents,
  10. } from '~/stores/ui';
  11. import {
  12. useCurrentCreatedAt, useCurrentUpdatedAt, useCurrentPageId, useRevisionId, useCurrentPagePath,
  13. useCreator, useRevisionAuthor, useIsGuestUser, useIsSharedUser, useShareLinkId,
  14. } from '~/stores/context';
  15. import { useSWRTagsInfo } from '~/stores/page';
  16. import { toastSuccess, toastError } from '~/client/util/apiNotification';
  17. import { apiPost } from '~/client/util/apiv1-client';
  18. import { IPageHasId } from '~/interfaces/page';
  19. import HistoryIcon from '../Icons/HistoryIcon';
  20. import AttachmentIcon from '../Icons/AttachmentIcon';
  21. import ShareLinkIcon from '../Icons/ShareLinkIcon';
  22. import { AdditionalMenuItemsRendererProps } from '../Common/Dropdown/PageItemControl';
  23. import { SubNavButtons } from './SubNavButtons';
  24. import PageEditorModeManager from './PageEditorModeManager';
  25. import { GrowiSubNavigation } from './GrowiSubNavigation';
  26. import PresentationIcon from '../Icons/PresentationIcon';
  27. import { exportAsMarkdown } from '~/client/services/page-operation';
  28. type AdditionalMenuItemsProps = AdditionalMenuItemsRendererProps & {
  29. pageId: string,
  30. revisionId: string,
  31. isLinkSharingDisabled?: boolean,
  32. }
  33. const AdditionalMenuItems = (props: AdditionalMenuItemsProps): JSX.Element => {
  34. const { t } = useTranslation();
  35. const { pageId, revisionId, isLinkSharingDisabled } = props;
  36. const { data: isGuestUser } = useIsGuestUser();
  37. const { data: isSharedUser } = useIsSharedUser();
  38. const { open } = usePageAccessoriesModal();
  39. return (
  40. <>
  41. {/* Presentation */}
  42. <DropdownItem onClick={() => { /* TODO: implement in https://redmine.weseek.co.jp/issues/87672 */ }}>
  43. <i className="icon-fw"><PresentationIcon /></i>
  44. { t('Presentation Mode') }
  45. </DropdownItem>
  46. {/* Export markdown */}
  47. <DropdownItem onClick={() => exportAsMarkdown(pageId, revisionId, 'md')}>
  48. <i className="icon-fw icon-cloud-download"></i>
  49. {t('export_bulk.export_page_markdown')}
  50. </DropdownItem>
  51. <DropdownItem divider />
  52. {/*
  53. TODO: show Tooltip when menu is disabled
  54. refs: PageAccessoriesModalControl
  55. */}
  56. <DropdownItem
  57. onClick={() => open(PageAccessoriesModalContents.PageHistory)}
  58. disabled={isGuestUser || isSharedUser}
  59. >
  60. <span className="mr-1"><HistoryIcon /></span>
  61. {t('History')}
  62. </DropdownItem>
  63. <DropdownItem
  64. onClick={() => open(PageAccessoriesModalContents.Attachment)}
  65. >
  66. <span className="mr-1"><AttachmentIcon /></span>
  67. {t('attachment_data')}
  68. </DropdownItem>
  69. <DropdownItem
  70. onClick={() => open(PageAccessoriesModalContents.ShareLink)}
  71. disabled={isGuestUser || isSharedUser || isLinkSharingDisabled}
  72. >
  73. <span className="mr-1"><ShareLinkIcon /></span>
  74. {t('share_links.share_link_management')}
  75. </DropdownItem>
  76. <DropdownItem divider />
  77. {/* Create template */}
  78. <DropdownItem onClick={() => { /* TODO: implement in https://redmine.weseek.co.jp/issues/87673 */ }}>
  79. <i className="icon-fw icon-magic-wand"></i> { t('template.option_label.create/edit') }
  80. </DropdownItem>
  81. </>
  82. );
  83. };
  84. const GrowiContextualSubNavigation = (props) => {
  85. const { data: isDeviceSmallerThanMd } = useIsDeviceSmallerThanMd();
  86. const { data: isDrawerMode } = useDrawerMode();
  87. const { data: editorMode, mutate: mutateEditorMode } = useEditorMode();
  88. const { data: createdAt } = useCurrentCreatedAt();
  89. const { data: updatedAt } = useCurrentUpdatedAt();
  90. const { data: pageId } = useCurrentPageId();
  91. const { data: revisionId } = useRevisionId();
  92. const { data: path } = useCurrentPagePath();
  93. const { data: creator } = useCreator();
  94. const { data: revisionAuthor } = useRevisionAuthor();
  95. const { data: isGuestUser } = useIsGuestUser();
  96. const { data: isSharedUser } = useIsSharedUser();
  97. const { data: shareLinkId } = useShareLinkId();
  98. const { data: isAbleToShowPageManagement } = useIsAbleToShowPageManagement();
  99. const { data: isAbleToShowTagLabel } = useIsAbleToShowTagLabel();
  100. const { data: isAbleToShowPageEditorModeManager } = useIsAbleToShowPageEditorModeManager();
  101. const { data: isAbleToShowPageAuthors } = useIsAbleToShowPageAuthors();
  102. const { mutate: mutateSWRTagsInfo, data: tagsInfoData } = useSWRTagsInfo(pageId);
  103. const {
  104. editorContainer, isCompactMode, isLinkSharingDisabled,
  105. } = props;
  106. const isViewMode = editorMode === EditorMode.View;
  107. const tagsUpdatedHandler = useCallback(async(newTags: string[]) => {
  108. // It will not be reflected in the DB until the page is refreshed
  109. if (editorMode === EditorMode.Editor) {
  110. return editorContainer.setState({ tags: newTags });
  111. }
  112. try {
  113. const { tags } = await apiPost('/tags.update', { pageId, revisionId, tags: newTags }) as { tags };
  114. // revalidate SWRTagsInfo
  115. mutateSWRTagsInfo();
  116. // update editorContainer.state
  117. editorContainer.setState({ tags });
  118. toastSuccess('updated tags successfully');
  119. }
  120. catch (err) {
  121. toastError(err, 'fail to update tags');
  122. }
  123. // eslint-disable-next-line react-hooks/exhaustive-deps
  124. }, [pageId]);
  125. const ControlComponents = useCallback(() => {
  126. function onPageEditorModeButtonClicked(viewType) {
  127. mutateEditorMode(viewType);
  128. }
  129. return (
  130. <>
  131. <div className="h-50 d-flex flex-column align-items-end justify-content-center">
  132. { pageId != null && isViewMode && (
  133. <SubNavButtons
  134. isCompactMode={isCompactMode}
  135. pageId={pageId}
  136. shareLinkId={shareLinkId}
  137. revisionId={revisionId}
  138. disableSeenUserInfoPopover={isSharedUser}
  139. showPageControlDropdown={isAbleToShowPageManagement}
  140. additionalMenuItemRenderer={props => (
  141. <AdditionalMenuItems {...props} pageId={pageId} revisionId={revisionId} isLinkSharingDisabled={isLinkSharingDisabled} />
  142. )}
  143. />
  144. ) }
  145. </div>
  146. <div className="h-50 d-flex flex-column align-items-end justify-content-center">
  147. {isAbleToShowPageEditorModeManager && (
  148. <PageEditorModeManager
  149. onPageEditorModeButtonClicked={onPageEditorModeButtonClicked}
  150. isBtnDisabled={isGuestUser}
  151. editorMode={editorMode}
  152. isDeviceSmallerThanMd={isDeviceSmallerThanMd}
  153. />
  154. )}
  155. </div>
  156. </>
  157. );
  158. }, [
  159. pageId, revisionId,
  160. editorMode, mutateEditorMode,
  161. isCompactMode, isLinkSharingDisabled,
  162. isDeviceSmallerThanMd, isGuestUser, isSharedUser,
  163. isViewMode, isAbleToShowPageEditorModeManager, isAbleToShowPageManagement,
  164. ]);
  165. if (path == null) {
  166. return <></>;
  167. }
  168. const currentPage: Partial<IPageHasId> = {
  169. _id: pageId ?? undefined,
  170. path,
  171. revision: revisionId ?? undefined,
  172. creator: creator ?? undefined,
  173. lastUpdateUser: revisionAuthor,
  174. createdAt: createdAt ?? undefined,
  175. updatedAt: updatedAt ?? undefined,
  176. };
  177. return (
  178. <GrowiSubNavigation
  179. page={currentPage}
  180. showDrawerToggler={isDrawerMode}
  181. showTagLabel={isAbleToShowTagLabel}
  182. showPageAuthors={isAbleToShowPageAuthors}
  183. isGuestUser={isGuestUser}
  184. isDrawerMode={isDrawerMode}
  185. isCompactMode={isCompactMode}
  186. tags={tagsInfoData?.tags || []}
  187. tagsUpdatedHandler={tagsUpdatedHandler}
  188. controls={ControlComponents}
  189. />
  190. );
  191. };
  192. /**
  193. * Wrapper component for using unstated
  194. */
  195. const GrowiContextualSubNavigationWrapper = withUnstatedContainers(GrowiContextualSubNavigation, [EditorContainer]);
  196. GrowiContextualSubNavigation.propTypes = {
  197. editorContainer: PropTypes.instanceOf(EditorContainer).isRequired,
  198. isCompactMode: PropTypes.bool,
  199. isLinkSharingDisabled: PropTypes.bool,
  200. };
  201. export default GrowiContextualSubNavigationWrapper;