2
0
Эх сурвалжийг харах

Refactor ThreadList and thread-relation types for improved clarity and functionality

Shun Miyazawa 9 сар өмнө
parent
commit
74afa449d9

+ 2 - 10
apps/app/src/features/openai/client/components/AiAssistant/Sidebar/ThreadList.tsx

@@ -1,12 +1,11 @@
 import React, { useCallback } from 'react';
 
-import { getIdStringForRef, isPopulated } from '@growi/core';
+import { getIdStringForRef } from '@growi/core';
 import { useTranslation } from 'react-i18next';
 
 import InfiniteScroll from '~/client/components/InfiniteScroll';
 import { toastError, toastSuccess } from '~/client/util/toastr';
 import { useSWRINFxRecentThreads } from '~/features/openai/client/stores/thread';
-import type { IThreadRelationHasId } from '~/features/openai/interfaces/thread-relation';
 import loggerFactory from '~/utils/logger';
 
 import { deleteThread } from '../../../services/thread';
@@ -23,13 +22,6 @@ export const ThreadList: React.FC = () => {
   const isEmpty = data?.[0]?.paginateResult.totalDocs === 0;
   const isReachingEnd = isEmpty || (data != null && (data[data.length - 1].paginateResult.hasNextPage === false));
 
-  const openChatHandler = useCallback((threadData: IThreadRelationHasId) => {
-    const aiAssistant = threadData.aiAssistant;
-    if (isPopulated(aiAssistant)) {
-      openChat({ ...aiAssistant, _id: getIdStringForRef(aiAssistant._id) }, threadData);
-    }
-  }, [openChat]);
-
   const deleteThreadHandler = useCallback(async(aiAssistantId: string, threadRelationId: string) => {
     try {
       await deleteThread({ aiAssistantId, threadRelationId });
@@ -54,7 +46,7 @@ export const ThreadList: React.FC = () => {
                 className="list-group-item list-group-item-action border-0 d-flex align-items-center rounded-1"
                 onClick={(e) => {
                   e.stopPropagation();
-                  openChatHandler(thread);
+                  openChat(thread.aiAssistant, thread);
                 }}
               >
                 <div>

+ 4 - 2
apps/app/src/features/openai/interfaces/thread-relation.ts

@@ -1,7 +1,7 @@
 import type { IUser, Ref, HasObjectId } from '@growi/core';
 import type { PaginateResult } from 'mongoose';
 
-import type { AiAssistant } from './ai-assistant';
+import type { AiAssistant, AiAssistantHasId } from './ai-assistant';
 
 
 export const ThreadType = {
@@ -23,8 +23,10 @@ export interface IThreadRelation {
 
 export type IThreadRelationHasId = IThreadRelation & HasObjectId;
 
+export type IThreadRelationPopulated = Omit<IThreadRelationHasId, 'aiAssistant'> & { aiAssistant: AiAssistantHasId }
+
 export type IThreadRelationPaginate = {
-  paginateResult: PaginateResult<IThreadRelationHasId>;
+  paginateResult: PaginateResult<IThreadRelationPopulated>;
 };
 
 export type IApiv3DeleteThreadParams = {