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+ const [ exampleChoice , setExampleChoice ] = useState < number > ( 0 ) ; // 0〜1
37+
1338 const handleSubmit = async ( e : FormEvent < HTMLFormElement > ) => {
1439 e . preventDefault ( ) ;
1540 setIsLoading ( true ) ;
1641 setResponse ( "" ) ;
1742
43+ let userQuestion = inputValue ;
44+ if ( ! userQuestion && exampleData ) {
45+ // 質問が空欄なら、質問例を使用
46+ userQuestion = exampleData [ Math . floor ( exampleChoice * exampleData . length ) ] ;
47+ setInputValue ( userQuestion ) ;
48+ }
49+
1850 const result = await askAI ( {
19- userQuestion : inputValue ,
51+ userQuestion,
2052 documentContent : documentContent ,
2153 } ) ;
2254
@@ -35,8 +67,18 @@ export function ChatForm({ documentContent }: { documentContent: string }) {
3567 < div className = "input-area" >
3668 < textarea
3769 className = "textarea textarea-ghost textarea-md rounded-lg"
38- placeholder = "質問を入力してください"
39- style = { { width : "100%" , height : "110px" , resize : "none" , outlineStyle : "none" } }
70+ placeholder = {
71+ "質問を入力してください" +
72+ ( exampleData
73+ ? ` (例:「${ exampleData [ Math . floor ( exampleChoice * exampleData . length ) ] } 」)`
74+ : "" )
75+ }
76+ style = { {
77+ width : "100%" ,
78+ height : "110px" ,
79+ resize : "none" ,
80+ outlineStyle : "none" ,
81+ } }
4082 value = { inputValue }
4183 onChange = { ( e ) => setInputValue ( e . target . value ) }
4284 disabled = { isLoading }
@@ -68,7 +110,10 @@ export function ChatForm({ documentContent }: { documentContent: string }) {
68110 { ! isFormVisible && (
69111 < button
70112 className = "btn btn-soft btn-secondary rounded-full"
71- onClick = { ( ) => setIsFormVisible ( true ) }
113+ onClick = { ( ) => {
114+ setIsFormVisible ( true ) ;
115+ setExampleChoice ( Math . random ( ) ) ;
116+ } }
72117 >
73118 チャットを開く
74119 </ button >
0 commit comments