Răsfoiți Sursa

Show thread list

Shun Miyazawa 1 an în urmă
părinte
comite
07daf0b695

+ 45 - 29
apps/app/src/features/openai/client/components/AiAssistant/Sidebar/AiAssistantTree.tsx

@@ -3,35 +3,27 @@ import React, { useCallback, useState } from 'react';
 import { getIdStringForRef } from '@growi/core';
 import { getIdStringForRef } from '@growi/core';
 
 
 import { toastError, toastSuccess } from '~/client/util/toastr';
 import { toastError, toastSuccess } from '~/client/util/toastr';
+import type { IThreadRelationHasId } from '~/features/openai/interfaces/thread-relation';
 import { useCurrentUser } from '~/stores-universal/context';
 import { useCurrentUser } from '~/stores-universal/context';
 
 
 import type { AiAssistantAccessScope } from '../../../../interfaces/ai-assistant';
 import type { AiAssistantAccessScope } from '../../../../interfaces/ai-assistant';
 import { AiAssistantShareScope, type AiAssistantHasId } from '../../../../interfaces/ai-assistant';
 import { AiAssistantShareScope, type AiAssistantHasId } from '../../../../interfaces/ai-assistant';
 import { deleteAiAssistant } from '../../../services/ai-assistant';
 import { deleteAiAssistant } from '../../../services/ai-assistant';
 import { useAiAssistantChatSidebar, useAiAssistantManagementModal } from '../../../stores/ai-assistant';
 import { useAiAssistantChatSidebar, useAiAssistantManagementModal } from '../../../stores/ai-assistant';
+import { useSWRxThreads } from '../../../stores/thread';
 
 
 import styles from './AiAssistantTree.module.scss';
 import styles from './AiAssistantTree.module.scss';
 
 
 const moduleClass = styles['ai-assistant-tree-item'] ?? '';
 const moduleClass = styles['ai-assistant-tree-item'] ?? '';
 
 
-type Thread = {
-  _id: string;
-  name: string;
-}
-
-const dummyThreads: Thread[] = [
-  { _id: '1', name: 'thread1' },
-  { _id: '2', name: 'thread2' },
-  { _id: '3', name: 'thread3' },
-];
-
+/*
+*  For ThreadItem
+*/
 type ThreadItemProps = {
 type ThreadItemProps = {
-  thread: Thread;
+  thread: IThreadRelationHasId
 };
 };
 
 
