|
|
@@ -1,13 +1,15 @@
|
|
|
import { useCallback } from 'react';
|
|
|
|
|
|
import { useSWRStatic } from '@growi/core/dist/swr';
|
|
|
-import { type SWRResponse } from 'swr';
|
|
|
+import { type SWRResponse, mutate, useSWRConfig } from 'swr';
|
|
|
import useSWRImmutable from 'swr/immutable';
|
|
|
|
|
|
import { apiv3Get } from '~/client/util/apiv3-client';
|
|
|
|
|
|
import { type AccessibleAiAssistantsHasId, type AiAssistantHasId } from '../../interfaces/ai-assistant';
|
|
|
-import type { IThreadRelationHasId } from '../../interfaces/thread-relation';
|
|
|
+import type { IThreadRelationHasId } from '../../interfaces/thread-relation'; // IThreadHasId を削除
|
|
|
+
|
|
|
+import { useSWRxThreads } from './thread';
|
|
|
|
|
|
export const AiAssistantManagementModalPageMode = {
|
|
|
HOME: 'home',
|
|
|
@@ -72,6 +74,7 @@ type AiAssistantSidebarUtils = {
|
|
|
): void
|
|
|
openEditor(): void
|
|
|
close(): void
|
|
|
+ refreshCurrentThreadData(): Promise<void>
|
|
|
}
|
|
|
|
|
|
export const useAiAssistantSidebar = (
|
|
|
@@ -79,11 +82,37 @@ export const useAiAssistantSidebar = (
|
|
|
): SWRResponse<AiAssistantSidebarStatus, Error> & AiAssistantSidebarUtils => {
|
|
|
const initialStatus = { isOpened: false };
|
|
|
const swrResponse = useSWRStatic<AiAssistantSidebarStatus, Error>('AiAssistantSidebar', status, { fallbackData: initialStatus });
|
|
|
+ const { cache } = useSWRConfig();
|
|
|
+
|
|
|
+ const refreshCurrentThreadData = useCallback(async() => {
|
|
|
+ const { aiAssistantData, threadData } = swrResponse.data ?? {};
|
|
|
+
|
|
|
+ if (aiAssistantData?._id == null || threadData?._id == null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ const threadsCacheKey = ['threads', aiAssistantData._id];
|
|
|
+ await mutate(threadsCacheKey);
|
|
|
+
|
|
|
+ // useSWRxThreads を直接呼び出す代わりに cache を使用
|
|
|
+ // cache.get のキーは文字列である必要があるため、配列を結合
|
|
|
+ const threadsData = cache.get(threadsCacheKey.join(','))?.data as IThreadRelationHasId[] | undefined; // IThread を IThreadRelationHasId に変更
|
|
|
+
|
|
|
+ if (threadsData == null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ const newThreadDataFromServer = threadsData.find(t => t._id === threadData._id);
|
|
|
+
|
|
|
+ if (newThreadDataFromServer != null && swrResponse.data != null) { // swrResponse.data の存在を確認
|
|
|
+ swrResponse.mutate({ ...swrResponse.data, threadData: newThreadDataFromServer });
|
|
|
+ }
|
|
|
+ }, [swrResponse, cache]);
|
|
|
|
|
|
return {
|
|
|
...swrResponse,
|
|
|
openChat: useCallback(
|
|
|
- (aiAssistantData: AiAssistantHasId, threadData: IThreadRelationHasId) => {
|
|
|
+ (aiAssistantData: AiAssistantHasId, threadData?: IThreadRelationHasId) => {
|
|
|
swrResponse.mutate({ isOpened: true, aiAssistantData, threadData });
|
|
|
}, [swrResponse],
|
|
|
),
|
|
|
@@ -99,5 +128,6 @@ export const useAiAssistantSidebar = (
|
|
|
isOpened: false, isEditorAssistant: false, aiAssistantData: undefined, threadData: undefined,
|
|
|
}), [swrResponse],
|
|
|
),
|
|
|
+ refreshCurrentThreadData,
|
|
|
};
|
|
|
};
|