11"use client" ;
22
3- import { useState , FormEvent , useEffect , useRef , useCallback } from "react" ;
3+ import { useState , FormEvent , useEffect , useRef , useCallback , useMemo } from "react" ;
44// import useSWR from "swr";
55// import {
66// getQuestionExample,
@@ -26,8 +26,6 @@ export function ChatForm({ path, sectionContent, close }: ChatFormProps) {
2626 const [ isLoading , setIsLoading ] = useState ( false ) ;
2727 const [ errorMessage , setErrorMessage ] = useState < string | null > ( null ) ;
2828
29- // const lang = getLanguageName(docs_id);
30-
3129 const { files, replOutputs, execResults } = useEmbedContext ( ) ;
3230
3331 const router = useRouter ( ) ;
@@ -64,42 +62,43 @@ export function ChatForm({ path, sectionContent, close }: ChatFormProps) {
6462 }
6563 } , [ pathname ] ) ;
6664
67- // const documentContentInView = sectionContent
68- // .filter((s) => s.inView)
69- // .map((s) => s.rawContent)
70- // .join("\n\n");
71- // const { data: exampleData, error: exampleError } = useSWR(
72- // // 質問フォームを開いたときだけで良い
73- // {
74- // lang,
75- // documentContent: documentContentInView,
76- // } satisfies QuestionExampleParams,
77- // getQuestionExample,
78- // {
79- // // リクエストは古くても構わないので1回でいい
80- // revalidateIfStale: false,
81- // revalidateOnFocus: false,
82- // revalidateOnReconnect: false,
83- // }
84- // );
85- // if (exampleError) {
86- // console.error("Error getting question example:", exampleError);
87- // }
65+ const exampleData = useMemo (
66+ ( ) =>
67+ sectionContent
68+ . filter ( ( s ) => s . inView )
69+ . map ( ( s ) => s . question )
70+ . filter ( ( qe ) => qe !== undefined )
71+ . flat ( ) ,
72+ // eslint-disable-next-line react-hooks/exhaustive-deps
73+ [ ]
74+ ) ;
8875 // 質問フォームを開くたびにランダムに選び直し、
8976 // exampleData[Math.floor(exampleChoice * exampleData.length)] を採用する
90- const [ exampleChoice , setExampleChoice ] = useState < number > ( 0 ) ; // 0〜1
77+ const [ exampleChoice , setExampleChoice ] = useState < number | undefined > (
78+ undefined
79+ ) ; // 0〜1
9180 useEffect ( ( ) => {
92- if ( exampleChoice === 0 ) {
81+ if ( exampleChoice === undefined ) {
9382 setExampleChoice ( Math . random ( ) ) ;
9483 }
9584 } , [ exampleChoice ] ) ;
9685
9786 const handleSubmit = async ( e : FormEvent < HTMLFormElement > ) => {
87+
88+ let userQuestion = inputValue ;
89+ if ( ! userQuestion && exampleData . length > 0 && exampleChoice ) {
90+ // 質問が空欄なら、質問例を使用
91+ userQuestion =
92+ exampleData [ Math . floor ( exampleChoice * exampleData . length ) ] ;
93+ setInputValue ( userQuestion ) ;
94+ }
95+ if ( ! userQuestion ) {
96+ return ;
97+ }
98+
9899 e . preventDefault ( ) ;
99100 setIsLoading ( true ) ;
100- setErrorMessage ( null ) ;
101-
102- const userQuestion = inputValue ;
101+ setErrorMessage ( null ) ; // Clear previous error message
103102
104103 let response : Response ;
105104 try {
@@ -214,10 +213,10 @@ export function ChatForm({ path, sectionContent, close }: ChatFormProps) {
214213 < textarea
215214 className = "textarea textarea-ghost textarea-md rounded-box bg-transparent!"
216215 placeholder = {
217- "質問を入力してください" /* +
218- (exampleData
216+ "質問を入力してください" +
217+ ( exampleData . length > 0 && exampleChoice !== undefined
219218 ? ` (例:「${ exampleData [ Math . floor ( exampleChoice * exampleData . length ) ] } 」)`
220- : "")*/
219+ : "" )
221220 }
222221 style = { {
223222 width : "100%" ,
0 commit comments