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

Refactor thread relation types: unify pagination type and clean up imports

Shun Miyazawa 10 месяцев назад
Родитель
Сommit
e17993f22a

+ 5 - 12
apps/app/src/features/openai/client/stores/thread.tsx

@@ -1,17 +1,11 @@
-import type { PaginateResult } from 'mongoose';
 import { type SWRResponse, type SWRConfiguration } from 'swr';
 import useSWRImmutable from 'swr/immutable';
-import useSWRInfinite from 'swr/infinite'; // eslint-disable-line camelcase
+import useSWRInfinite from 'swr/infinite';
 import type { SWRInfiniteResponse } from 'swr/infinite';
 import useSWRMutation, { type SWRMutationResponse } from 'swr/mutation';
 
 import { apiv3Get } from '~/client/util/apiv3-client';
-import type { IThreadRelationHasId } from '~/features/openai/interfaces/thread-relation';
-
-
-type RecentThreadsApiResult = {
-  paginateResult: PaginateResult<IThreadRelationHasId>;
-};
+import type { IThreadRelationHasId, IThreadRelationPaginate } from '~/features/openai/interfaces/thread-relation';
 
 const getKey = (aiAssistantId?: string) => (aiAssistantId != null ? [`/openai/threads/${aiAssistantId}`] : null);
 
@@ -37,7 +31,7 @@ export const useSWRMUTxThreads = (aiAssistantId?: string): SWRMutationResponse<I
 // Helper function to generate key for pagination
 const getRecentThreadsKey = (
     pageIndex: number,
-    previousPageData: RecentThreadsApiResult | null,
+    previousPageData: IThreadRelationPaginate | null,
 ): [string, number, number] | null => {
   // If no more data, return null to stop fetching
   if (previousPageData && !previousPageData.paginateResult.hasNextPage) {
@@ -52,13 +46,12 @@ const getRecentThreadsKey = (
 
 
 export const useSWRINFxRecentThreads = (
-    includeWipPage?: boolean,
     config?: SWRConfiguration,
-): SWRInfiniteResponse<RecentThreadsApiResult, Error> => {
+): SWRInfiniteResponse<IThreadRelationPaginate, Error> => {
   const PER_PAGE = 20;
   return useSWRInfinite(
     (pageIndex, previousPageData) => getRecentThreadsKey(pageIndex, previousPageData),
-    ([endpoint, page, limit]) => apiv3Get<RecentThreadsApiResult>(endpoint, { page, limit }).then(response => response.data),
+    ([endpoint, page, limit]) => apiv3Get<IThreadRelationPaginate>(endpoint, { page, limit }).then(response => response.data),
     {
       ...config,
       revalidateFirstPage: false,

+ 5 - 0
apps/app/src/features/openai/interfaces/thread-relation.ts

@@ -1,4 +1,5 @@
 import type { IUser, Ref, HasObjectId } from '@growi/core';
+import type { PaginateResult } from 'mongoose';
 
 import type { AiAssistant } from './ai-assistant';
 
@@ -21,6 +22,10 @@ export interface IThreadRelation {
 
 export type IThreadRelationHasId = IThreadRelation & HasObjectId;
 
+export type IThreadRelationPaginate = {
+  paginateResult: PaginateResult<IThreadRelationHasId>;
+};
+
 export type IApiv3DeleteThreadParams = {
   aiAssistantId: string
   threadRelationId: string;