Просмотр исходного кода

use useCreatePageAndTransit in useNewPageInput

Futa Arai 1 год назад
Родитель
Сommit
9e7455d310

+ 12 - 6
apps/app/src/client/services/create-page/use-create-page-and-transit.tsx

@@ -28,6 +28,7 @@ type OnTerminated = () => void;
 
 export type CreatePageAndTransitOpts = {
   shouldCheckPageExists?: boolean,
+  shouldTransit?: boolean,
   onCreationStart?: OnCreated,
   onCreated?: OnCreated,
   onAborted?: OnAborted,
@@ -59,6 +60,7 @@ export const useCreatePageAndTransit: UseCreatePageAndTransit = () => {
       shouldCheckPageExists,
       onCreationStart, onCreated, onAborted, onTerminated,
     } = opts;
+    const shouldTransit = opts.shouldTransit ?? true;
 
     // check the page existence
     if (shouldCheckPageExists && params.path != null) {
@@ -68,11 +70,13 @@ export const useCreatePageAndTransit: UseCreatePageAndTransit = () => {
         const { isExist } = await exist(pagePath);
 
         if (isExist) {
-          // routing
-          if (pagePath !== currentPagePath) {
-            await router.push(`${pagePath}#edit`);
+          if (shouldTransit) {
+            // routing
+            if (pagePath !== currentPagePath) {
+              await router.push(`${pagePath}#edit`);
+            }
+            mutateEditorMode(EditorMode.Editor);
           }
-          mutateEditorMode(EditorMode.Editor);
           onAborted?.();
           return;
         }
@@ -95,8 +99,10 @@ export const useCreatePageAndTransit: UseCreatePageAndTransit = () => {
 
         closeGrantedGroupsInheritanceSelectModal();
 
-        await router.push(`/${response.page._id}#edit`);
-        mutateEditorMode(EditorMode.Editor);
+        if (shouldTransit) {
+          await router.push(`/${response.page._id}#edit`);
+          mutateEditorMode(EditorMode.Editor);
+        }
 
         onCreated?.();
       }

+ 2 - 3
apps/app/src/components/Navbar/PageEditorModeManager.tsx

@@ -1,10 +1,10 @@
 import React, { type ReactNode, useCallback, useMemo } from 'react';
 
 import { Origin } from '@growi/core';
+import { normalizePath } from '@growi/core/dist/utils/path-utils';
 import { useTranslation } from 'next-i18next';
 
 import { useCreatePageAndTransit } from '~/client/services/create-page';
-import { apiv3Get } from '~/client/util/apiv3-client';
 import { toastError } from '~/client/util/toastr';
 import { useIsNotFound } from '~/stores/page';
 import { EditorMode, useEditorMode, useIsDeviceLargerThanMd } from '~/stores/ui';
@@ -77,8 +77,7 @@ export const PageEditorModeManager = (props: Props): JSX.Element => {
     }
 
     try {
-      let parentPath = path?.split('/').slice(0, -1).join('/'); // does not have to exist
-      parentPath = parentPath === '' ? '/' : parentPath;
+      const parentPath = path != null ? normalizePath(path.split('/').slice(0, -1).join('/')) : undefined; // does not have to exist
       await createAndTransit(
         {
           path, parentPath, wip: shouldCreateWipPage(path), origin: Origin.View,

+ 0 - 1
apps/app/src/components/Sidebar/PageCreateButton/hooks/use-create-todays-memo.tsx

@@ -6,7 +6,6 @@ import { format } from 'date-fns/format';
 import { useTranslation } from 'react-i18next';
 
 import { useCreatePageAndTransit } from '~/client/services/create-page';
-import { apiv3Get } from '~/client/util/apiv3-client';
 import { useCurrentUser } from '~/stores/context';
 
 

+ 28 - 19
apps/app/src/components/TreeItem/NewPageInput/use-new-page-input.tsx

@@ -11,7 +11,7 @@ import { useRect } from '@growi/ui/dist/utils';
 import { useTranslation } from 'next-i18next';
 import { debounce } from 'throttle-debounce';
 
-import { createPage } from '~/client/services/create-page';
+import { useCreatePageAndTransit } from '~/client/services/create-page';
 import { toastWarning, toastError, toastSuccess } from '~/client/util/toastr';
 import type { InputValidationResult } from '~/client/util/use-input-validator';
 import { ValidationTarget, useInputValidator } from '~/client/util/use-input-validator';
@@ -60,6 +60,7 @@ export const useNewPageInput = (): UseNewPageInput => {
   const Input: FC<TreeItemToolProps> = (props) => {
 
     const { t } = useTranslation();
+    const { createAndTransit } = useCreatePageAndTransit();
 
     const { itemNode, stateHandlers, isEnableActions } = props;
     const { page, children } = itemNode;
@@ -107,23 +108,31 @@ export const useNewPageInput = (): UseNewPageInput => {
       setShowInput(false);
 
       try {
-        await createPage({
-          path: newPagePath,
-          body: undefined,
-          // keep grant info undefined to inherit from parent
-          grant: undefined,
-          grantUserGroupIds: undefined,
-          origin: Origin.View,
-          wip: shouldCreateWipPage(newPagePath),
-        });
-
-        mutatePageTree();
-
-        if (!hasDescendants) {
-          stateHandlers?.setIsOpen(true);
-        }
-
-        toastSuccess(t('successfully_saved_the_page'));
+        await createAndTransit(
+          {
+            path: newPagePath,
+            parentPath,
+            body: undefined,
+            // keep grant info undefined to inherit from parent
+            grant: undefined,
+            grantUserGroupIds: undefined,
+            origin: Origin.View,
+            wip: shouldCreateWipPage(newPagePath),
+          },
+          {
+            shouldCheckPageExists: true,
+            shouldTransit: false,
+            onCreated: () => {
+              mutatePageTree();
+
+              if (!hasDescendants) {
+                stateHandlers?.setIsOpen(true);
+              }
+
+              toastSuccess(t('successfully_saved_the_page'));
+            },
+          },
+        );
       }
       catch (err) {
         toastError(err);
@@ -131,7 +140,7 @@ export const useNewPageInput = (): UseNewPageInput => {
       finally {
         setProcessingSubmission(false);
       }
-    }, [cancel, hasDescendants, page.path, stateHandlers, t]);
+    }, [cancel, hasDescendants, page.path, stateHandlers, t, createAndTransit]);
 
     const inputContainerClass = newPageInputStyles['new-page-input-container'] ?? '';
     const isInvalid = validationResult != null;

+ 2 - 1
apps/app/src/server/routes/apiv3/page/index.ts

@@ -6,6 +6,7 @@ import {
 } from '@growi/core';
 import { ErrorV3 } from '@growi/core/dist/models';
 import { convertToNewAffiliationPath } from '@growi/core/dist/utils/page-path-utils';
+import { normalizePath } from '@growi/core/dist/utils/path-utils';
 import mongoose from 'mongoose';
 import sanitize from 'sanitize-filename';
 
@@ -645,7 +646,7 @@ module.exports = (crowi) => {
   router.get('/non-user-related-groups-granted', loginRequiredStrictly, validator.nonUserRelatedGroupsGranted, apiV3FormValidator,
     async(req, res: ApiV3Response) => {
       const { user } = req;
-      const { path } = req.query;
+      const path = normalizePath(req.query.path);
       const pageGrantService = crowi.pageGrantService as IPageGrantService;
       try {
         const page = await Page.findByPath(path, true) ?? await Page.findNonEmptyClosestAncestor(path);