PageSelectModal.tsx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import type { FC } from 'react';
  2. import {
  3. Suspense, useState, useCallback, useEffect,
  4. } from 'react';
  5. import nodePath from 'path';
  6. import { pathUtils } from '@growi/core/dist/utils';
  7. import { useTranslation } from 'next-i18next';
  8. import {
  9. Modal, ModalHeader, ModalBody, ModalFooter, Button,
  10. } from 'reactstrap';
  11. import type { IPageForItem } from '~/interfaces/page';
  12. import { useTargetAndAncestors, useIsGuestUser, useIsReadOnlyUser } from '~/stores/context';
  13. import { usePageSelectModal } from '~/stores/modal';
  14. import { useSWRxCurrentPage } from '~/stores/page';
  15. import { ItemsTree } from '../ItemsTree';
  16. import ItemsTreeContentSkeleton from '../ItemsTree/ItemsTreeContentSkeleton';
  17. import { usePagePathRenameHandler } from '../PageEditor/page-path-rename-utils';
  18. import { TreeItemForModal } from './TreeItemForModal';
  19. export const PageSelectModal: FC = () => {
  20. const {
  21. data: PageSelectModalData,
  22. close: closeModal,
  23. } = usePageSelectModal();
  24. const isOpened = PageSelectModalData?.isOpened ?? false;
  25. const [clickedParentPagePath, setClickedParentPagePath] = useState<string | null>(null);
  26. const { t } = useTranslation();
  27. const { data: isGuestUser } = useIsGuestUser();
  28. const { data: isReadOnlyUser } = useIsReadOnlyUser();
  29. const { data: targetAndAncestorsData } = useTargetAndAncestors();
  30. const { data: currentPage } = useSWRxCurrentPage();
  31. const pagePathRenameHandler = usePagePathRenameHandler(currentPage);
  32. const onClickTreeItem = useCallback((page: IPageForItem) => {
  33. const parentPagePath = page.path;
  34. if (parentPagePath == null) {
  35. return <></>;
  36. }
  37. setClickedParentPagePath(parentPagePath);
  38. }, []);
  39. const onClickCancel = useCallback(() => {
  40. setClickedParentPagePath(null);
  41. closeModal();
  42. }, [closeModal]);
  43. const onClickDone = useCallback(() => {
  44. if (clickedParentPagePath != null) {
  45. const currentPageTitle = nodePath.basename(currentPage?.path ?? '') || '/';
  46. const newPagePath = nodePath.resolve(clickedParentPagePath, currentPageTitle);
  47. pagePathRenameHandler(newPagePath);
  48. }
  49. closeModal();
  50. }, [clickedParentPagePath, closeModal, currentPage?.path, pagePathRenameHandler]);
  51. if (currentPage == null) {
  52. return <></>;
  53. }
  54. // const parentPagePath = pathUtils.addTrailingSlash(nodePath.dirname(currentPage.path));
  55. const targetPathOrId = clickedParentPagePath || currentPage.path;
  56. const targetPath = clickedParentPagePath || currentPage.path;
  57. // console.log(clickedParentPagePath);
  58. // console.log(targetPathOrId);
  59. if (isGuestUser == null) {
  60. return <></>;
  61. }
  62. return (
  63. <Modal
  64. isOpen={isOpened}
  65. toggle={closeModal}
  66. centered
  67. size="sm"
  68. >
  69. <ModalHeader toggle={closeModal}>{t('page_select_modal.select_page_location')}</ModalHeader>
  70. <ModalBody>
  71. <Suspense fallback={<ItemsTreeContentSkeleton />}>
  72. <ItemsTree
  73. CustomTreeItem={TreeItemForModal}
  74. isEnableActions={!isGuestUser}
  75. isReadOnlyUser={!!isReadOnlyUser}
  76. targetPath={targetPath}
  77. targetPathOrId={targetPathOrId}
  78. targetAndAncestorsData={targetAndAncestorsData}
  79. onClickTreeItem={onClickTreeItem}
  80. />
  81. </Suspense>
  82. </ModalBody>
  83. <ModalFooter>
  84. <Button color="secondary" onClick={onClickCancel}>{t('Cancel')}</Button>
  85. <Button color="primary" onClick={onClickDone}>{t('Done')}</Button>
  86. </ModalFooter>
  87. </Modal>
  88. );
  89. };