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

impl mutateDescendantsPageListForCurrentPath instead of useDescendantsPageListForCurrentPathTermManager

Yuki Takei 3 лет назад
Родитель
Сommit
8304210097

+ 5 - 7
packages/app/src/components/DescendantsPageList.tsx

@@ -15,8 +15,9 @@ import {
 } from '~/stores/context';
 import { useIsTrashPage } from '~/stores/page';
 import {
+  mutateDescendantsPageListForCurrentPath,
   mutatePageTree,
-  useDescendantsPageListForCurrentPathTermManager, useSWRxDescendantsPageListForCurrrentPath,
+  useSWRxDescendantsPageListForCurrrentPath,
   useSWRxPageInfoForList, useSWRxPageList,
 } from '~/stores/page-listing';
 
@@ -53,9 +54,6 @@ export const DescendantsPageListSubstance = (props: SubstanceProps): JSX.Element
 
   let pageWithMetas: IDataWithMeta<IPageHasId, IPageInfoForOperation>[] = [];
 
-  // for mutation
-  const { advance: advanceDpl } = useDescendantsPageListForCurrentPathTermManager();
-
   // initial data
   if (pagingResult != null) {
     // convert without meta at first
@@ -85,12 +83,12 @@ export const DescendantsPageListSubstance = (props: SubstanceProps): JSX.Element
     toastSuccess(t('page_has_been_reverted', { path }));
 
     mutatePageTree();
-    advanceDpl();
+    mutateDescendantsPageListForCurrentPath();
 
     if (onPagePutBacked != null) {
       onPagePutBacked(path);
     }
-  }, [advanceDpl, onPagePutBacked, t]);
+  }, [onPagePutBacked, t]);
 
   function setPageNumber(selectedPageNumber) {
     setActivePage(selectedPageNumber);
@@ -144,7 +142,7 @@ export const DescendantsPageList = (props: DescendantsPageListProps): JSX.Elemen
 
   const { data: isSharedUser } = useIsSharedUser();
 
-  const { data: pagingResult, error, mutate } = useSWRxPageList(isSharedUser ? null : path, activePage);
+  const { data: pagingResult, error, mutate } = useSWRxPageList(null, isSharedUser ? null : path, activePage);
 
   if (error != null) {
     return (

+ 7 - 8
packages/app/src/components/SearchPage/SearchResultContent.tsx

@@ -18,7 +18,7 @@ import { useCurrentUser, useIsContainerFluid } from '~/stores/context';
 import {
   usePageDuplicateModal, usePageRenameModal, usePageDeleteModal,
 } from '~/stores/modal';
-import { mutatePageTree, useDescendantsPageListForCurrentPathTermManager } from '~/stores/page-listing';
+import { mutateDescendantsPageListForCurrentPath, mutatePageTree } from '~/stores/page-listing';
 import { useSearchResultOptions } from '~/stores/renderer';
 import { useFullTextSearchTermManager } from '~/stores/search';
 
@@ -93,7 +93,6 @@ export const SearchResultContent: FC<Props> = (props: Props) => {
 
   // for mutation
   const { advance: advanceFts } = useFullTextSearchTermManager();
-  const { advance: advanceDpl } = useDescendantsPageListForCurrentPathTermManager();
 
   // ***************************  Auto Scroll  ***************************
   useEffect(() => {
@@ -168,10 +167,10 @@ export const SearchResultContent: FC<Props> = (props: Props) => {
 
       mutatePageTree();
       advanceFts();
-      advanceDpl();
+      mutateDescendantsPageListForCurrentPath();
     };
     openDuplicateModal(pageToDuplicate, { onDuplicated: duplicatedHandler });
-  }, [advanceDpl, advanceFts, openDuplicateModal, t]);
+  }, [advanceFts, openDuplicateModal, t]);
 
   const renameItemClickedHandler = useCallback((pageToRename: IPageToRenameWithMeta) => {
     const renamedHandler: OnRenamedFunction = (path) => {
@@ -179,10 +178,10 @@ export const SearchResultContent: FC<Props> = (props: Props) => {
 
       mutatePageTree();
       advanceFts();
-      advanceDpl();
+      mutateDescendantsPageListForCurrentPath();
     };
     openRenameModal(pageToRename, { onRenamed: renamedHandler });
-  }, [advanceDpl, advanceFts, openRenameModal, t]);
+  }, [advanceFts, openRenameModal, t]);
 
   const onDeletedHandler: OnDeletedFunction = useCallback((pathOrPathsToDelete, isRecursively, isCompletely) => {
     if (typeof pathOrPathsToDelete !== 'string') {
@@ -198,8 +197,8 @@ export const SearchResultContent: FC<Props> = (props: Props) => {
     }
     mutatePageTree();
     advanceFts();
-    advanceDpl();
-  }, [advanceDpl, advanceFts, t]);
+    mutateDescendantsPageListForCurrentPath();
+  }, [advanceFts, t]);
 
   const deleteItemClickedHandler = useCallback((pageToDelete: IPageToDeleteWithMeta) => {
     openDeleteModal([pageToDelete], { onDeleted: onDeletedHandler });

+ 7 - 8
packages/app/src/components/Sidebar/PageTree/ItemsTree.tsx

@@ -18,7 +18,7 @@ import {
 } from '~/stores/modal';
 import { useCurrentPagePath, usePageInfoTermManager, useSWRMUTxCurrentPage } from '~/stores/page';
 import {
-  useSWRxPageAncestorsChildren, useSWRxRootPage, useDescendantsPageListForCurrentPathTermManager, mutatePageTree,
+  useSWRxPageAncestorsChildren, useSWRxRootPage, mutatePageTree, mutateDescendantsPageListForCurrentPath,
 } from '~/stores/page-listing';
 import { useFullTextSearchTermManager } from '~/stores/search';
 import { usePageTreeDescCountMap, useSidebarScrollerRef } from '~/stores/ui';
@@ -119,7 +119,6 @@ const ItemsTree = (props: ItemsTreeProps): JSX.Element => {
   // for mutation
   const { trigger: mutateCurrentPage } = useSWRMUTxCurrentPage();
   const { advance: advanceFts } = useFullTextSearchTermManager();
-  const { advance: advanceDpl } = useDescendantsPageListForCurrentPathTermManager();
   const { advance: advancePi } = usePageInfoTermManager();
 
   const [isInitialScrollCompleted, setIsInitialScrollCompleted] = useState(false);
@@ -152,12 +151,12 @@ const ItemsTree = (props: ItemsTreeProps): JSX.Element => {
   const onRenamed = useCallback((fromPath: string | undefined, toPath: string) => {
     mutatePageTree();
     advanceFts();
-    advanceDpl();
+    mutateDescendantsPageListForCurrentPath();
 
     if (currentPagePath === fromPath || currentPagePath === toPath) {
       mutateCurrentPage();
     }
-  }, [advanceDpl, advanceFts, currentPagePath, mutateCurrentPage]);
+  }, [advanceFts, currentPagePath, mutateCurrentPage]);
 
   const onClickDuplicateMenuItem = useCallback((pageToDuplicate: IPageForPageDuplicateModal) => {
     // eslint-disable-next-line @typescript-eslint/no-unused-vars
@@ -166,11 +165,11 @@ const ItemsTree = (props: ItemsTreeProps): JSX.Element => {
 
       mutatePageTree();
       advanceFts();
-      advanceDpl();
+      mutateDescendantsPageListForCurrentPath();
     };
 
     openDuplicateModal(pageToDuplicate, { onDuplicated: duplicatedHandler });
-  }, [advanceDpl, advanceFts, openDuplicateModal, t]);
+  }, [advanceFts, openDuplicateModal, t]);
 
   const onClickDeleteMenuItem = useCallback((pageToDelete: IPageToDeleteWithMeta) => {
     const onDeletedHandler: OnDeletedFunction = (pathOrPathsToDelete, isRecursively, isCompletely) => {
@@ -189,7 +188,7 @@ const ItemsTree = (props: ItemsTreeProps): JSX.Element => {
 
       mutatePageTree();
       advanceFts();
-      advanceDpl();
+      mutateDescendantsPageListForCurrentPath();
       advancePi();
 
       if (currentPagePath === pathOrPathsToDelete) {
@@ -199,7 +198,7 @@ const ItemsTree = (props: ItemsTreeProps): JSX.Element => {
     };
 
     openDeleteModal([pageToDelete], { onDeleted: onDeletedHandler });
-  }, [advanceDpl, advanceFts, advancePi, currentPagePath, mutateCurrentPage, openDeleteModal, router, t]);
+  }, [advanceFts, advancePi, currentPagePath, mutateCurrentPage, openDeleteModal, router, t]);
 
   // ***************************  Scroll on init ***************************
   const scrollOnInit = useCallback(() => {

+ 30 - 33
packages/app/src/stores/page-listing.tsx

@@ -16,7 +16,6 @@ import {
 } from '../interfaces/page-listing-results';
 
 import { useCurrentPagePath } from './page';
-import { ITermNumberManagerUtil, useTermNumberManager } from './use-static-swr';
 
 export const useSWRxPagesByPath = (path?: Nullable<string>): SWRResponse<IPageHasId[], Error> => {
   const findAll = true;
@@ -45,48 +44,46 @@ export const useSWRInifinitexRecentlyUpdated = () : SWRInfiniteResponse<(IPageHa
     },
   );
 };
-// eslint-disable-next-line @typescript-eslint/no-unused-vars
 export const useSWRxPageList = (
-    path: string | null, pageNumber?: number, termNumber?: number, limit?: number,
+    mutationId: string | null,
+    path: string | null, pageNumber?: number, limit?: number,
 ): SWRResponse<IPagingResult<IPageHasId>, Error> => {
-
-  let key: [string, number|undefined] | null;
-  // if path not exist then the key is null
-  if (path == null) {
-    key = null;
-  }
-  else {
-    const pageListPath = `/pages/list?path=${path}&page=${pageNumber ?? 1}`;
-    // if limit exist then add it as query string
-    const requestPath = limit == null ? pageListPath : `${pageListPath}&limit=${limit}`;
-    key = [requestPath, termNumber];
-  }
-
   return useSWR(
-    key,
-    ([endpoint]) => apiv3Get<{pages: IPageHasId[], totalCount: number, limit: number}>(endpoint).then((response) => {
-      return {
-        items: response.data.pages,
-        totalCount: response.data.totalCount,
-        limit: response.data.limit,
-      };
-    }),
+    path == null
+      ? null
+      : [mutationId, '/pages/list', path, pageNumber, limit],
+    ([, endpoint, path, pageNumber, limit]) => {
+      const args = Object.assign(
+        { path, page: pageNumber ?? 1 },
+        // if limit exist then add it as query string
+        (limit != null) ? { limit } : {},
+      );
+
+      return apiv3Get<{pages: IPageHasId[], totalCount: number, limit: number}>(endpoint, args)
+        .then((response) => {
+          return {
+            items: response.data.pages,
+            totalCount: response.data.totalCount,
+            limit: response.data.limit,
+          };
+        });
+    },
+    {
+      keepPreviousData: true,
+    },
   );
 };
 
-export const useDescendantsPageListForCurrentPathTermManager = (isDisabled?: boolean) : SWRResponse<number, Error> & ITermNumberManagerUtil => {
-  return useTermNumberManager(isDisabled === true ? null : 'descendantsPageListForCurrentPathTermNumber');
+const MUTATION_ID_FOR_DESCENDANTS_PAGELIST_FOR_CURRENT_PATH = 'descendantsPageListForCurrentPath';
+export const mutateDescendantsPageListForCurrentPath = async(): Promise<any[]> => {
+  return mutate(
+    key => Array.isArray(key) && key[0] === MUTATION_ID_FOR_DESCENDANTS_PAGELIST_FOR_CURRENT_PATH,
+  );
 };
 
 export const useSWRxDescendantsPageListForCurrrentPath = (pageNumber?: number, limit?:number): SWRResponse<IPagingResult<IPageHasId>, Error> => {
   const { data: currentPagePath } = useCurrentPagePath();
-  const { data: termNumber } = useDescendantsPageListForCurrentPathTermManager();
-
-  const path = currentPagePath == null || termNumber == null
-    ? null
-    : currentPagePath;
-
-  return useSWRxPageList(path, pageNumber, termNumber, limit);
+  return useSWRxPageList(MUTATION_ID_FOR_DESCENDANTS_PAGELIST_FOR_CURRENT_PATH, currentPagePath ?? null, pageNumber, limit);
 };