33import { useState , FormEvent } from "react" ;
44import { askAI } from "@/app/actions/chatActions" ;
55import { StyledMarkdown } from "./markdown" ;
6+ import useSWR from "swr" ;
7+ import { getQuestionExample } from "../actions/questionExample" ;
8+ import { getLanguageName } from "../pagesList" ;
69
7- export function ChatForm ( { documentContent } : { documentContent : string } ) {
10+ export function ChatForm ( {
11+ docs_id,
12+ documentContent,
13+ } : {
14+ docs_id : string ;
15+ documentContent : string ;
16+ } ) {
817 const [ inputValue , setInputValue ] = useState ( "" ) ;
918 const [ response , setResponse ] = useState ( "" ) ;
1019 const [ isLoading , setIsLoading ] = useState ( false ) ;
1120 const [ isFormVisible , setIsFormVisible ] = useState ( false ) ;
1221
22+ const lang = getLanguageName ( docs_id ) ;
23+ const { data : exampleData , error : exampleError } = useSWR (
24+ // 質問フォームを開いたときだけで良い
25+ isFormVisible ? { lang, documentContent } : null ,
26+ getQuestionExample ,
27+ {
28+ // リクエストは古くても構わないので1回でいい
29+ revalidateIfStale : false ,
30+ revalidateOnFocus : false ,
31+ revalidateOnReconnect : false ,
32+ }
33+ ) ;
34+ if ( exampleError ) {
35+ console . error ( "Error getting question example:" , exampleError ) ;
36+ }
37+ // 質問フォームを開くたびにランダムに選び直し、
38+ // exampleData[Math.floor(exampleChoice * exampleData.length)] を採用する
39+ const [ exampleChoice , setExampleChoice ] = useState < number > ( 0 ) ; // 0〜1
40+
1341 const handleSubmit = async ( e : FormEvent < HTMLFormElement > ) => {
1442 e . preventDefault ( ) ;
1543 setIsLoading ( true ) ;
1644 setResponse ( "" ) ;
1745
46+ let userQuestion = inputValue ;
47+ if ( ! userQuestion && exampleData ) {
48+ // 質問が空欄なら、質問例を使用
49+ userQuestion = exampleData [ Math . floor ( exampleChoice * exampleData . length ) ] ;
50+ setInputValue ( userQuestion ) ;
51+ }
52+
1853 const result = await askAI ( {
19- userQuestion : inputValue ,
54+ userQuestion,
2055 documentContent : documentContent ,
2156 } ) ;
2257
@@ -35,8 +70,18 @@ export function ChatForm({ documentContent }: { documentContent: string }) {
3570 < div className = "input-area" >
3671 < textarea
3772 className = "textarea textarea-ghost textarea-md rounded-lg"
38- placeholder = "質問を入力してください"
39- style = { { width : "100%" , height : "110px" , resize : "none" , outlineStyle : "none" } }
73+ placeholder = {
74+ "質問を入力してください" +
75+ ( exampleData
76+ ? ` (例:「${ exampleData [ Math . floor ( exampleChoice * exampleData . length ) ] } 」)`
77+ : "" )
78+ }
79+ style = { {
80+ width : "100%" ,
81+ height : "110px" ,
82+ resize : "none" ,
83+ outlineStyle : "none" ,
84+ } }
4085 value = { inputValue }
4186 onChange = { ( e ) => setInputValue ( e . target . value ) }
4287 disabled = { isLoading }
@@ -68,7 +113,10 @@ export function ChatForm({ documentContent }: { documentContent: string }) {
68113 { ! isFormVisible && (
69114 < button
70115 className = "btn btn-soft btn-secondary rounded-full"
71- onClick = { ( ) => setIsFormVisible ( true ) }
116+ onClick = { ( ) => {
117+ setIsFormVisible ( true ) ;
118+ setExampleChoice ( Math . random ( ) ) ;
119+ } }
72120 >
73121 チャットを開く
74122 </ button >
0 commit comments