Skip to content
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
34 changes: 28 additions & 6 deletions application/frontend/src/pages/chatbot/chatbot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { oneLight } from 'react-syntax-highlighter/dist/esm/styles/prism';
import { Button, Container, Form, GridRow, Header, Icon } from 'semantic-ui-react';
import { Grid } from 'semantic-ui-react';

import { LoadingAndErrorIndicator } from '../../components/LoadingAndErrorIndicator';

import { useEnvironment } from '../../hooks';
import { Document } from '../../types';

Expand Down Expand Up @@ -93,11 +93,17 @@ export const Chatbot = () => {
})
.catch((error) => {
console.error('Error checking if user is logged in:', error);
setError(error);
setError(error instanceof Error ? error.message : 'Network error checking login status');
setLoading(false);
});
}

useEffect(() => {
if (user === '') {
login();
}
}, []);

function processResponse(response: string) {
const responses = response.split('```');
const res: JSX.Element[] = [];
Expand Down Expand Up @@ -148,7 +154,22 @@ export const Chatbot = () => {
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ prompt: currentTerm }),
})
.then((response) => response.json())
.then(async (response) => {
if (!response.ok) {
const text = await response.text();
let errorMessage = response.statusText;
try {
const json = JSON.parse(text);
if (json.error) errorMessage = json.error;
else if (json.message) errorMessage = json.message;
} catch (e) {
// not json, use text if available
if (text) errorMessage = text;
}
throw new Error(errorMessage || `Error ${response.status}`);
}
return response.json();
})
.then((data) => {
setLoading(false);
setError('');
Expand All @@ -168,7 +189,7 @@ export const Chatbot = () => {
})
.catch((error) => {
console.error('Error fetching answer:', error);
setError(error);
setError(error instanceof Error ? error.message : 'An unexpected network error occurred');
setLoading(false);
});
}
Expand All @@ -193,7 +214,7 @@ export const Chatbot = () => {

return (
<>
{user !== '' ? null : login()}
{/* user login check moved to useEffect */}

<Grid textAlign="center" verticalAlign="middle" className="chatbot-layout">
<Grid.Column>
Expand All @@ -207,7 +228,8 @@ export const Chatbot = () => {
{' '}
{error && (
<div className="ui negative message">
<div className="header">Document could not be loaded</div>
<div className="header">Error</div>
<p>{error}</p>
</div>
)}
<div className="chat-messages" ref={messagesContainerRef}>
Expand Down