Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

solve the empty query problem in chat.tsx #92

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion app/components/chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ export function Chat(props: { repo: string }) {
// Holds the user input while typing
const [query, setQuery] = useState('');

// Holds whether the input is empty 'ARI
const [isInputEmpty, setIsInputEmpty] = useState(true);

// A reference to the chat container to allow scrolling to the bottom
const bottomRef: React.RefObject<HTMLDivElement> = useRef(null);

Expand All @@ -38,12 +41,21 @@ export function Chat(props: { repo: string }) {
// Get the new value of the input box
const value = event.target.value;

// Update the input empty state
setIsInputEmpty(value.trim() === '');

// Update the url state
setQuery(value);
}

// Send the user query to the server
async function sendQuery(q: string) {

if (!q.trim()) {
toast({ variant: "destructive", title: "Empty Query", description: "Please enter a query." });
return;
}

setMessages((messages) => [...messages, { text: q, type: MessageTypes.Query }, { text: "", type: MessageTypes.Pending }]);

return fetch(`/api/repo/${props.repo}?q=${encodeURIComponent(q)}&type=text`, {
Expand Down Expand Up @@ -89,6 +101,7 @@ export function Chat(props: { repo: string }) {
// On question selected from the predefined questions list
async function onQuestionSelected(value: string) {
setQuery(value)
setIsInputEmpty(false); // input is not empty if a question is selected
return sendQuery(value)
}

Expand Down Expand Up @@ -140,7 +153,7 @@ export function Chat(props: { repo: string }) {
</SelectContent>
</Select>
<Input className="w-2/3" placeholder="Type a question..." onChange={handleQueryInputChange} />
<Button>Send</Button>
<Button disabled={isInputEmpty}>Send</Button>
</form>
}
</footer>
Expand Down
Loading