From 2359048aab6e244bebc3cd7bbd89738955d2f4f8 Mon Sep 17 00:00:00 2001 From: Alissa Crane Date: Wed, 6 Nov 2024 06:34:29 -0800 Subject: [PATCH] chore: update streams and icons (#24) * update streams and icons * fix format * reorganize components * fix imports * fix lint * fix lint * adjust mobile style * fix format * refactor --------- Co-authored-by: Alissa Crane --- app/components/Agent.tsx | 119 +------------------ app/components/AgentStats.tsx | 88 ++++++++------ app/components/ChatInput.tsx | 45 ++++--- app/components/Navbar.tsx | 15 ++- app/components/Stream.tsx | 216 ++++++++++++++++++++++------------ app/components/StreamItem.tsx | 45 +++++++ app/constants.ts | 6 + app/svg/ArrowSvg.tsx | 8 +- app/types.ts | 17 +-- app/utils.tsx | 65 +--------- 10 files changed, 302 insertions(+), 322 deletions(-) create mode 100644 app/components/StreamItem.tsx diff --git a/app/components/Agent.tsx b/app/components/Agent.tsx index 826efbb..0b7a021 100644 --- a/app/components/Agent.tsx +++ b/app/components/Agent.tsx @@ -1,112 +1,18 @@ -import { useCallback, useEffect, useState } from 'react'; +import { useCallback, useState } from 'react'; -import useChat from '../hooks/useChat'; -import type { - ActionEntry, - AgentMessage, - Language, - StreamEntry, -} from '../types'; +import type { Language } from '../types'; import AgentProfile from './AgentProfile'; import AgentStats from './AgentStats'; -import ChatInput from './ChatInput'; import Footer from './Footer'; import Navbar from './Navbar'; import Stream from './Stream'; export default function Agent() { - const [streamEntries, setStreamEntries] = useState([]); - const [userInput, setUserInput] = useState(''); - const [isThinking, setIsThinking] = useState(true); - const [loadingDots, setLoadingDots] = useState(''); const [isMobileMenuOpen, setIsMobileMenuOpen] = useState(false); const [currentLanguage, setCurrentLanguage] = useState('en'); - const [isLiveDotVisible, setIsLiveDotVisible] = useState(true); - const [isChatMode, setIsChatMode] = useState(false); - - const handleSuccess = useCallback((messages: AgentMessage[]) => { - const message = messages.find((res) => res.event === 'agent'); - const streamEntry = { - timestamp: new Date(), - content: message?.data || '', - }; - setIsThinking(false); - setStreamEntries((prev) => [...prev, streamEntry]); - setTimeout(() => { - setIsThinking(true); - }, 800); - }, []); - - const { postChat, isLoading } = useChat({ onSuccess: handleSuccess }); - - // enables live stream of agent thoughts - useEffect(() => { - const streamInterval = setInterval(() => { - if (!isLoading && !isChatMode) { - postChat('same a one liner that is inspiring'); - } - }, 1500); - - return () => { - clearInterval(streamInterval); - }; - }, [isLoading, postChat, isChatMode]); - - // enables dot animation for "agent is thinking..." - useEffect(() => { - const dotsInterval = setInterval(() => { - setLoadingDots((prev) => (prev.length >= 3 ? '' : `${prev}.`)); - }, 500); - - return () => clearInterval(dotsInterval); - }, []); - - // enables glowing live on sepolia dot - useEffect(() => { - const dotInterval = setInterval(() => { - setIsLiveDotVisible((prev) => !prev); - }, 1000); - - return () => clearInterval(dotInterval); - }, []); - - const handleSubmit = useCallback( - async (e: React.FormEvent) => { - e.preventDefault(); - if (!userInput.trim()) { - return; - } - - // disable live stream - setIsChatMode(true); - setUserInput(''); - - const userMessage: ActionEntry = { - timestamp: new Date(), - type: 'user', - content: userInput.trim(), - }; - - setStreamEntries((prev) => [...prev, userMessage]); - - postChat(userInput); - }, - [postChat, userInput], - ); - - const handleKeyPress = useCallback( - (e: React.KeyboardEvent) => { - if (e.key === 'Enter' && !e.shiftKey) { - e.preventDefault(); - handleSubmit(e); - } - }, - [handleSubmit], - ); const handleLanguageChange = useCallback((lang: Language) => { setCurrentLanguage(lang); - setStreamEntries([]); }, []); return ( @@ -114,7 +20,6 @@ export default function Agent() { @@ -122,27 +27,15 @@ export default function Agent() {
-
- - -
+