diff --git a/frontend/.env.development b/frontend/.env.development index d728457f..d9292fb3 100644 --- a/frontend/.env.development +++ b/frontend/.env.development @@ -1,2 +1,2 @@ # Please put appropriate value -VITE_API_HOST = https://dev.docsapi.arc53.com \ No newline at end of file +VITE_API_HOST = https://docsapi.arc53.com \ No newline at end of file diff --git a/frontend/src/conversation/Conversation.tsx b/frontend/src/conversation/Conversation.tsx index a33a4fe1..62bea48b 100644 --- a/frontend/src/conversation/Conversation.tsx +++ b/frontend/src/conversation/Conversation.tsx @@ -1,19 +1,20 @@ -import { useEffect, useRef } from 'react'; +import { Fragment, useEffect, useRef } from 'react'; import { useDispatch, useSelector } from 'react-redux'; import Hero from '../Hero'; import { AppDispatch } from '../store'; import ConversationBubble from './ConversationBubble'; import { - addMessage, + addQuery, fetchAnswer, - selectConversation, + selectQueries, selectStatus, } from './conversationSlice'; import Send from './../assets/send.svg'; import Spinner from './../assets/spinner.svg'; +import { Query } from './conversationModels'; export default function Conversation() { - const messages = useSelector(selectConversation); + const queries = useSelector(selectQueries); const status = useSelector(selectStatus); const dispatch = useDispatch(); const endMessageRef = useRef(null); @@ -21,34 +22,65 @@ export default function Conversation() { useEffect( () => endMessageRef?.current?.scrollIntoView({ behavior: 'smooth' }), - [messages], + [queries], ); const handleQuestion = (question: string) => { - dispatch(addMessage({ text: question, type: 'QUESTION' })); + dispatch(addQuery({ prompt: question })); dispatch(fetchAnswer({ question })); }; + const prepResponseView = (query: Query, index: number) => { + let responseView; + if (query.error) { + responseView = ( + + ); + } else if (query.response) { + responseView = ( + + ); + } + return responseView; + }; + return (
- {messages.length > 0 && ( + {queries.length > 0 && (
- {messages.map((message, index) => { + {queries.map((query, index) => { return ( - + + + {prepResponseView(query, index)} + ); })}
)} - {messages.length === 0 && } + {queries.length === 0 && }
(function ConversationBubble({ message, type, className }, ref) { return ( diff --git a/frontend/src/conversation/conversationModels.ts b/frontend/src/conversation/conversationModels.ts index 17a3bd13..92ed976f 100644 --- a/frontend/src/conversation/conversationModels.ts +++ b/frontend/src/conversation/conversationModels.ts @@ -8,7 +8,7 @@ export interface Message { } export interface ConversationState { - conversation: Message[]; + queries: Query[]; status: Status; } @@ -22,4 +22,5 @@ export interface Query { prompt: string; response?: string; feedback?: FEEDBACK; + error?: string; } diff --git a/frontend/src/conversation/conversationSlice.ts b/frontend/src/conversation/conversationSlice.ts index a822a380..6701bc10 100644 --- a/frontend/src/conversation/conversationSlice.ts +++ b/frontend/src/conversation/conversationSlice.ts @@ -1,10 +1,10 @@ import { createAsyncThunk, createSlice, PayloadAction } from '@reduxjs/toolkit'; import store from '../store'; import { fetchAnswerApi } from './conversationApi'; -import { Answer, ConversationState, Message } from './conversationModels'; +import { Answer, ConversationState, Query } from './conversationModels'; const initialState: ConversationState = { - conversation: [], + queries: [], status: 'idle', }; @@ -27,8 +27,8 @@ export const conversationSlice = createSlice({ name: 'conversation', initialState, reducers: { - addMessage(state, action: PayloadAction) { - state.conversation.push(action.payload); + addQuery(state, action: PayloadAction) { + state.queries.push(action.payload); }, }, extraReducers(builder) { @@ -38,27 +38,22 @@ export const conversationSlice = createSlice({ }) .addCase(fetchAnswer.fulfilled, (state, action) => { state.status = 'idle'; - state.conversation.push({ - text: action.payload.answer, - type: 'ANSWER', - }); + state.queries[state.queries.length - 1].response = + action.payload.answer; }) .addCase(fetchAnswer.rejected, (state, action) => { state.status = 'failed'; - state.conversation.push({ - text: 'Something went wrong. Please try again later.', - type: 'ERROR', - }); + state.queries[state.queries.length - 1].error = + 'Something went wrong. Please try again later.'; }); }, }); type RootState = ReturnType; -export const selectConversation = (state: RootState) => - state.conversation.conversation; +export const selectQueries = (state: RootState) => state.conversation.queries; export const selectStatus = (state: RootState) => state.conversation.status; -export const { addMessage } = conversationSlice.actions; +export const { addQuery } = conversationSlice.actions; export default conversationSlice.reducer;