-const ThreadItem: React.FC<ThreadItemProps> = ({
-  thread,
-}) => {
+const ThreadItem: React.FC<ThreadItemProps> = ({ thread }) => {
 
 
   const deleteThreadHandler = useCallback(() => {
   const deleteThreadHandler = useCallback(() => {
     // TODO: https://redmine.weseek.co.jp/issues/161490
     // TODO: https://redmine.weseek.co.jp/issues/161490
@@ -52,7 +44,7 @@ const ThreadItem: React.FC<ThreadItemProps> = ({
       </div>
       </div>
 
 
       <div className="grw-ai-assistant-title-anchor ps-1">
       <div className="grw-ai-assistant-title-anchor ps-1">
-        <p className="text-truncate m-auto">{thread.name}</p>
+        <p className="text-truncate m-auto">{thread.threadId}</p>
       </div>
       </div>
 
 
       <div className="grw-ai-assistant-actions opacity-0 d-flex justify-content-center ">
       <div className="grw-ai-assistant-actions opacity-0 d-flex justify-content-center ">
@@ -69,6 +61,36 @@ const ThreadItem: React.FC<ThreadItemProps> = ({
 };
 };
 
 
 
 
+/*
+*  ThreadItems
+*/
+type ThreadItemsProps = {
+  aiAssistantId: string;
+};
+
+const ThreadItems: React.FC<ThreadItemsProps> = ({ aiAssistantId }) => {
+  const { data: threads } = useSWRxThreads(aiAssistantId);
+
+  if (threads == null || threads.length === 0) {
+    return <p className="text-secondary ms-5">スレッドが存在しません</p>;
+  }
+
+  return (
+    <div className="grw-ai-assistant-item-children">
+      {threads.map(thread => (
+        <ThreadItem
+          key={thread._id}
+          thread={thread}
+        />
+      ))}
+    </div>
+  );
+};
+
+
+/*
+*  AiAssistantItem
+*/
 const getShareScopeIcon = (shareScope: AiAssistantShareScope, accessScope: AiAssistantAccessScope): string => {
 const getShareScopeIcon = (shareScope: AiAssistantShareScope, accessScope: AiAssistantAccessScope): string => {
   const determinedSharedScope = shareScope === AiAssistantShareScope.SAME_AS_ACCESS_SCOPE ? accessScope : shareScope;
   const determinedSharedScope = shareScope === AiAssistantShareScope.SAME_AS_ACCESS_SCOPE ? accessScope : shareScope;
   switch (determinedSharedScope) {
   switch (determinedSharedScope) {
@@ -84,7 +106,6 @@ const getShareScopeIcon = (shareScope: AiAssistantShareScope, accessScope: AiAss
 type AiAssistantItemProps = {
 type AiAssistantItemProps = {
   currentUserId?: string;
   currentUserId?: string;
   aiAssistant: AiAssistantHasId;
   aiAssistant: AiAssistantHasId;
-  threads: Thread[];
   onEditClicked?: (aiAssistantData: AiAssistantHasId) => void;
   onEditClicked?: (aiAssistantData: AiAssistantHasId) => void;
   onItemClicked?: (aiAssistantData: AiAssistantHasId) => void;
   onItemClicked?: (aiAssistantData: AiAssistantHasId) => void;
   onDeleted?: () => void;
   onDeleted?: () => void;
@@ -93,7 +114,6 @@ type AiAssistantItemProps = {
 const AiAssistantItem: React.FC<AiAssistantItemProps> = ({
 const AiAssistantItem: React.FC<AiAssistantItemProps> = ({
   currentUserId,
   currentUserId,
   aiAssistant,
   aiAssistant,
-  threads,
   onEditClicked,
   onEditClicked,
   onItemClicked,
   onItemClicked,
   onDeleted,
   onDeleted,
@@ -185,20 +205,17 @@ const AiAssistantItem: React.FC<AiAssistantItemProps> = ({
         )}
         )}
       </li>
       </li>
 
 
-      {isThreadsOpened && threads.length > 0 && (
-        <div className="grw-ai-assistant-item-children">
-          {threads.map(thread => (
-            <ThreadItem
-              key={thread._id}
-              thread={thread}
-            />
-          ))}
-        </div>
-      )}
+      { isThreadsOpened && (
+        <ThreadItems aiAssistantId={aiAssistant._id} />
+      ) }
     </>
     </>
   );
   );
 };
 };
 
 
+
+/*
+*  AiAssistantTree
+*/
 type AiAssistantTreeProps = {
 type AiAssistantTreeProps = {
   aiAssistants: AiAssistantHasId[];
   aiAssistants: AiAssistantHasId[];
   onDeleted?: () => void;
   onDeleted?: () => void;
@@ -216,7 +233,6 @@ export const AiAssistantTree: React.FC<AiAssistantTreeProps> = ({ aiAssistants,
           key={assistant._id}
           key={assistant._id}
           currentUserId={currentUser?._id}
           currentUserId={currentUser?._id}
           aiAssistant={assistant}
           aiAssistant={assistant}
-          threads={dummyThreads}
           onEditClicked={openAiAssistantManagementModal}
           onEditClicked={openAiAssistantManagementModal}
           onItemClicked={openAiAssistantChatSidebar}
           onItemClicked={openAiAssistantChatSidebar}
           onDeleted={onDeleted}
           onDeleted={onDeleted}