BookmarkFolderTree.tsx 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import React, { useCallback } from 'react';
  2. import type { IPageToDeleteWithMeta } from '@growi/core';
  3. import { useTranslation } from 'next-i18next';
  4. import { useRouter } from 'next/router';
  5. import { toastSuccess } from '~/client/util/toastr';
  6. import type { OnDeletedFunction } from '~/interfaces/ui';
  7. import {
  8. useSWRxUserBookmarks, useSWRMUTxCurrentUserBookmarks,
  9. } from '~/stores/bookmark';
  10. import { useSWRxBookmarkFolderAndChild } from '~/stores/bookmark-folder';
  11. import { useIsReadOnlyUser } from '~/stores/context';
  12. import { usePageDeleteModal } from '~/stores/modal';
  13. import { mutateAllPageInfo, useSWRMUTxPageInfo, useSWRxCurrentPage } from '~/stores/page';
  14. import { BookmarkFolderItem } from './BookmarkFolderItem';
  15. import { BookmarkItem } from './BookmarkItem';
  16. import styles from './BookmarkFolderTree.module.scss';
  17. // type DragItemDataType = {
  18. // bookmarkFolder: BookmarkFolderItems
  19. // level: number
  20. // parentFolder: BookmarkFolderItems | null
  21. // } & IPageHasId
  22. type Props = {
  23. isUserHomepage?: boolean,
  24. userId?: string,
  25. isOperable: boolean,
  26. }
  27. export const BookmarkFolderTree: React.FC<Props> = (props: Props) => {
  28. const { isUserHomepage, userId } = props;
  29. // const acceptedTypes: DragItemType[] = [DRAG_ITEM_TYPE.FOLDER, DRAG_ITEM_TYPE.BOOKMARK];
  30. const { t } = useTranslation();
  31. const router = useRouter();
  32. const { data: isReadOnlyUser } = useIsReadOnlyUser();
  33. const { data: currentPage } = useSWRxCurrentPage();
  34. const { data: bookmarkFolders, mutate: mutateBookmarkFolders } = useSWRxBookmarkFolderAndChild(userId);
  35. const { data: userBookmarks, mutate: mutateUserBookmarks } = useSWRxUserBookmarks(userId ?? null);
  36. const { trigger: mutatePageInfo } = useSWRMUTxPageInfo(currentPage?._id ?? null);
  37. const { trigger: mutateCurrentUserBookmarks } = useSWRMUTxCurrentUserBookmarks();
  38. const { open: openDeleteModal } = usePageDeleteModal();
  39. const bookmarkFolderTreeMutation = useCallback(() => {
  40. mutateUserBookmarks();
  41. mutateCurrentUserBookmarks();
  42. mutatePageInfo();
  43. mutateBookmarkFolders();
  44. }, [mutateBookmarkFolders, mutatePageInfo, mutateCurrentUserBookmarks, mutateUserBookmarks]);
  45. const onClickDeleteMenuItemHandler = useCallback((pageToDelete: IPageToDeleteWithMeta) => {
  46. const pageDeletedHandler: OnDeletedFunction = (pathOrPathsToDelete, _isRecursively, isCompletely) => {
  47. if (typeof pathOrPathsToDelete !== 'string') return;
  48. toastSuccess(isCompletely ? t('deleted_pages_completely', { path: pathOrPathsToDelete }) : t('deleted_pages', { path: pathOrPathsToDelete }));
  49. bookmarkFolderTreeMutation();
  50. mutateAllPageInfo();
  51. if (pageToDelete.data._id === currentPage?._id && _isRecursively) {
  52. router.push(`/trash${currentPage.path}`);
  53. }
  54. };
  55. openDeleteModal([pageToDelete], { onDeleted: pageDeletedHandler });
  56. }, [openDeleteModal, t, bookmarkFolderTreeMutation, currentPage?._id, currentPage?.path, router]);
  57. /* TODO: update in bookmarks folder v2. */
  58. // const itemDropHandler = async(item: DragItemDataType, dragType: string | null | symbol) => {
  59. // if (dragType === DRAG_ITEM_TYPE.FOLDER) {
  60. // try {
  61. // await updateBookmarkFolder(item.bookmarkFolder._id, item.bookmarkFolder.name, null);
  62. // await mutateBookmarkData();
  63. // toastSuccess(t('toaster.update_successed', { target: t('bookmark_folder.bookmark_folder'), ns: 'commons' }));
  64. // }
  65. // catch (err) {
  66. // toastError(err);
  67. // }
  68. // }
  69. // else {
  70. // try {
  71. // await addBookmarkToFolder(item._id, null);
  72. // await mutateUserBookmarks();
  73. // toastSuccess(t('toaster.add_succeeded', { target: t('bookmark_folder.bookmark'), ns: 'commons' }));
  74. // }
  75. // catch (err) {
  76. // toastError(err);
  77. // }
  78. // }
  79. // };
  80. // const isDroppable = (item: DragItemDataType, dragType: string | null | symbol) => {
  81. // if (dragType === DRAG_ITEM_TYPE.FOLDER) {
  82. // const isRootFolder = item.level === 0;
  83. // return !isRootFolder;
  84. // }
  85. // const isRootBookmark = item.parentFolder == null;
  86. // return !isRootBookmark;
  87. // };
  88. return (
  89. <div className={`grw-folder-tree-container ${styles['grw-folder-tree-container']}`}>
  90. <ul className={`grw-foldertree ${styles['grw-foldertree']} list-group py-2`}>
  91. {bookmarkFolders?.map((bookmarkFolder) => {
  92. return (
  93. <BookmarkFolderItem
  94. key={bookmarkFolder._id}
  95. isReadOnlyUser={!!isReadOnlyUser}
  96. isOperable={props.isOperable}
  97. bookmarkFolder={bookmarkFolder}
  98. isOpen={false}
  99. level={0}
  100. root={bookmarkFolder._id}
  101. isUserHomepage={isUserHomepage}
  102. onClickDeleteMenuItemHandler={onClickDeleteMenuItemHandler}
  103. bookmarkFolderTreeMutation={bookmarkFolderTreeMutation}
  104. />
  105. );
  106. })}
  107. {userBookmarks?.map(userBookmark => (
  108. <div key={userBookmark?._id} className="grw-foldertree-item-container grw-root-bookmarks">
  109. <BookmarkItem
  110. isReadOnlyUser={!!isReadOnlyUser}
  111. isOperable={props.isOperable}
  112. bookmarkedPage={userBookmark}
  113. level={0}
  114. parentFolder={null}
  115. canMoveToRoot={false}
  116. onClickDeleteMenuItemHandler={onClickDeleteMenuItemHandler}
  117. bookmarkFolderTreeMutation={bookmarkFolderTreeMutation}
  118. />
  119. </div>
  120. ))}
  121. </ul>
  122. {/* TODO: update in bookmarks folder v2. Also delete drop_item_here in translation.json, if don't need it. */}
  123. {/* {bookmarkFolderData != null && bookmarkFolderData.length > 0 && (
  124. <DragAndDropWrapper
  125. useDropMode={true}
  126. type={acceptedTypes}
  127. onDropItem={itemDropHandler}
  128. isDropable={isDroppable}
  129. >
  130. <div className="grw-drop-item-area">
  131. <div className="d-flex flex-column align-items-center">
  132. {t('bookmark_folder.drop_item_here')}
  133. </div>
  134. </div>
  135. </DragAndDropWrapper>
  136. )} */}
  137. </div>
  138. );
  139. };