2
0

GrowiContextualSubNavigation.tsx 9.9 KB

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