GrowiContextualSubNavigation.tsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. import React, { useState, useEffect, useCallback } from 'react';
  2. import { isPopulated } from '@growi/core';
  3. import { useTranslation } from 'next-i18next';
  4. import dynamic from 'next/dynamic';
  5. import { DropdownItem } from 'reactstrap';
  6. import { exportAsMarkdown } from '~/client/services/page-operation';
  7. import { toastSuccess, toastError } from '~/client/util/apiNotification';
  8. import { apiPost } from '~/client/util/apiv1-client';
  9. import {
  10. IPageToRenameWithMeta, IPageWithMeta, IPageInfoForEntity, IPageHasId,
  11. } from '~/interfaces/page';
  12. import { OnDuplicatedFunction, OnRenamedFunction, OnDeletedFunction } from '~/interfaces/ui';
  13. import {
  14. useCurrentPageId,
  15. useCurrentPathname,
  16. useCurrentUser, useIsGuestUser, useIsSharedUser, useShareLinkId, useTemplateTagData,
  17. } from '~/stores/context';
  18. import { usePageTagsForEditors } from '~/stores/editor';
  19. import {
  20. usePageAccessoriesModal, PageAccessoriesModalContents, IPageForPageDuplicateModal,
  21. usePageDuplicateModal, usePageRenameModal, usePageDeleteModal, usePagePresentationModal,
  22. } from '~/stores/modal';
  23. import { useSWRxCurrentPage, useSWRxTagsInfo } from '~/stores/page';
  24. import {
  25. EditorMode, useDrawerMode, useEditorMode, useIsAbleToShowPageManagement, useIsAbleToShowTagLabel,
  26. useIsAbleToShowPageEditorModeManager, useIsAbleToShowPageAuthors,
  27. } from '~/stores/ui';
  28. import CreateTemplateModal from '../CreateTemplateModal';
  29. import AttachmentIcon from '../Icons/AttachmentIcon';
  30. import HistoryIcon from '../Icons/HistoryIcon';
  31. import PresentationIcon from '../Icons/PresentationIcon';
  32. import ShareLinkIcon from '../Icons/ShareLinkIcon';
  33. import { Skelton } from '../Skelton';
  34. import { GrowiSubNavigation } from './GrowiSubNavigation';
  35. import { SubNavButtonsProps } from './SubNavButtons';
  36. import PageEditorModeManagerStyles from './PageEditorModeManager.module.scss';
  37. type AdditionalMenuItemsProps = {
  38. pageId: string,
  39. revisionId: string,
  40. isLinkSharingDisabled?: boolean,
  41. onClickTemplateMenuItem: (isPageTemplateModalShown: boolean) => void,
  42. }
  43. const AdditionalMenuItems = (props: AdditionalMenuItemsProps): JSX.Element => {
  44. const { t } = useTranslation();
  45. const {
  46. pageId, revisionId, isLinkSharingDisabled, onClickTemplateMenuItem,
  47. } = props;
  48. const openPageTemplateModalHandler = () => {
  49. onClickTemplateMenuItem(true);
  50. };
  51. const { data: isGuestUser } = useIsGuestUser();
  52. const { data: isSharedUser } = useIsSharedUser();
  53. const { open: openPresentationModal } = usePagePresentationModal();
  54. const { open: openAccessoriesModal } = usePageAccessoriesModal();
  55. const hrefForPresentationModal = `${pageId}/?presentation=1`;
  56. return (
  57. <>
  58. {/* Presentation */}
  59. <DropdownItem
  60. onClick={() => openPresentationModal(hrefForPresentationModal)}
  61. data-testid="open-presentation-modal-btn"
  62. className="grw-page-control-dropdown-item"
  63. >
  64. <i className="icon-fw grw-page-control-dropdown-icon">
  65. <PresentationIcon />
  66. </i>
  67. { t('Presentation Mode') }
  68. </DropdownItem>
  69. {/* Export markdown */}
  70. <DropdownItem
  71. onClick={() => exportAsMarkdown(pageId, revisionId, 'md')}
  72. className="grw-page-control-dropdown-item"
  73. >
  74. <i className="icon-fw icon-cloud-download grw-page-control-dropdown-icon"></i>
  75. {t('export_bulk.export_page_markdown')}
  76. </DropdownItem>
  77. <DropdownItem divider />
  78. {/*
  79. TODO: show Tooltip when menu is disabled
  80. refs: PageAccessoriesModalControl
  81. */}
  82. <DropdownItem
  83. onClick={() => openAccessoriesModal(PageAccessoriesModalContents.PageHistory)}
  84. disabled={isGuestUser || isSharedUser}
  85. data-testid="open-page-accessories-modal-btn-with-history-tab"
  86. className="grw-page-control-dropdown-item"
  87. >
  88. <span className="grw-page-control-dropdown-icon">
  89. <HistoryIcon />
  90. </span>
  91. {t('History')}
  92. </DropdownItem>
  93. <DropdownItem
  94. onClick={() => openAccessoriesModal(PageAccessoriesModalContents.Attachment)}
  95. data-testid="open-page-accessories-modal-btn-with-attachment-data-tab"
  96. className="grw-page-control-dropdown-item"
  97. >
  98. <span className="grw-page-control-dropdown-icon">
  99. <AttachmentIcon />
  100. </span>
  101. {t('attachment_data')}
  102. </DropdownItem>
  103. <DropdownItem
  104. onClick={() => openAccessoriesModal(PageAccessoriesModalContents.ShareLink)}
  105. disabled={isGuestUser || isSharedUser || isLinkSharingDisabled}
  106. data-testid="open-page-accessories-modal-btn-with-share-link-management-data-tab"
  107. className="grw-page-control-dropdown-item"
  108. >
  109. <span className="grw-page-control-dropdown-icon">
  110. <ShareLinkIcon />
  111. </span>
  112. {t('share_links.share_link_management')}
  113. </DropdownItem>
  114. <DropdownItem divider />
  115. {/* Create template */}
  116. <DropdownItem
  117. onClick={openPageTemplateModalHandler}
  118. className="grw-page-control-dropdown-item"
  119. data-testid="open-page-template-modal-btn"
  120. >
  121. <i className="icon-fw icon-magic-wand grw-page-control-dropdown-icon"></i>
  122. { t('template.option_label.create/edit') }
  123. </DropdownItem>
  124. </>
  125. );
  126. };
  127. type GrowiContextualSubNavigationProps = {
  128. isCompactMode?: boolean,
  129. isLinkSharingDisabled: boolean,
  130. };
  131. const GrowiContextualSubNavigation = (props: GrowiContextualSubNavigationProps): JSX.Element => {
  132. const PageEditorModeManager = dynamic(
  133. () => import('./PageEditorModeManager'),
  134. { ssr: false, loading: () => <Skelton additionalClass={`${PageEditorModeManagerStyles['grw-page-editor-mode-manager-skelton']}`} /> },
  135. );
  136. const SubNavButtons = dynamic<SubNavButtonsProps>(
  137. () => import('./SubNavButtons').then(mod => mod.SubNavButtons),
  138. { ssr: false, loading: () => <Skelton additionalClass='btn-skelton py-2' /> },
  139. );
  140. const { data: currentPage, mutate: mutateCurrentPage } = useSWRxCurrentPage();
  141. const path = currentPage?.path;
  142. const revision = currentPage?.revision;
  143. const revisionId = (revision != null && isPopulated(revision)) ? revision._id : undefined;
  144. const { data: isDrawerMode } = useDrawerMode();
  145. const { data: editorMode, mutate: mutateEditorMode } = useEditorMode();
  146. const { data: pageId } = useCurrentPageId();
  147. const { data: currentPathname } = useCurrentPathname();
  148. const { data: currentUser } = useCurrentUser();
  149. const { data: isGuestUser } = useIsGuestUser();
  150. const { data: isSharedUser } = useIsSharedUser();
  151. const { data: shareLinkId } = useShareLinkId();
  152. const { data: isAbleToShowPageManagement } = useIsAbleToShowPageManagement();
  153. const { data: isAbleToShowTagLabel } = useIsAbleToShowTagLabel();
  154. const { data: isAbleToShowPageEditorModeManager } = useIsAbleToShowPageEditorModeManager();
  155. const { data: isAbleToShowPageAuthors } = useIsAbleToShowPageAuthors();
  156. const { mutate: mutateSWRTagsInfo, data: tagsInfoData } = useSWRxTagsInfo(currentPage?._id);
  157. const { data: tagsForEditors, mutate: mutatePageTagsForEditors, sync: syncPageTagsForEditors } = usePageTagsForEditors(currentPage?._id);
  158. const { open: openDuplicateModal } = usePageDuplicateModal();
  159. const { open: openRenameModal } = usePageRenameModal();
  160. const { open: openDeleteModal } = usePageDeleteModal();
  161. const { data: templateTagData } = useTemplateTagData();
  162. useEffect(() => {
  163. // Run only when tagsInfoData has been updated
  164. if (templateTagData == null) {
  165. syncPageTagsForEditors();
  166. }
  167. // eslint-disable-next-line react-hooks/exhaustive-deps
  168. }, [tagsInfoData?.tags]);
  169. useEffect(() => {
  170. if (pageId === null && templateTagData != null) {
  171. const tags = templateTagData.split(',').filter((str: string) => {
  172. return str !== ''; // filter empty values
  173. });
  174. mutatePageTagsForEditors(tags);
  175. }
  176. }, [pageId, mutatePageTagsForEditors, templateTagData, mutateSWRTagsInfo]);
  177. const [isPageTemplateModalShown, setIsPageTempleteModalShown] = useState(false);
  178. const { isCompactMode, isLinkSharingDisabled } = props;
  179. const isViewMode = editorMode === EditorMode.View;
  180. const tagsUpdatedHandlerForViewMode = useCallback(async(newTags: string[]) => {
  181. if (currentPage == null) {
  182. return;
  183. }
  184. const { _id: pageId, revision: revisionId } = currentPage;
  185. try {
  186. await apiPost('/tags.update', { pageId, revisionId, tags: newTags });
  187. mutateCurrentPage();
  188. // revalidate SWRTagsInfo
  189. mutateSWRTagsInfo();
  190. mutatePageTagsForEditors(newTags);
  191. toastSuccess('updated tags successfully');
  192. }
  193. catch (err) {
  194. toastError(err, 'fail to update tags');
  195. }
  196. }, [mutateSWRTagsInfo, mutatePageTagsForEditors, mutateCurrentPage, pageId, revisionId]);
  197. const tagsUpdatedHandlerForEditMode = useCallback((newTags: string[]): void => {
  198. // It will not be reflected in the DB until the page is refreshed
  199. mutatePageTagsForEditors(newTags);
  200. return;
  201. }, [mutatePageTagsForEditors]);
  202. const duplicateItemClickedHandler = useCallback(async(page: IPageForPageDuplicateModal) => {
  203. const duplicatedHandler: OnDuplicatedFunction = (fromPath, toPath) => {
  204. window.location.href = toPath;
  205. };
  206. openDuplicateModal(page, { onDuplicated: duplicatedHandler });
  207. }, [openDuplicateModal]);
  208. const renameItemClickedHandler = useCallback(async(page: IPageToRenameWithMeta<IPageInfoForEntity>) => {
  209. const renamedHandler: OnRenamedFunction = () => {
  210. if (page.data._id !== null) {
  211. window.location.href = `/${page.data._id}`;
  212. return;
  213. }
  214. window.location.reload();
  215. };
  216. openRenameModal(page, { onRenamed: renamedHandler });
  217. }, [openRenameModal]);
  218. const onDeletedHandler: OnDeletedFunction = useCallback((pathOrPathsToDelete, isRecursively, isCompletely) => {
  219. if (typeof pathOrPathsToDelete !== 'string') {
  220. return;
  221. }
  222. const path = pathOrPathsToDelete;
  223. if (isCompletely) {
  224. // redirect to NotFound Page
  225. window.location.href = path;
  226. }
  227. else {
  228. window.location.reload();
  229. }
  230. }, []);
  231. const deleteItemClickedHandler = useCallback((pageWithMeta: IPageWithMeta) => {
  232. openDeleteModal([pageWithMeta], { onDeleted: onDeletedHandler });
  233. }, [onDeletedHandler, openDeleteModal]);
  234. const templateMenuItemClickHandler = useCallback(() => {
  235. setIsPageTempleteModalShown(true);
  236. }, []);
  237. const ControlComponents = useCallback(() => {
  238. if (currentPage == null || pageId == null) {
  239. return <></>;
  240. }
  241. function onPageEditorModeButtonClicked(viewType) {
  242. mutateEditorMode(viewType);
  243. }
  244. const additionalMenuItemsRenderer = () => {
  245. if (revisionId == null || pageId == null) {
  246. return <></>;
  247. }
  248. return (
  249. <AdditionalMenuItems
  250. pageId={pageId}
  251. revisionId={revisionId}
  252. isLinkSharingDisabled={isLinkSharingDisabled}
  253. onClickTemplateMenuItem={templateMenuItemClickHandler}
  254. />
  255. );
  256. };
  257. return (
  258. <>
  259. <div className="d-flex flex-column align-items-end justify-content-center py-md-2" style={{ gap: `${isCompactMode ? '5px' : '7px'}` }}>
  260. { isViewMode && (
  261. <div className="h-50 w-100">
  262. <SubNavButtons
  263. isCompactMode={isCompactMode}
  264. pageId={pageId}
  265. revisionId={revisionId}
  266. shareLinkId={shareLinkId}
  267. path={path}
  268. disableSeenUserInfoPopover={isSharedUser}
  269. showPageControlDropdown={isAbleToShowPageManagement}
  270. additionalMenuItemRenderer={additionalMenuItemsRenderer}
  271. onClickDuplicateMenuItem={duplicateItemClickedHandler}
  272. onClickRenameMenuItem={renameItemClickedHandler}
  273. onClickDeleteMenuItem={deleteItemClickedHandler}
  274. />
  275. </div>
  276. ) }
  277. {isAbleToShowPageEditorModeManager && (
  278. <PageEditorModeManager
  279. onPageEditorModeButtonClicked={onPageEditorModeButtonClicked}
  280. isBtnDisabled={isGuestUser}
  281. editorMode={editorMode}
  282. />
  283. )}
  284. </div>
  285. {path != null && currentUser != null && (
  286. <CreateTemplateModal
  287. path={path}
  288. isOpen={isPageTemplateModalShown}
  289. onClose={() => setIsPageTempleteModalShown(false)}
  290. />
  291. )}
  292. </>
  293. );
  294. }, [
  295. pageId, revisionId, editorMode, mutateEditorMode, isCompactMode,
  296. isLinkSharingDisabled, isGuestUser, isSharedUser, currentUser,
  297. isViewMode, isAbleToShowPageEditorModeManager, isAbleToShowPageManagement,
  298. duplicateItemClickedHandler, renameItemClickedHandler, deleteItemClickedHandler,
  299. templateMenuItemClickHandler, isPageTemplateModalShown,
  300. ]);
  301. if (currentPathname == null) {
  302. return <></>;
  303. }
  304. const notFoundPage: Partial<IPageHasId> = {
  305. path: currentPathname,
  306. };
  307. return (
  308. <GrowiSubNavigation
  309. page={currentPage ?? notFoundPage}
  310. showDrawerToggler={isDrawerMode}
  311. showTagLabel={isAbleToShowTagLabel}
  312. showPageAuthors={isAbleToShowPageAuthors}
  313. isGuestUser={isGuestUser}
  314. isDrawerMode={isDrawerMode}
  315. isCompactMode={isCompactMode}
  316. tags={isViewMode ? tagsInfoData?.tags : tagsForEditors}
  317. tagsUpdatedHandler={isViewMode ? tagsUpdatedHandlerForViewMode : tagsUpdatedHandlerForEditMode}
  318. controls={ControlComponents}
  319. additionalClasses={['container-fluid']}
  320. />
  321. );
  322. };
  323. export default GrowiContextualSubNavigation;