GrowiContextualSubNavigation.tsx 13 KB

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