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

Enhance AiAssistant components to pass aiAssistantId on deletion and close sidebar if necessary

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

+ 3 - 3
apps/app/src/features/openai/client/components/AiAssistant/Sidebar/AiAssistantList.tsx

@@ -27,7 +27,7 @@ type AiAssistantItemProps = {
   onEditClick: (aiAssistantData: AiAssistantHasId) => void;
   onItemClick: (aiAssistantData: AiAssistantHasId, threadData?: IThreadRelationHasId) => void;
   onUpdated?: () => void;
-  onDeleted?: () => void;
+  onDeleted?: (aiAssistantId: string) => void;
 };
 
 const AiAssistantItem: React.FC<AiAssistantItemProps> = ({
@@ -65,7 +65,7 @@ const AiAssistantItem: React.FC<AiAssistantItemProps> = ({
   const deleteAiAssistantHandler = useCallback(async() => {
     try {
       await deleteAiAssistant(aiAssistant._id);
-      onDeleted?.();
+      onDeleted?.(aiAssistant._id);
       toastSuccess(t('ai_assistant_substance.toaster.ai_assistant_deleted_success'));
     }
     catch (err) {
@@ -147,7 +147,7 @@ type AiAssistantListProps = {
   isTeamAssistant?: boolean;
   aiAssistants: AiAssistantHasId[];
   onUpdated?: () => void;
-  onDeleted?: () => void;
+  onDeleted?: (aiAssistantId: string) => void;
   onCollapsed?: () => void;
 };
 

+ 9 - 3
apps/app/src/features/openai/client/components/AiAssistant/Sidebar/AiAssistantSubstance.tsx

@@ -2,7 +2,7 @@ import React, { type JSX, useCallback } from 'react';
 
 import { useTranslation } from 'react-i18next';
 
-import { useAiAssistantManagementModal, useSWRxAiAssistants } from '../../../stores/ai-assistant';
+import { useAiAssistantManagementModal, useSWRxAiAssistants, useAiAssistantSidebar } from '../../../stores/ai-assistant';
 import { useSWRINFxRecentThreads } from '../../../stores/thread';
 
 import { AiAssistantList } from './AiAssistantList';
@@ -15,13 +15,19 @@ const moduleClass = styles['grw-ai-assistant-substance'] ?? '';
 export const AiAssistantContent = (): JSX.Element => {
   const { t } = useTranslation();
   const { open } = useAiAssistantManagementModal();
+  const { data: aiAssistantSidebarData, close: closeAiAssistantSidebar } = useAiAssistantSidebar();
   const { mutate: mutateRecentThreads } = useSWRINFxRecentThreads();
   const { data: aiAssistants, mutate: mutateAiAssistants } = useSWRxAiAssistants();
 
-  const deleteAiAssistantHandler = useCallback(async() => {
+  const deleteAiAssistantHandler = useCallback(async(aiAssistantId: string) => {
     await mutateAiAssistants();
     await mutateRecentThreads();
-  }, [mutateAiAssistants, mutateRecentThreads]);
+
+    // If the sidebar is opened for the assistant being deleted, close it
+    if (aiAssistantSidebarData?.isOpened && aiAssistantSidebarData?.aiAssistantData?._id === aiAssistantId) {
+      closeAiAssistantSidebar();
+    }
+  }, [aiAssistantSidebarData?.aiAssistantData?._id, aiAssistantSidebarData?.isOpened, closeAiAssistantSidebar, mutateAiAssistants, mutateRecentThreads]);
 
   return (
     <div className={moduleClass}>

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

@@ -17,7 +17,7 @@ export const ThreadList: React.FC = () => {
   const swrInifiniteThreads = useSWRINFxRecentThreads();
   const { t } = useTranslation();
   const { data, mutate: mutateRecentThreads } = swrInifiniteThreads;
-  const { openChat, data: aiAssistantSidebarData } = useAiAssistantSidebar();
+  const { openChat, data: aiAssistantSidebarData, close: closeAiAssistantSidebar } = useAiAssistantSidebar();
   const { trigger: mutateAssistantThreadData } = useSWRMUTxThreads(aiAssistantSidebarData?.aiAssistantData?._id);
 
   const isEmpty = data?.[0]?.paginateResult.totalDocs === 0;
@@ -30,12 +30,24 @@ export const ThreadList: React.FC = () => {
 
       mutateAssistantThreadData();
       mutateRecentThreads();
+
+      // If the sidebar is opened for the assistant being deleted, close it
+      if (aiAssistantSidebarData?.isOpened && aiAssistantSidebarData?.aiAssistantData?._id === aiAssistantId) {
+        closeAiAssistantSidebar();
+      }
     }
     catch (err) {
       logger.error(err);
       toastError(t('ai_assistant_substance.toaster.thread_deleted_failed'));
     }
-  }, [mutateAssistantThreadData, mutateRecentThreads, t]);
+  }, [
+    aiAssistantSidebarData?.aiAssistantData?._id,
+    aiAssistantSidebarData?.isOpened,
+    closeAiAssistantSidebar,
+    mutateAssistantThreadData,
+    mutateRecentThreads,
+    t,
+  ]);
 
   return (
     <>