GrowiContextualSubNavigation.tsx 14 KB

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