Skip to content

Commit b60d945

Browse files
authored
Merge pull request #67 from ut-code/copilot/add-error-message-to-chat
Display chat error messages to users in the chat form
2 parents 8436232 + 69221ae commit b60d945

File tree

1 file changed

+47
-44
lines changed

1 file changed

+47
-44
lines changed

app/[docs_id]/chatForm.tsx

Lines changed: 47 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export function ChatForm({
2828
// const [messages, updateChatHistory] = useChatHistory(sectionId);
2929
const [inputValue, setInputValue] = useState("");
3030
const [isLoading, setIsLoading] = useState(false);
31+
const [errorMessage, setErrorMessage] = useState<string | null>(null);
3132

3233
const { addChat } = useChatHistoryContext();
3334

@@ -68,6 +69,7 @@ export function ChatForm({
6869
const handleSubmit = async (e: FormEvent<HTMLFormElement>) => {
6970
e.preventDefault();
7071
setIsLoading(true);
72+
setErrorMessage(null); // Clear previous error message
7173

7274
const userMessage: ChatMessage = { sender: "user", text: inputValue };
7375

@@ -89,12 +91,8 @@ export function ChatForm({
8991
});
9092

9193
if (result.error) {
92-
const errorMessage: ChatMessage = {
93-
sender: "error",
94-
text: `エラー: ${result.error}`,
95-
};
94+
setErrorMessage(result.error);
9695
console.log(result.error);
97-
// TODO: ユーザーに表示
9896
} else {
9997
const aiMessage: ChatMessage = { sender: "ai", text: result.response };
10098
const chatId = addChat(result.targetSectionId, [userMessage, aiMessage]);
@@ -115,54 +113,59 @@ export function ChatForm({
115113
}}
116114
onSubmit={handleSubmit}
117115
>
118-
<div className="input-area">
119-
<textarea
120-
className="textarea textarea-ghost textarea-md rounded-lg"
121-
placeholder={
122-
"質問を入力してください" +
123-
(exampleData
124-
? ` (例:「${exampleData[Math.floor(exampleChoice * exampleData.length)]}」)`
125-
: "")
126-
}
127-
style={{
128-
width: "100%",
129-
height: "110px",
130-
resize: "none",
131-
outlineStyle: "none",
132-
}}
133-
value={inputValue}
134-
onChange={(e) => setInputValue(e.target.value)}
135-
disabled={isLoading}
136-
></textarea>
137-
</div>
116+
<textarea
117+
className="textarea textarea-ghost textarea-md rounded-lg"
118+
placeholder={
119+
"質問を入力してください" +
120+
(exampleData
121+
? ` (例:「${exampleData[Math.floor(exampleChoice * exampleData.length)]}」)`
122+
: "")
123+
}
124+
style={{
125+
width: "100%",
126+
height: "110px",
127+
resize: "none",
128+
outlineStyle: "none",
129+
}}
130+
value={inputValue}
131+
onChange={(e) => setInputValue(e.target.value)}
132+
disabled={isLoading}
133+
></textarea>
138134
<div
139-
className="controls"
140135
style={{
141136
margin: "10px",
142137
display: "flex",
143138
alignItems: "center",
144139
justifyContent: "space-between",
145140
}}
146141
>
147-
<div className="left-icons">
148-
<button
149-
className="btn btn-soft btn-secondary rounded-full"
150-
onClick={close}
151-
type="button"
152-
>
153-
閉じる
154-
</button>
155-
</div>
156-
<div className="right-controls">
157-
<button
158-
type="submit"
159-
className="btn btn-soft btn-circle btn-accent border-2 border-accent rounded-full"
160-
title="送信"
161-
disabled={isLoading}
142+
<button
143+
className="btn btn-soft btn-secondary rounded-full"
144+
onClick={close}
145+
type="button"
146+
>
147+
閉じる
148+
</button>
149+
{errorMessage && (
150+
<div
151+
className="text-error text-left text-nowrap overflow-hidden text-ellipsis"
152+
style={{
153+
marginLeft: "10px",
154+
marginRight: "10px",
155+
flex: 1,
156+
}}
162157
>
163-
<span className="icon"></span>
164-
</button>
165-
</div>
158+
{errorMessage}
159+
</div>
160+
)}
161+
<button
162+
type="submit"
163+
className="btn btn-soft btn-circle btn-accent border-2 border-accent rounded-full"
164+
title="送信"
165+
disabled={isLoading}
166+
>
167+
<span className="icon"></span>
168+
</button>
166169
</div>
167170
</form>
168171
);

0 commit comments

Comments
 (0)