Browse Source

Refactor AiAssistantManagementHome to handle name and description changes with new props

Shun Miyazawa 1 year ago
parent
commit
fff81f4aeb

+ 19 - 2
apps/app/src/features/openai/client/components/AiAssistant/AiAssistantManagementModal/AiAssistantManagementHome.tsx

@@ -11,12 +11,25 @@ import { useCurrentUser } from '~/stores-universal/context';
 import { useAiAssistantManagementModal, AiAssistantManagementModalPageMode } from '../../../stores/ai-assistant';
 import { useAiAssistantManagementModal, AiAssistantManagementModalPageMode } from '../../../stores/ai-assistant';
 
 
 type Props = {
 type Props = {
+  name: string;
+  description: string;
   instruction: string;
   instruction: string;
   shareScope: AiAssistantShareScope
   shareScope: AiAssistantShareScope
+  onNameChange: (value: string) => void;
+  onDescriptionChange: (value: string) => void;
+  onCreateAiAssistant: () => Promise<void>
 }
 }
 
 
 export const AiAssistantManagementHome = (props: Props): JSX.Element => {
 export const AiAssistantManagementHome = (props: Props): JSX.Element => {
-  const { instruction, shareScope } = props;
+  const {
+    name,
+    description,
+    instruction,
+    shareScope,
+    onNameChange,
+    onDescriptionChange,
+    onCreateAiAssistant,
+  } = props;
 
 
   const { t } = useTranslation();
   const { t } = useTranslation();
   const { data: currentUser } = useCurrentUser();
   const { data: currentUser } = useCurrentUser();
@@ -44,6 +57,8 @@ export const AiAssistantManagementHome = (props: Props): JSX.Element => {
               placeholder="アシスタント名を入力"
               placeholder="アシスタント名を入力"
               bsSize="lg"
               bsSize="lg"
               className="border-0 border-bottom border-2 px-0 rounded-0"
               className="border-0 border-bottom border-2 px-0 rounded-0"
+              value={name}
+              onChange={e => onNameChange(e.target.value)}
             />
             />
           </div>
           </div>
 
 
@@ -56,6 +71,8 @@ export const AiAssistantManagementHome = (props: Props): JSX.Element => {
               type="textarea"
               type="textarea"
               placeholder="内容や用途のメモを表示させることができます"
               placeholder="内容や用途のメモを表示させることができます"
               rows="4"
               rows="4"
+              value={description}
+              onChange={e => onDescriptionChange(e.target.value)}
             />
             />
             <small className="text-secondary d-block mt-2">
             <small className="text-secondary d-block mt-2">
               メモの内容はアシスタントの処理に影響しません。
               メモの内容はアシスタントの処理に影響しません。
@@ -105,7 +122,7 @@ export const AiAssistantManagementHome = (props: Props): JSX.Element => {
 
 
         <ModalFooter>
         <ModalFooter>
           <button type="button" className="btn btn-outline-secondary" onClick={() => {}}>キャンセル</button>
           <button type="button" className="btn btn-outline-secondary" onClick={() => {}}>キャンセル</button>
-          <button type="button" className="btn btn-primary" onClick={() => {}}>アシスタントを作成する</button>
+          <button type="button" className="btn btn-primary" onClick={onCreateAiAssistant}>アシスタントを作成する</button>
         </ModalFooter>
         </ModalFooter>
       </div>
       </div>
     </>
     </>

+ 26 - 7
apps/app/src/features/openai/client/components/AiAssistant/AiAssistantManagementModal/AiAssistantManagementModal.tsx

@@ -32,6 +32,8 @@ const AiAssistantManagementModalSubstance = (): JSX.Element => {
   const pageMode = aiAssistantManagementModalData?.pageMode ?? AiAssistantManagementModalPageMode.HOME;
   const pageMode = aiAssistantManagementModalData?.pageMode ?? AiAssistantManagementModalPageMode.HOME;
 
 
   // States
   // States
+  const [name, setName] = useState<string>('');
+  const [description, setDescription] = useState<string>('');
   const [selectedShareScope, setSelectedShareScope] = useState<AiAssistantShareScope>(AiAssistantShareScope.OWNER);
   const [selectedShareScope, setSelectedShareScope] = useState<AiAssistantShareScope>(AiAssistantShareScope.OWNER);
   const [selectedAccessScope, setSelectedAccessScope] = useState<AiAssistantAccessScope>(AiAssistantAccessScope.OWNER);
   const [selectedAccessScope, setSelectedAccessScope] = useState<AiAssistantAccessScope>(AiAssistantAccessScope.OWNER);
   const [selectedUserGroupsForAccessScope, setSelectedUserGroupsForAccessScope] = useState<PopulatedGrantedGroup[]>([]);
   const [selectedUserGroupsForAccessScope, setSelectedUserGroupsForAccessScope] = useState<PopulatedGrantedGroup[]>([]);
@@ -39,28 +41,40 @@ const AiAssistantManagementModalSubstance = (): JSX.Element => {
   const [selectedPages, setSelectedPages] = useState<SelectedPage[]>([]);
   const [selectedPages, setSelectedPages] = useState<SelectedPage[]>([]);
   const [instruction, setInstruction] = useState<string>(t('modal_ai_assistant.default_instruction'));
   const [instruction, setInstruction] = useState<string>(t('modal_ai_assistant.default_instruction'));
 
 
-  // Functions
-  const clickCreateAiAssistantHandler = useCallback(async() => {
+
+  /*
+  *  For AiAssistantManagementHome methods
+  */
+  const changeNameHandler = useCallback((value: string) => {
+    setName(value);
+  }, []);
+
+  const changeDescriptionHandler = useCallback((value: string) => {
+    setDescription(value);
+  }, []);
+
+  const createAiAssistantHandler = useCallback(async() => {
     try {
     try {
       const pagePathPatterns = selectedPages
       const pagePathPatterns = selectedPages
         .map(selectedPage => (selectedPage.isIncludeSubPage ? `${selectedPage.page.path}/*` : selectedPage.page.path))
         .map(selectedPage => (selectedPage.isIncludeSubPage ? `${selectedPage.page.path}/*` : selectedPage.page.path))
         .filter((path): path is string => path !== undefined && path !== null);
         .filter((path): path is string => path !== undefined && path !== null);
 
 
       await createAiAssistant({
       await createAiAssistant({
-        name: 'test',
-        description: 'test',
+        name,
+        description,
         additionalInstruction: instruction,
         additionalInstruction: instruction,
         pagePathPatterns,
         pagePathPatterns,
-        shareScope: 'publicOnly',
-        accessScope: 'publicOnly',
+        shareScope: selectedShareScope,
+        accessScope: selectedAccessScope,
       });
       });
+
       toastSuccess('アシスタントを作成しました');
       toastSuccess('アシスタントを作成しました');
     }
     }
     catch (err) {
     catch (err) {
       toastError('アシスタントの作成に失敗しました');
       toastError('アシスタントの作成に失敗しました');
       logger.error(err);
       logger.error(err);
     }
     }
-  }, [instruction, selectedPages]);
+  }, [description, instruction, name, selectedAccessScope, selectedPages, selectedShareScope]);
 
 
 
 
   /*
   /*
@@ -126,8 +140,13 @@ const AiAssistantManagementModalSubstance = (): JSX.Element => {
       <TabContent activeTab={pageMode}>
       <TabContent activeTab={pageMode}>
         <TabPane tabId={AiAssistantManagementModalPageMode.HOME}>
         <TabPane tabId={AiAssistantManagementModalPageMode.HOME}>
           <AiAssistantManagementHome
           <AiAssistantManagementHome
+            name={name}
+            description={description}
             shareScope={selectedShareScope}
             shareScope={selectedShareScope}
             instruction={instruction}
             instruction={instruction}
+            onNameChange={changeNameHandler}
+            onDescriptionChange={changeDescriptionHandler}
+            onCreateAiAssistant={createAiAssistantHandler}
           />
           />
         </TabPane>
         </TabPane>