import { Fragment, useEffect, useRef, useState } from 'react'; import { useDispatch, useSelector } from 'react-redux'; import Hero from '../Hero'; import { AppDispatch } from '../store'; import ConversationBubble from './ConversationBubble'; import { addQuery, fetchAnswer, selectQueries, selectStatus, updateQuery, } from './conversationSlice'; import Send from './../assets/send.svg'; import SendDark from './../assets/send_dark.svg' import Spinner from './../assets/spinner.svg'; import { FEEDBACK, Query } from './conversationModels'; import { sendFeedback } from './conversationApi'; import ArrowDown from './../assets/arrow-down.svg'; export default function Conversation() { const queries = useSelector(selectQueries); const status = useSelector(selectStatus); const dispatch = useDispatch(); const endMessageRef = useRef(null); const inputRef = useRef(null); const isDarkTheme = document.documentElement.classList.contains('dark'); const [hasScrolledToLast, setHasScrolledToLast] = useState(true); useEffect(() => { scrollIntoView(); }, [queries.length, queries[queries.length - 1]]); useEffect(() => { const element = document.getElementById('inputbox') as HTMLInputElement; if (element) { element.focus(); } }, []); useEffect(() => { const observerCallback: IntersectionObserverCallback = (entries) => { entries.forEach((entry) => { setHasScrolledToLast(entry.isIntersecting); }); }; const observer = new IntersectionObserver(observerCallback, { root: null, threshold: [1, 0.8], }); if (endMessageRef.current) { observer.observe(endMessageRef.current); } return () => { observer.disconnect(); }; }, [endMessageRef.current]); const scrollIntoView = () => { endMessageRef?.current?.scrollIntoView({ behavior: 'smooth', block: 'start', }); }; const handleQuestion = (question: string) => { question = question.trim(); if (question === '') return; dispatch(addQuery({ prompt: question })); dispatch(fetchAnswer({ question })); }; const handleFeedback = (query: Query, feedback: FEEDBACK, index: number) => { const prevFeedback = query.feedback; dispatch(updateQuery({ index, query: { feedback } })); sendFeedback(query.prompt, query.response!, feedback).catch(() => dispatch(updateQuery({ index, query: { feedback: prevFeedback } })), ); }; const prepResponseView = (query: Query, index: number) => { let responseView; if (query.error) { responseView = ( ); } else if (query.response) { responseView = ( handleFeedback(query, feedback, index) } > ); } return responseView; }; const handlePaste = (e: React.ClipboardEvent) => { e.preventDefault(); const text = e.clipboardData.getData('text/plain'); document.execCommand('insertText', false, text); }; return (
{queries.length > 0 && !hasScrolledToLast && ( )} {queries.length > 0 && (
{queries.map((query, index) => { return ( {prepResponseView(query, index)} ); })}
)} {queries.length === 0 && ( )}
{ if (e.key === 'Enter' && !e.shiftKey) { e.preventDefault(); if (inputRef.current?.textContent && status !== 'loading') { handleQuestion(inputRef.current.textContent); inputRef.current.textContent = ''; } } }} >
{status === 'loading' ? ( ) : (
{ if (inputRef.current?.textContent) { handleQuestion(inputRef.current.textContent); inputRef.current.textContent = ''; } }} src={isDarkTheme ? SendDark : Send} >
)}

This is a chatbot that uses the GPT-3, Faiss and LangChain to answer questions.

); }