Explorar o código

Calculate on client side when creating new assistant

Shun Miyazawa hai 1 ano
pai
achega
ad3db855ca

+ 3 - 1
apps/app/src/features/openai/client/components/AiAssistant/AiAssistantManagementModal/AiAssistantManagementHome.tsx

@@ -18,6 +18,7 @@ type Props = {
   description: string;
   description: string;
   instruction: string;
   instruction: string;
   shareScope: AiAssistantShareScope
   shareScope: AiAssistantShareScope
+  totalSelectedPageCount: number;
   onNameChange: (value: string) => void;
   onNameChange: (value: string) => void;
   onDescriptionChange: (value: string) => void;
   onDescriptionChange: (value: string) => void;
   onCreateAiAssistant: () => Promise<void>
   onCreateAiAssistant: () => Promise<void>
@@ -30,6 +31,7 @@ export const AiAssistantManagementHome = (props: Props): JSX.Element => {
     description,
     description,
     instruction,
     instruction,
     shareScope,
     shareScope,
+    totalSelectedPageCount,
     onNameChange,
     onNameChange,
     onDescriptionChange,
     onDescriptionChange,
     onCreateAiAssistant,
     onCreateAiAssistant,
@@ -116,7 +118,7 @@ export const AiAssistantManagementHome = (props: Props): JSX.Element => {
             >
             >
               <span className="fw-normal">{t('modal_ai_assistant.page_mode_title.pages')}</span>
               <span className="fw-normal">{t('modal_ai_assistant.page_mode_title.pages')}</span>
               <div className="d-flex align-items-center text-secondary">
               <div className="d-flex align-items-center text-secondary">
-                <span>3ページ</span>
+                <span>{`${totalSelectedPageCount} ページ`}</span>
                 <span className="material-symbols-outlined ms-2 align-middle">chevron_right</span>
                 <span className="material-symbols-outlined ms-2 align-middle">chevron_right</span>
               </div>
               </div>
             </button>
             </button>

+ 14 - 1
apps/app/src/features/openai/client/components/AiAssistant/AiAssistantManagementModal/AiAssistantManagementModal.tsx

@@ -1,4 +1,6 @@
-import React, { useCallback, useState, useEffect } from 'react';
+import React, {
+  useCallback, useState, useEffect, useMemo,
+} from 'react';
 
 
 import { type IGrantedGroup, isPopulated } from '@growi/core';
 import { type IGrantedGroup, isPopulated } from '@growi/core';
 import { useTranslation } from 'react-i18next';
 import { useTranslation } from 'react-i18next';
@@ -71,6 +73,16 @@ 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'));
 
 
+  const totalSelectedPageCount = useMemo(() => {
+    return selectedPages.reduce((prev, current) => {
+      const descendantCount = current.isIncludeSubPage
+        ? current.page.descendantCount ?? 0
+        : 0;
+      const pageCountWithDescendants = descendantCount + 1;
+      return prev + pageCountWithDescendants;
+    }, 0);
+  }, [selectedPages]);
+
   // Effects
   // Effects
   useEffect(() => {
   useEffect(() => {
     if (shouldEdit) {
     if (shouldEdit) {
@@ -213,6 +225,7 @@ const AiAssistantManagementModalSubstance = (): JSX.Element => {
             description={description}
             description={description}
             shareScope={selectedShareScope}
             shareScope={selectedShareScope}
             instruction={instruction}
             instruction={instruction}
+            totalSelectedPageCount={totalSelectedPageCount}
             onNameChange={changeNameHandler}
             onNameChange={changeNameHandler}
             onDescriptionChange={changeDescriptionHandler}
             onDescriptionChange={changeDescriptionHandler}
             onCreateAiAssistant={createAiAssistantHandler}
             onCreateAiAssistant={createAiAssistantHandler}