Yuki Takei 4 лет назад
Родитель
Сommit
9810a37ffa
2 измененных файлов с 29 добавлено и 2 удалено
  1. 10 2
      packages/app/src/stores/search.tsx
  2. 19 0
      packages/app/src/stores/use-static-swr.tsx

+ 10 - 2
packages/app/src/stores/search.tsx

@@ -5,6 +5,13 @@ import { apiGet } from '~/client/util/apiv1-client';
 
 import { IFormattedSearchResult, SORT_AXIS, SORT_ORDER } from '~/interfaces/search';
 
+import { ITermNumberManagerUtil, useTermNumberManager } from './use-static-swr';
+
+
+export const useFullTextSearchTermManager = (isDisabled?: boolean) : SWRResponse<number, Error> & ITermNumberManagerUtil => {
+  return useTermNumberManager(isDisabled === true ? null : 'fullTextSearchTermNumber');
+};
+
 
 export type ISearchConfigurations = {
   limit: number,
@@ -44,8 +51,9 @@ const createSearchQuery = (keyword: string, includeTrashPages: boolean, includeU
 };
 
 export const useSWRxFullTextSearch = (
-    keyword: string, configurations: ISearchConfigurations,
+    keyword: string, configurations: ISearchConfigurations, disableTermManager = false,
 ): SWRResponse<IFormattedSearchResult, Error> & { conditions: ISearchConditions } => {
+  const { data: termNumber } = useFullTextSearchTermManager(disableTermManager);
 
   const {
     limit, offset, sort, order, includeTrashPages, includeUserPages,
@@ -62,7 +70,7 @@ export const useSWRxFullTextSearch = (
   const rawQuery = createSearchQuery(keyword, fixedConfigurations.includeTrashPages, fixedConfigurations.includeUserPages);
 
   const swrResult = useSWRImmutable(
-    ['/search', keyword, fixedConfigurations],
+    ['/search', keyword, fixedConfigurations, termNumber],
     (endpoint, keyword, fixedConfigurations) => {
       const {
         limit, offset, sort, order,

+ 19 - 0
packages/app/src/stores/use-static-swr.tsx

@@ -29,3 +29,22 @@ export function useStaticSWR<Data, Error>(
 
   return swrResponse;
 }
+
+
+export type ITermNumberManagerUtil = {
+  advance(): void,
+}
+
+export const useTermNumberManager = (key: Key) : SWRResponse<number, Error> & ITermNumberManagerUtil => {
+  const swrResult = useStaticSWR<number, Error>(key, undefined, { fallbackData: 0 });
+
+  return {
+    ...swrResult,
+    advance: () => {
+      const { data: currentNum } = swrResult;
+      if (currentNum != null) {
+        swrResult.mutate(currentNum + 1);
+      }
+    },
+  };
+};