import { useEffect, useRef, useState } from 'react'; import './App.css' const BASE_URL='http://localhost:3333'; function App() { const [history, setHistory] = useState([]); const [processing, setProcessing] = useState(false); const [prompt, setPrompt] = useState([]); const refHistoryContainer= useRef(null); const refPrompContainer= useRef(null); function onSubmit(event) { event.preventDefault(); const input = event.target.elements.input.value; console.log("Submitted:", input); // setProcessing(true); fetch(`${BASE_URL}/generate`, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ input:[ ...history, { role:'user', content:[{ type:'input_text', text: input }] } ] }), }).then(response => { if (!response.ok) { throw new Error('Network response was not ok'); } response.json().then(data => {; console.log(data); // add to history setHistory(prev => [...prev, { role: 'user', content: [{ type:'input_text', text: input }] }, { role: 'assistant', content: [{ type:'output_text', text: data.output_text }] }]); setPrompt([ ...prompt, data.prompt, ]); // clear input event.target.elements.input.value = ''; setProcessing(false); }); }); } useEffect(()=>{ refHistoryContainer.current.scrollTop = refHistoryContainer.current.scrollHeight; },[history]); useEffect(()=>{ refPrompContainer.current.scrollTop = refPrompContainer.current.scrollHeight; },[prompt]); return (
{prompt?.length==0 ? (
Promp will appear here...
):( prompt?.map((item, index) => (

{item}

)) )}
{history?.length==0? (
History will appear here...
):( history.map((item, index) => (
{item.content.map((content, idx) => (

{content.text}

))}
)) )}
) } export default App