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" ;
68
7- export function ChatForm ( { documentContent } : { documentContent : string } ) {
9+ export function ChatForm ( {
10+ docs_id,
11+ documentContent,
12+ } : {
13+ docs_id : string ;
14+ documentContent : string ;
15+ } ) {
816 const [ inputValue , setInputValue ] = useState ( "" ) ;
917 const [ response , setResponse ] = useState ( "" ) ;
1018 const [ isLoading , setIsLoading ] = useState ( false ) ;
1119 const [ isFormVisible , setIsFormVisible ] = useState ( false ) ;
1220
21+ const lang = docs_id . split ( "-" ) [ 0 ] ;
22+ const { data : exampleData , error : exampleError } = useSWR (
23+ // 質問フォームを開いたときだけで良い
24+ isFormVisible ? { lang, documentContent } : null ,
25+ getQuestionExample ,
26+ {
27+ // リクエストは古くても構わないので1回でいい
28+ revalidateIfStale : false ,
29+ revalidateOnFocus : false ,
30+ revalidateOnReconnect : false ,
31+ }
32+ ) ;
33+ if ( exampleError ) {
34+ console . error ( "Error getting question example:" , exampleError ) ;
35+ }
36+ // 質問フォームを開くたびにランダムに選び直し、
37+ // exampleData[Math.floor(exampleChoice * exampleData.length)] を採用する
38+ const [ exampleChoice , setExampleChoice ] = useState < number > ( 0 ) ; // 0〜1
39+
1340 const handleSubmit = async ( e : FormEvent < HTMLFormElement > ) => {
1441 e . preventDefault ( ) ;
1542 setIsLoading ( true ) ;
1643 setResponse ( "" ) ;
1744
45+ let userQuestion = inputValue ;
46+ if ( ! userQuestion && exampleData ) {
47+ // 質問が空欄なら、質問例を使用
48+ userQuestion = exampleData [ Math . floor ( exampleChoice * exampleData . length ) ] ;
49+ setInputValue ( userQuestion ) ;
50+ }
51+
1852 const result = await askAI ( {
19- userQuestion : inputValue ,
53+ userQuestion,
2054 documentContent : documentContent ,
2155 } ) ;
2256
@@ -35,8 +69,18 @@ export function ChatForm({ documentContent }: { documentContent: string }) {
3569 < div className = "input-area" >
3670 < textarea
3771 className = "textarea textarea-ghost textarea-md rounded-lg"
38- placeholder = "質問を入力してください"
39- style = { { width : "100%" , height : "110px" , resize : "none" , outlineStyle : "none" } }
72+ placeholder = {
73+ "質問を入力してください" +
74+ ( exampleData
75+ ? ` (例:「${ exampleData [ Math . floor ( exampleChoice * exampleData . length ) ] } 」)`
76+ : "" )
77+ }
78+ style = { {
79+ width : "100%" ,
80+ height : "110px" ,
81+ resize : "none" ,
82+ outlineStyle : "none" ,
83+ } }
4084 value = { inputValue }
4185 onChange = { ( e ) => setInputValue ( e . target . value ) }
4286 disabled = { isLoading }
@@ -68,7 +112,10 @@ export function ChatForm({ documentContent }: { documentContent: string }) {
68112 { ! isFormVisible && (
69113 < button
70114 className = "btn btn-soft btn-secondary rounded-full"
71- onClick = { ( ) => setIsFormVisible ( true ) }
115+ onClick = { ( ) => {
116+ setIsFormVisible ( true ) ;
117+ setExampleChoice ( Math . random ( ) ) ;
118+ } }
72119 >
73120 チャットを開く
74121 </ button >
0 commit comments