Skip to content

Commit 5deb0a7

Browse files
committed
sent a message to user when try to send empty question
1 parent a3e5a53 commit 5deb0a7

File tree

1 file changed

+36
-26
lines changed

1 file changed

+36
-26
lines changed

app/components/chat.tsx

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,16 @@ export function Chat(props: { repo: string }) {
4444

4545
// Send the user query to the server
4646
async function sendQuery(q: string) {
47+
if (!q) {
48+
toast({
49+
variant: "destructive",
50+
title: "Uh oh! Something went wrong.",
51+
description: "Please enter a question.",
52+
})
53+
setQuery("")
54+
return
55+
}
56+
4757
setMessages((messages) => [...messages, { text: q, type: MessageTypes.Query }, { text: "", type: MessageTypes.Pending }]);
4858

4959
return fetch(`/api/repo/${props.repo}?q=${encodeURIComponent(q)}&type=text`, {
@@ -57,16 +67,16 @@ export function Chat(props: { repo: string }) {
5767
return result.json()
5868
}).then(data => {
5969
// Create an array of messages from the current messages remove the last pending message and add the new response
60-
setMessages(function(messages) {
61-
if(messages[messages.length - 1].type === MessageTypes.Pending){
70+
setMessages(function (messages) {
71+
if (messages[messages.length - 1].type === MessageTypes.Pending) {
6272
// Remove the last pending message if exists
63-
messages = messages.slice(0, -1);
64-
}
73+
messages = messages.slice(0, -1);
74+
}
6575
return [...messages, { text: data.result, type: MessageTypes.Response }];
6676
});
6777
}).catch((error) => {
68-
setMessages(function(messages) {
69-
if(messages[messages.length - 1].type === MessageTypes.Pending){
78+
setMessages(function (messages) {
79+
if (messages[messages.length - 1].type === MessageTypes.Pending) {
7080
// Remove the last pending message if exists
7181
return messages.slice(0, -1);
7282
}
@@ -83,7 +93,7 @@ export function Chat(props: { repo: string }) {
8393
// A function that handles the click event
8494
async function handleQueryClick(event: any) {
8595
event.preventDefault();
86-
return sendQuery(query);
96+
return sendQuery(query.trim());
8797
}
8898

8999
// On question selected from the predefined questions list
@@ -117,32 +127,32 @@ export function Chat(props: { repo: string }) {
117127
} else {
118128
return (<div key={index} className="flex items-end gap-2">
119129
<div>
120-
<Image src="/dots.gif" width={100} height={10} alt="Waiting for response"/>
130+
<Image src="/dots.gif" width={100} height={10} alt="Waiting for response" />
121131
</div>
122132
</div>)
123133
}
124134
})
125135
}
126136
</main>
127137
<footer className="border p-4">
128-
{props.repo &&
129-
<form className="flex flex-row gap-2" onSubmit={handleQueryClick}>
130-
<Select onValueChange={onQuestionSelected}>
131-
<SelectTrigger className="w-1/3">
132-
<SelectValue placeholder="Suggested questions" />
133-
</SelectTrigger>
134-
<SelectContent>
135-
{
136-
QUESTIONS.map((question, index) => {
137-
return <SelectItem key={index} value={question}>{question}</SelectItem>
138-
})
139-
}
140-
</SelectContent>
141-
</Select>
142-
<Input className="w-2/3" placeholder="Type a question..." onChange={handleQueryInputChange} />
143-
<Button>Send</Button>
144-
</form>
145-
}
138+
{props.repo &&
139+
<form className="flex flex-row gap-2" onSubmit={handleQueryClick}>
140+
<Select onValueChange={onQuestionSelected}>
141+
<SelectTrigger className="w-1/3">
142+
<SelectValue placeholder="Suggested questions" />
143+
</SelectTrigger>
144+
<SelectContent>
145+
{
146+
QUESTIONS.map((question, index) => {
147+
return <SelectItem key={index} value={question}>{question}</SelectItem>
148+
})
149+
}
150+
</SelectContent>
151+
</Select>
152+
<Input className="w-2/3" placeholder="Type a question..." onChange={handleQueryInputChange} value={query}/>
153+
<Button>Send</Button>
154+
</form>
155+
}
146156
</footer>
147157
</>
148158
);

0 commit comments

Comments
 (0)