Skip to content

Commit db84d65

Browse files
committed
質問例表示機能を再実装
1 parent ff2adee commit db84d65

File tree

1 file changed

+43
-58
lines changed

1 file changed

+43
-58
lines changed

app/[lang]/[pageId]/chatForm.tsx

Lines changed: 43 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -27,75 +27,60 @@ export function ChatForm({ path, sectionContent, close }: ChatFormProps) {
2727

2828
const { addChat } = useChatHistoryContext();
2929

30-
// const lang = getLanguageName(docs_id);
31-
3230
const { files, replOutputs, execResults } = useEmbedContext();
3331

34-
// const documentContentInView = sectionContent
35-
// .filter((s) => s.inView)
36-
// .map((s) => s.rawContent)
37-
// .join("\n\n");
38-
// const { data: exampleData, error: exampleError } = useSWR(
39-
// // 質問フォームを開いたときだけで良い
40-
// {
41-
// lang,
42-
// documentContent: documentContentInView,
43-
// } satisfies QuestionExampleParams,
44-
// getQuestionExample,
45-
// {
46-
// // リクエストは古くても構わないので1回でいい
47-
// revalidateIfStale: false,
48-
// revalidateOnFocus: false,
49-
// revalidateOnReconnect: false,
50-
// }
51-
// );
52-
// if (exampleError) {
53-
// console.error("Error getting question example:", exampleError);
54-
// }
32+
const exampleData = sectionContent
33+
.filter((s) => s.inView)
34+
.map((s) => s.question)
35+
.filter((qe) => qe !== undefined)
36+
.flat();
5537
// 質問フォームを開くたびにランダムに選び直し、
5638
// exampleData[Math.floor(exampleChoice * exampleData.length)] を採用する
57-
const [exampleChoice, setExampleChoice] = useState<number>(0); // 0〜1
39+
const [exampleChoice, setExampleChoice] = useState<number | undefined>(
40+
undefined
41+
); // 0〜1
5842
useEffect(() => {
59-
if (exampleChoice === 0) {
43+
if (exampleChoice === undefined) {
6044
setExampleChoice(Math.random());
6145
}
6246
}, [exampleChoice]);
6347

6448
const handleSubmit = async (e: FormEvent<HTMLFormElement>) => {
65-
e.preventDefault();
66-
setIsLoading(true);
67-
setErrorMessage(null); // Clear previous error message
49+
let userQuestion = inputValue;
50+
if (!userQuestion && exampleData.length > 0 && exampleChoice) {
51+
// 質問が空欄なら、質問例を使用
52+
userQuestion =
53+
exampleData[Math.floor(exampleChoice * exampleData.length)];
54+
setInputValue(userQuestion);
55+
}
56+
if (userQuestion) {
57+
e.preventDefault();
58+
setIsLoading(true);
59+
setErrorMessage(null); // Clear previous error message
6860

69-
const userQuestion = inputValue;
70-
// if (!userQuestion && exampleData) {
71-
// // 質問が空欄なら、質問例を使用
72-
// userQuestion =
73-
// exampleData[Math.floor(exampleChoice * exampleData.length)];
74-
// setInputValue(userQuestion);
75-
// }
61+
const result = await askAI({
62+
path,
63+
userQuestion,
64+
sectionContent,
65+
replOutputs,
66+
files,
67+
execResults,
68+
});
7669

77-
const result = await askAI({
78-
path,
79-
userQuestion,
80-
sectionContent,
81-
replOutputs,
82-
files,
83-
execResults,
84-
});
70+
if (result.error !== null) {
71+
setErrorMessage(result.error);
72+
console.log(result.error);
73+
} else {
74+
addChat(result.chat);
75+
document.getElementById(result.chat.sectionId)?.scrollIntoView({
76+
behavior: "smooth",
77+
});
78+
setInputValue("");
79+
close();
80+
}
8581

86-
if (result.error !== null) {
87-
setErrorMessage(result.error);
88-
console.log(result.error);
89-
} else {
90-
addChat(result.chat);
91-
document.getElementById(result.chat.sectionId)?.scrollIntoView({
92-
behavior: "smooth",
93-
});
94-
setInputValue("");
95-
close();
82+
setIsLoading(false);
9683
}
97-
98-
setIsLoading(false);
9984
};
10085

10186
return (
@@ -110,10 +95,10 @@ export function ChatForm({ path, sectionContent, close }: ChatFormProps) {
11095
<textarea
11196
className="textarea textarea-ghost textarea-md rounded-box"
11297
placeholder={
113-
"質問を入力してください" /* +
114-
(exampleData
98+
"質問を入力してください" +
99+
(exampleData.length > 0 && exampleChoice !== undefined
115100
? ` (例:「${exampleData[Math.floor(exampleChoice * exampleData.length)]}」)`
116-
: "")*/
101+
: "")
117102
}
118103
style={{
119104
width: "100%",

0 commit comments

Comments
 (0)