PageSelectModal.tsx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import type { FC } from 'react';
  2. import {
  3. Suspense, useState, useCallback,
  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 SimpleBar from 'simplebar-react';
  12. import type { IPageForItem } from '~/interfaces/page';
  13. import { useIsGuestUser, useIsReadOnlyUser } from '~/stores-universal/context';
  14. import { usePageSelectModal } from '~/stores/modal';
  15. import { useSWRxCurrentPage } from '~/stores/page';
  16. import { ItemsTree } from '../ItemsTree';
  17. import ItemsTreeContentSkeleton from '../ItemsTree/ItemsTreeContentSkeleton';
  18. import { usePagePathRenameHandler } from '../PageEditor/page-path-rename-utils';
  19. import { TreeItemForModal } from './TreeItemForModal';
  20. export const PageSelectModal: FC = () => {
  21. const {
  22. data: PageSelectModalData,
  23. close: closeModal,
  24. } = usePageSelectModal();
  25. const isOpened = PageSelectModalData?.isOpened ?? false;
  26. const [clickedParentPagePath, setClickedParentPagePath] = useState<string | null>(null);
  27. const { t } = useTranslation();
  28. const { data: isGuestUser } = useIsGuestUser();
  29. const { data: isReadOnlyUser } = useIsReadOnlyUser();
  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. const parentPagePath = pathUtils.addTrailingSlash(nodePath.dirname(currentPage?.path ?? ''));
  52. const targetPathOrId = clickedParentPagePath || parentPagePath;
  53. const targetPath = clickedParentPagePath || parentPagePath;
  54. if (isGuestUser == null) {
  55. return <></>;
  56. }
  57. return (
  58. <Modal
  59. isOpen={isOpened}
  60. toggle={closeModal}
  61. centered
  62. >
  63. <ModalHeader toggle={closeModal}>{t('page_select_modal.select_page_location')}</ModalHeader>
  64. <ModalBody className="p-0">
  65. <Suspense fallback={<ItemsTreeContentSkeleton />}>
  66. <SimpleBar style={{ maxHeight: 'calc(85vh - 133px)' }}> {/* 133px = 63px(ModalHeader) + 70px(ModalFooter) */}
  67. <div className="p-3">
  68. <ItemsTree
  69. CustomTreeItem={TreeItemForModal}
  70. isEnableActions={!isGuestUser}
  71. isReadOnlyUser={!!isReadOnlyUser}
  72. targetPath={targetPath}
  73. targetPathOrId={targetPathOrId}
  74. onClickTreeItem={onClickTreeItem}
  75. />
  76. </div>
  77. </SimpleBar>
  78. </Suspense>
  79. </ModalBody>
  80. <ModalFooter>
  81. <Button color="secondary" onClick={onClickCancel}>{t('Cancel')}</Button>
  82. <Button color="primary" onClick={onClickDone}>{t('Done')}</Button>
  83. </ModalFooter>
  84. </Modal>
  85. );
  86. };