|
@@ -1,8 +1,11 @@
|
|
|
-import React, { useCallback, useState, type JSX } from 'react';
|
|
|
|
|
|
|
+import React, {
|
|
|
|
|
+ useCallback, useMemo, useState, type JSX,
|
|
|
|
|
+} from 'react';
|
|
|
|
|
|
|
|
import nodePath from 'path';
|
|
import nodePath from 'path';
|
|
|
|
|
|
|
|
import type { IPageHasId, IPageInfoExt, IPageToDeleteWithMeta } from '@growi/core';
|
|
import type { IPageHasId, IPageInfoExt, IPageToDeleteWithMeta } from '@growi/core';
|
|
|
|
|
+import { getIdStringForRef } from '@growi/core';
|
|
|
import { DevidedPagePath } from '@growi/core/dist/models';
|
|
import { DevidedPagePath } from '@growi/core/dist/models';
|
|
|
import { pathUtils } from '@growi/core/dist/utils';
|
|
import { pathUtils } from '@growi/core/dist/utils';
|
|
|
import { useRouter } from 'next/router';
|
|
import { useRouter } from 'next/router';
|
|
@@ -59,17 +62,21 @@ export const BookmarkItem = (props: Props): JSX.Element => {
|
|
|
...bookmarkedPage, parentFolder,
|
|
...bookmarkedPage, parentFolder,
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
|
|
+ const bookmarkedPageId = bookmarkedPage?._id;
|
|
|
|
|
+ const bookmarkedPagePath = bookmarkedPage?.path;
|
|
|
|
|
+ const bookmarkedPageRevision = bookmarkedPage?.revision;
|
|
|
|
|
+
|
|
|
const onClickMoveToRootHandler = useCallback(async() => {
|
|
const onClickMoveToRootHandler = useCallback(async() => {
|
|
|
- if (bookmarkedPage == null) return;
|
|
|
|
|
|
|
+ if (bookmarkedPageId == null) return;
|
|
|
|
|
|
|
|
try {
|
|
try {
|
|
|
- await addBookmarkToFolder(bookmarkedPage._id, null);
|
|
|
|
|
|
|
+ await addBookmarkToFolder(bookmarkedPageId, null);
|
|
|
bookmarkFolderTreeMutation();
|
|
bookmarkFolderTreeMutation();
|
|
|
}
|
|
}
|
|
|
catch (err) {
|
|
catch (err) {
|
|
|
toastError(err);
|
|
toastError(err);
|
|
|
}
|
|
}
|
|
|
- }, [bookmarkFolderTreeMutation, bookmarkedPage]);
|
|
|
|
|
|
|
+ }, [bookmarkFolderTreeMutation, bookmarkedPageId]);
|
|
|
|
|
|
|
|
const bookmarkMenuItemClickHandler = useCallback(async(pageId: string, shouldBookmark: boolean) => {
|
|
const bookmarkMenuItemClickHandler = useCallback(async(pageId: string, shouldBookmark: boolean) => {
|
|
|
if (shouldBookmark) {
|
|
if (shouldBookmark) {
|
|
@@ -91,23 +98,23 @@ export const BookmarkItem = (props: Props): JSX.Element => {
|
|
|
}, []);
|
|
}, []);
|
|
|
|
|
|
|
|
const rename = useCallback(async(inputText: string) => {
|
|
const rename = useCallback(async(inputText: string) => {
|
|
|
- if (bookmarkedPage == null) return;
|
|
|
|
|
|
|
+ if (bookmarkedPageId == null) return;
|
|
|
|
|
|
|
|
|
|
|
|
|
if (inputText.trim() === '') {
|
|
if (inputText.trim() === '') {
|
|
|
return cancel();
|
|
return cancel();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- const parentPath = pathUtils.addTrailingSlash(nodePath.dirname(bookmarkedPage.path ?? ''));
|
|
|
|
|
|
|
+ const parentPath = pathUtils.addTrailingSlash(nodePath.dirname(bookmarkedPagePath ?? ''));
|
|
|
const newPagePath = nodePath.resolve(parentPath, inputText.trim());
|
|
const newPagePath = nodePath.resolve(parentPath, inputText.trim());
|
|
|
- if (newPagePath === bookmarkedPage.path) {
|
|
|
|
|
|
|
+ if (newPagePath === bookmarkedPagePath) {
|
|
|
setRenameInputShown(false);
|
|
setRenameInputShown(false);
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
try {
|
|
|
setRenameInputShown(false);
|
|
setRenameInputShown(false);
|
|
|
- await renamePage(bookmarkedPage._id, bookmarkedPage.revision, newPagePath);
|
|
|
|
|
|
|
+ await renamePage(bookmarkedPageId, bookmarkedPageRevision, newPagePath);
|
|
|
bookmarkFolderTreeMutation();
|
|
bookmarkFolderTreeMutation();
|
|
|
mutatePageInfo();
|
|
mutatePageInfo();
|
|
|
}
|
|
}
|
|
@@ -115,26 +122,26 @@ export const BookmarkItem = (props: Props): JSX.Element => {
|
|
|
setRenameInputShown(true);
|
|
setRenameInputShown(true);
|
|
|
toastError(err);
|
|
toastError(err);
|
|
|
}
|
|
}
|
|
|
- }, [bookmarkedPage, cancel, bookmarkFolderTreeMutation, mutatePageInfo]);
|
|
|
|
|
|
|
+ }, [bookmarkedPageId, bookmarkedPagePath, bookmarkedPageRevision, cancel, bookmarkFolderTreeMutation, mutatePageInfo]);
|
|
|
|
|
|
|
|
const deleteMenuItemClickHandler = useCallback(async(_pageId: string, pageInfo: IPageInfoExt | undefined): Promise<void> => {
|
|
const deleteMenuItemClickHandler = useCallback(async(_pageId: string, pageInfo: IPageInfoExt | undefined): Promise<void> => {
|
|
|
- if (bookmarkedPage == null) return;
|
|
|
|
|
|
|
+ if (bookmarkedPageId == null) return;
|
|
|
|
|
|
|
|
- if (bookmarkedPage._id == null || bookmarkedPage.path == null) {
|
|
|
|
|
|
|
+ if (bookmarkedPageId == null || bookmarkedPagePath == null) {
|
|
|
throw Error('_id and path must not be null.');
|
|
throw Error('_id and path must not be null.');
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
const pageToDelete: IPageToDeleteWithMeta = {
|
|
const pageToDelete: IPageToDeleteWithMeta = {
|
|
|
data: {
|
|
data: {
|
|
|
- _id: bookmarkedPage._id,
|
|
|
|
|
- revision: bookmarkedPage.revision as string,
|
|
|
|
|
- path: bookmarkedPage.path,
|
|
|
|
|
|
|
+ _id: bookmarkedPageId,
|
|
|
|
|
+ revision: bookmarkedPageRevision == null ? null : getIdStringForRef(bookmarkedPageRevision),
|
|
|
|
|
+ path: bookmarkedPagePath,
|
|
|
},
|
|
},
|
|
|
meta: pageInfo,
|
|
meta: pageInfo,
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
onClickDeleteMenuItemHandler(pageToDelete);
|
|
onClickDeleteMenuItemHandler(pageToDelete);
|
|
|
- }, [bookmarkedPage, onClickDeleteMenuItemHandler]);
|
|
|
|
|
|
|
+ }, [bookmarkedPageId, bookmarkedPagePath, bookmarkedPageRevision, onClickDeleteMenuItemHandler]);
|
|
|
|
|
|
|
|
const putBackClickHandler = useCallback(() => {
|
|
const putBackClickHandler = useCallback(() => {
|
|
|
if (bookmarkedPage == null) return;
|
|
if (bookmarkedPage == null) return;
|
|
@@ -156,15 +163,33 @@ export const BookmarkItem = (props: Props): JSX.Element => {
|
|
|
openPutBackPageModal({ pageId, path }, { onPutBacked: putBackedHandler });
|
|
openPutBackPageModal({ pageId, path }, { onPutBacked: putBackedHandler });
|
|
|
}, [bookmarkedPage, openPutBackPageModal, bookmarkFolderTreeMutation, router, fetchCurrentPage, t]);
|
|
}, [bookmarkedPage, openPutBackPageModal, bookmarkFolderTreeMutation, router, fetchCurrentPage, t]);
|
|
|
|
|
|
|
|
|
|
+ const {
|
|
|
|
|
+ pageTitle, formerPagePath, isFormerRoot, bookmarkItemId,
|
|
|
|
|
+ } = useMemo(() => {
|
|
|
|
|
+ const bookmarkItemId = `bookmark-item-${bookmarkedPageId}`;
|
|
|
|
|
+
|
|
|
|
|
+ if (bookmarkedPagePath == null) {
|
|
|
|
|
+ return {
|
|
|
|
|
+ pageTitle: '',
|
|
|
|
|
+ formerPagePath: '',
|
|
|
|
|
+ isFormerRoot: false,
|
|
|
|
|
+ bookmarkItemId,
|
|
|
|
|
+ };
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ const dPagePath = new DevidedPagePath(bookmarkedPagePath, false, true);
|
|
|
|
|
+ return {
|
|
|
|
|
+ pageTitle: dPagePath.latter,
|
|
|
|
|
+ formerPagePath: dPagePath.former,
|
|
|
|
|
+ isFormerRoot: dPagePath.isFormerRoot,
|
|
|
|
|
+ bookmarkItemId,
|
|
|
|
|
+ };
|
|
|
|
|
+ }, [bookmarkedPagePath, bookmarkedPageId]);
|
|
|
|
|
+
|
|
|
if (bookmarkedPage == null) {
|
|
if (bookmarkedPage == null) {
|
|
|
return <></>;
|
|
return <></>;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- const dPagePath = new DevidedPagePath(bookmarkedPage.path, false, true);
|
|
|
|
|
- const { latter: pageTitle, former: formerPagePath } = dPagePath;
|
|
|
|
|
-
|
|
|
|
|
- const bookmarkItemId = `bookmark-item-${bookmarkedPage._id}`;
|
|
|
|
|
-
|
|
|
|
|
return (
|
|
return (
|
|
|
<DragAndDropWrapper
|
|
<DragAndDropWrapper
|
|
|
item={dragItem}
|
|
item={dragItem}
|
|
@@ -215,7 +240,7 @@ export const BookmarkItem = (props: Props): JSX.Element => {
|
|
|
target={bookmarkItemId}
|
|
target={bookmarkItemId}
|
|
|
fade={false}
|
|
fade={false}
|
|
|
>
|
|
>
|
|
|
- {dPagePath.isFormerRoot ? '/' : `${formerPagePath}/`}
|
|
|
|
|
|
|
+ {isFormerRoot ? '/' : `${formerPagePath}/`}
|
|
|
</UncontrolledTooltip>
|
|
</UncontrolledTooltip>
|
|
|
</li>
|
|
</li>
|
|
|
</DragAndDropWrapper>
|
|
</DragAndDropWrapper>
|