|
|
@@ -1,32 +1,25 @@
|
|
|
import React, { useState, useEffect, useCallback } from 'react';
|
|
|
|
|
|
-import PropTypes from 'prop-types';
|
|
|
import { useTranslation } from 'next-i18next';
|
|
|
import { DropdownItem } from 'reactstrap';
|
|
|
|
|
|
-import EditorContainer from '~/client/services/EditorContainer';
|
|
|
-import PageContainer from '~/client/services/PageContainer';
|
|
|
import { exportAsMarkdown } from '~/client/services/page-operation';
|
|
|
import { toastSuccess, toastError } from '~/client/util/apiNotification';
|
|
|
import { apiPost } from '~/client/util/apiv1-client';
|
|
|
-import { getIdForRef } from '~/interfaces/common';
|
|
|
+import { isPopulated } from '~/interfaces/common';
|
|
|
import {
|
|
|
- IPageHasId, IPageToRenameWithMeta, IPageWithMeta, IPageInfoForEntity,
|
|
|
+ IPageToRenameWithMeta, IPageWithMeta, IPageInfoForEntity,
|
|
|
} from '~/interfaces/page';
|
|
|
-import { IResTagsUpdateApiv1 } from '~/interfaces/tag';
|
|
|
import { OnDuplicatedFunction, OnRenamedFunction, OnDeletedFunction } from '~/interfaces/ui';
|
|
|
-import {
|
|
|
- useCurrentCreatedAt, useCurrentUpdatedAt, useCurrentPageId, useRevisionId, useCurrentPagePath,
|
|
|
- useCreator, useRevisionAuthor, useCurrentUser, useIsGuestUser, useIsSharedUser, useShareLinkId, useEmptyPageId,
|
|
|
-} from '~/stores/context';
|
|
|
+import { useCurrentUser, useIsGuestUser, useIsSharedUser } from '~/stores/context';
|
|
|
import { usePageTagsForEditors } from '~/stores/editor';
|
|
|
import {
|
|
|
usePageAccessoriesModal, PageAccessoriesModalContents, IPageForPageDuplicateModal,
|
|
|
usePageDuplicateModal, usePageRenameModal, usePageDeleteModal, usePagePresentationModal,
|
|
|
} from '~/stores/modal';
|
|
|
-import { useSWRxTagsInfo } from '~/stores/page';
|
|
|
+import { useSWRxCurrentPage, useSWRxTagsInfo } from '~/stores/page';
|
|
|
import {
|
|
|
- EditorMode, useDrawerMode, useEditorMode, useIsDeviceSmallerThanMd, useIsAbleToShowPageManagement, useIsAbleToShowTagLabel,
|
|
|
+ EditorMode, useDrawerMode, useEditorMode, useIsAbleToShowPageManagement, useIsAbleToShowTagLabel,
|
|
|
useIsAbleToShowPageEditorModeManager, useIsAbleToShowPageAuthors,
|
|
|
} from '~/stores/ui';
|
|
|
|
|
|
@@ -36,7 +29,6 @@ import AttachmentIcon from '../Icons/AttachmentIcon';
|
|
|
import HistoryIcon from '../Icons/HistoryIcon';
|
|
|
import PresentationIcon from '../Icons/PresentationIcon';
|
|
|
import ShareLinkIcon from '../Icons/ShareLinkIcon';
|
|
|
-import { withUnstatedContainers } from '../UnstatedUtils';
|
|
|
|
|
|
|
|
|
import { GrowiSubNavigation } from './GrowiSubNavigation';
|
|
|
@@ -149,23 +141,25 @@ const AdditionalMenuItems = (props: AdditionalMenuItemsProps): JSX.Element => {
|
|
|
);
|
|
|
};
|
|
|
|
|
|
+type GrowiContextualSubNavigationProps = {
|
|
|
+ isCompactMode: boolean,
|
|
|
+ isLinkSharingDisabled: boolean,
|
|
|
+};
|
|
|
+
|
|
|
+const GrowiContextualSubNavigation = (props: GrowiContextualSubNavigationProps): JSX.Element => {
|
|
|
+
|
|
|
+ const { data: pageData, mutate: mutateCurrentPage } = useSWRxCurrentPage();
|
|
|
+ const pageId = pageData?._id;
|
|
|
+ const path = pageData?.path;
|
|
|
+
|
|
|
+ const revision = pageData?.revision;
|
|
|
+ const revisionId = (revision != null && isPopulated(revision)) ? revision._id : undefined;
|
|
|
|
|
|
-const GrowiContextualSubNavigation = (props) => {
|
|
|
- const { data: isDeviceSmallerThanMd } = useIsDeviceSmallerThanMd();
|
|
|
const { data: isDrawerMode } = useDrawerMode();
|
|
|
const { data: editorMode, mutate: mutateEditorMode } = useEditorMode();
|
|
|
- const { data: createdAt } = useCurrentCreatedAt();
|
|
|
- const { data: updatedAt } = useCurrentUpdatedAt();
|
|
|
- const { data: pageId } = useCurrentPageId();
|
|
|
- const { data: emptyPageId } = useEmptyPageId();
|
|
|
- const { data: revisionId } = useRevisionId();
|
|
|
- const { data: path } = useCurrentPagePath();
|
|
|
- const { data: creator } = useCreator();
|
|
|
- const { data: revisionAuthor } = useRevisionAuthor();
|
|
|
const { data: currentUser } = useCurrentUser();
|
|
|
const { data: isGuestUser } = useIsGuestUser();
|
|
|
const { data: isSharedUser } = useIsSharedUser();
|
|
|
- const { data: shareLinkId } = useShareLinkId();
|
|
|
|
|
|
const { data: isAbleToShowPageManagement } = useIsAbleToShowPageManagement();
|
|
|
const { data: isAbleToShowTagLabel } = useIsAbleToShowTagLabel();
|
|
|
@@ -187,18 +181,15 @@ const GrowiContextualSubNavigation = (props) => {
|
|
|
|
|
|
const [isPageTemplateModalShown, setIsPageTempleteModalShown] = useState(false);
|
|
|
|
|
|
- const {
|
|
|
- isCompactMode, isLinkSharingDisabled, pageContainer,
|
|
|
- } = props;
|
|
|
+ const { isCompactMode, isLinkSharingDisabled } = props;
|
|
|
|
|
|
const isViewMode = editorMode === EditorMode.View;
|
|
|
|
|
|
|
|
|
const tagsUpdatedHandlerForViewMode = useCallback(async(newTags: string[]) => {
|
|
|
try {
|
|
|
- const res: IResTagsUpdateApiv1 = await apiPost('/tags.update', { pageId, revisionId, tags: newTags });
|
|
|
- const updatedRevisionId = getIdForRef(res.savedPage.revision);
|
|
|
- await pageContainer.setState({ revisionId: updatedRevisionId });
|
|
|
+ await apiPost('/tags.update', { pageId, revisionId, tags: newTags });
|
|
|
+ mutateCurrentPage();
|
|
|
|
|
|
// revalidate SWRTagsInfo
|
|
|
mutateSWRTagsInfo();
|
|
|
@@ -210,7 +201,7 @@ const GrowiContextualSubNavigation = (props) => {
|
|
|
toastError(err, 'fail to update tags');
|
|
|
}
|
|
|
|
|
|
- }, [pageId, revisionId, mutateSWRTagsInfo, mutatePageTagsForEditors, pageContainer]);
|
|
|
+ }, [mutateSWRTagsInfo, mutatePageTagsForEditors, mutateCurrentPage, pageId, revisionId]);
|
|
|
|
|
|
const tagsUpdatedHandlerForEditMode = useCallback((newTags: string[]): void => {
|
|
|
// It will not be reflected in the DB until the page is refreshed
|
|
|
@@ -262,7 +253,7 @@ const GrowiContextualSubNavigation = (props) => {
|
|
|
|
|
|
|
|
|
const ControlComponents = useCallback(() => {
|
|
|
- const pageIdForSubNavButtons = pageId ?? emptyPageId; // for SubNavButtons
|
|
|
+ const pageIdForSubNavButtons = pageId; // for SubNavButtons
|
|
|
|
|
|
function onPageEditorModeButtonClicked(viewType) {
|
|
|
mutateEditorMode(viewType);
|
|
|
@@ -288,7 +279,6 @@ const GrowiContextualSubNavigation = (props) => {
|
|
|
<SubNavButtons
|
|
|
isCompactMode={isCompactMode}
|
|
|
pageId={pageIdForSubNavButtons}
|
|
|
- shareLinkId={shareLinkId}
|
|
|
revisionId={revisionId}
|
|
|
path={path}
|
|
|
disableSeenUserInfoPopover={isSharedUser}
|
|
|
@@ -305,7 +295,6 @@ const GrowiContextualSubNavigation = (props) => {
|
|
|
onPageEditorModeButtonClicked={onPageEditorModeButtonClicked}
|
|
|
isBtnDisabled={isGuestUser}
|
|
|
editorMode={editorMode}
|
|
|
- isDeviceSmallerThanMd={isDeviceSmallerThanMd}
|
|
|
/>
|
|
|
)}
|
|
|
</div>
|
|
|
@@ -319,30 +308,20 @@ const GrowiContextualSubNavigation = (props) => {
|
|
|
</>
|
|
|
);
|
|
|
}, [
|
|
|
- pageId, emptyPageId, revisionId, shareLinkId, editorMode, mutateEditorMode, isCompactMode,
|
|
|
- isLinkSharingDisabled, isDeviceSmallerThanMd, isGuestUser, isSharedUser, currentUser,
|
|
|
+ pageId, revisionId, editorMode, mutateEditorMode, isCompactMode,
|
|
|
+ isLinkSharingDisabled, isGuestUser, isSharedUser, currentUser,
|
|
|
isViewMode, isAbleToShowPageEditorModeManager, isAbleToShowPageManagement,
|
|
|
duplicateItemClickedHandler, renameItemClickedHandler, deleteItemClickedHandler,
|
|
|
path, templateMenuItemClickHandler, isPageTemplateModalShown,
|
|
|
]);
|
|
|
|
|
|
- if (path == null) {
|
|
|
+ if (pageData == null) {
|
|
|
return <></>;
|
|
|
}
|
|
|
|
|
|
- const currentPage: Partial<IPageHasId> = {
|
|
|
- _id: pageId ?? undefined,
|
|
|
- path,
|
|
|
- revision: revisionId ?? undefined,
|
|
|
- creator: creator ?? undefined,
|
|
|
- lastUpdateUser: revisionAuthor,
|
|
|
- createdAt: createdAt ?? undefined,
|
|
|
- updatedAt: updatedAt ?? undefined,
|
|
|
- };
|
|
|
-
|
|
|
return (
|
|
|
<GrowiSubNavigation
|
|
|
- page={currentPage}
|
|
|
+ page={pageData}
|
|
|
showDrawerToggler={isDrawerMode}
|
|
|
showTagLabel={isAbleToShowTagLabel}
|
|
|
showPageAuthors={isAbleToShowPageAuthors}
|
|
|
@@ -357,18 +336,5 @@ const GrowiContextualSubNavigation = (props) => {
|
|
|
);
|
|
|
};
|
|
|
|
|
|
-/**
|
|
|
- * Wrapper component for using unstated
|
|
|
- */
|
|
|
-const GrowiContextualSubNavigationWrapper = withUnstatedContainers(GrowiContextualSubNavigation, [EditorContainer, PageContainer]);
|
|
|
-
|
|
|
-
|
|
|
-GrowiContextualSubNavigation.propTypes = {
|
|
|
- editorContainer: PropTypes.instanceOf(EditorContainer).isRequired,
|
|
|
- pageContainer: PropTypes.instanceOf(PageContainer).isRequired,
|
|
|
-
|
|
|
- isCompactMode: PropTypes.bool,
|
|
|
- isLinkSharingDisabled: PropTypes.bool,
|
|
|
-};
|
|
|
|
|
|
-export default GrowiContextualSubNavigationWrapper;
|
|
|
+export default GrowiContextualSubNavigation;
|