GrowiContextualSubNavigation.tsx 13 KB

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