Skip to content

Commit

Permalink
update components with new design
Browse files Browse the repository at this point in the history
  • Loading branch information
alissacrane-cb committed Nov 6, 2024
1 parent 2359048 commit 71e81b6
Show file tree
Hide file tree
Showing 11 changed files with 300 additions and 177 deletions.
29 changes: 25 additions & 4 deletions app/components/Agent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ import { useCallback, useState } from 'react';

import type { Language } from '../types';
import AgentProfile from './AgentProfile';
import AgentStats from './AgentStats';
import AgentBalance from './AgentBalance';
import Footer from './Footer';
import Navbar from './Navbar';
import Stream from './Stream';
import Chat from './Chat';
import AgentAssets from './AgentAssets';

export default function Agent() {
const [isMobileMenuOpen, setIsMobileMenuOpen] = useState(false);
const [isMobileChatOpen, setIsMobileChatOpen] = useState(false);
const [currentLanguage, setCurrentLanguage] = useState<Language>('en');

const handleLanguageChange = useCallback((lang: Language) => {
Expand All @@ -20,6 +23,8 @@ export default function Agent() {
<Navbar
isMobileMenuOpen={isMobileMenuOpen}
setIsMobileMenuOpen={setIsMobileMenuOpen}
isMobileChatOpen={isMobileChatOpen}
setIsMobileChatOpen={setIsMobileChatOpen}
setCurrentLanguage={handleLanguageChange}
currentLanguage={currentLanguage}
/>
Expand All @@ -29,13 +34,29 @@ export default function Agent() {
className={`
${
isMobileMenuOpen ? 'translate-x-0' : '-translate-x-full'
} fixed z-20 flex h-full w-full flex-col overflow-y-auto bg-black p-2 transition-transform duration-300 lg:relative lg:z-0 lg:w-1/3 lg:translate-x-0 lg:border-[#5788FA]/50 lg:border-r `}
} fixed z-20 flex h-full w-full flex-col overflow-y-auto bg-black transition-transform duration-300 lg:relative lg:z-0 lg:w-1/3 lg:translate-x-0 lg:border-[#5788FA]/50 lg:border-r `}
>
<AgentProfile currentLanguage={currentLanguage} />
<AgentStats currentLanguage={currentLanguage} />
<AgentBalance />
<AgentAssets />
</div>

<Stream currentLanguage={currentLanguage} />
<div className="flex lg:w-2/3 w-full">
<Stream currentLanguage={currentLanguage} />
<Chat currentLanguage={currentLanguage} className="hidden" />
</div>

<div
className={`
${
isMobileChatOpen ? 'translate-y-0' : 'hidden'
} fixed pt-[100px] top-0 z-8 flex h-full w-full flex-col overflow-y-auto bg-black transition-transform duration-300 md:hidden`}
>
<Chat
currentLanguage={currentLanguage}
className="w-full flex-col flex"
/>
</div>
</div>
<Footer />
</div>
Expand Down
54 changes: 54 additions & 0 deletions app/components/AgentAssets.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { useCallback, useEffect, useMemo, useState } from 'react';
import getNfts from '../hooks/useGetNfts';

// TODO: add assets
export default function AgentAssets() {
const [tab, setTab] = useState('tokens');

const tokensClass = useMemo(() => {
if (tab === 'tokens') {
return 'border-b border-[#5788FA] flex items-center justify-center py-1';
}
return ' flex items-center justify-center py-1';
}, [tab]);
const nftsClass = useMemo(() => {
if (tab === 'nft') {
return 'border-b border-[#5788FA] flex items-center justify-center py-1';
}
return ' flex items-center justify-center py-1';
}, [tab]);
const createdClass = useMemo(() => {
if (tab === 'created') {
return 'border-b border-[#5788FA] flex items-center justify-center py-1';
}
return ' flex items-center justify-center py-1';
}, [tab]);

const handleTabChange = useCallback((tab: string) => {
return () => setTab(tab);
}, []);

useEffect(() => {
getNfts();
}, []);

return (
<div className="mr-2 mb-4 rounded-sm bg-black p-4">
<div className="flex flex-col items-start gap-4">
<div className="gap-6 flex border-b border-zinc-700 grow w-full">
<button onClick={handleTabChange('tokens')} className={tokensClass}>
Tokens
</button>
<button onClick={handleTabChange('nft')} className={nftsClass}>
NFTs
</button>
<button onClick={handleTabChange('created')} className={createdClass}>
Created
</button>
</div>

{tab === 'tokens' ? <div>tokens</div> : <div>nfts</div>}
</div>
</div>
);
}
19 changes: 19 additions & 0 deletions app/components/AgentBalance.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { useBalance } from 'wagmi';
import { AGENT_WALLET_ADDRESS } from '../constants';

export default function AgentBalance() {
const { data } = useBalance({
address: AGENT_WALLET_ADDRESS,
query: { refetchInterval: 5000 },
});

return (
<div className="rounded-sm bg-black border-t p-4 pt-8 border-zinc-700">
<div className="flex flex-col items-start ">
<span className="font-bold text-3xl text-[#5788FA]">
{`${Number.parseFloat(data?.formatted || '').toFixed(6)} ETH`}
</span>
</div>
</div>
);
}
8 changes: 5 additions & 3 deletions app/components/AgentProfile.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
import { AGENT_NAME, AGENT_WALLET_ADDRESS, notoSansThai } from '../constants';
import { translations } from '../translations';
import type { Language } from '../types';

type AgentProfileProps = {
Expand Down Expand Up @@ -59,7 +58,7 @@ export default function AgentProfile({ currentLanguage }: AgentProfileProps) {
}, []);

return (
<div className="mb-4">
<div className="p-4">
<div className="flex flex-col space-y-4 py-2">
<div className="flex items-center space-x-5">
<svg
Expand Down Expand Up @@ -104,7 +103,10 @@ export default function AgentProfile({ currentLanguage }: AgentProfileProps) {
currentLanguage === 'th' ? notoSansThai.className : ''
}`}
>
{translations[currentLanguage].profile.bio}
Lorem Ipsum is simply dummy text of the printing and typesetting
industry. Lorem Ipsum has been the industry's standard dummy text ever
since the 1500s, when an unknown printer took a galley of type and
scrambled it to make a type specimen book.
</p>
</div>
</div>
Expand Down
76 changes: 0 additions & 76 deletions app/components/AgentStats.tsx

This file was deleted.

112 changes: 112 additions & 0 deletions app/components/Chat.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import { useCallback, useEffect, useRef, useState } from 'react';
import { AgentMessage, Language, StreamEntry } from '../types';
import ChatInput from './ChatInput';
import useChat from '../hooks/useChat';
import StreamItem from './StreamItem';
import { notoSansThai } from '../constants';
import { cn } from '@coinbase/onchainkit/theme';

type ChatProps = {
currentLanguage: Language;
enableLiveStream?: boolean;
className?: string;
};

export default function Chat({ className, currentLanguage }: ChatProps) {
const [userInput, setUserInput] = useState('');
const [streamEntries, setStreamEntries] = useState<StreamEntry[]>([]);

const bottomRef = useRef<HTMLDivElement>(null);

// TODO: revisit this logic
const handleSuccess = useCallback((messages: AgentMessage[]) => {
// const message = messages.find((res) => res.event === "agent");
const filteredMessages = messages.filter(
(msg) => msg.event !== 'completed',
);
const streams = filteredMessages.map((msg) => {
return {
timestamp: new Date(),
content: msg?.data || '',
type: msg?.event,
};
});
// const streamEntry = {
// timestamp: new Date(),
// content: message?.data || "",
// };
setStreamEntries((prev) => [...prev, ...streams]);
}, []);

const { postChat, isLoading } = useChat({ onSuccess: handleSuccess });

const handleSubmit = useCallback(
async (e: React.FormEvent) => {
e.preventDefault();
if (!userInput.trim()) {
return;
}

setUserInput('');

const userMessage: StreamEntry = {
timestamp: new Date(),
type: 'user',
content: userInput.trim(),
};

setStreamEntries((prev) => [...prev, userMessage]);

postChat(userInput);
},
[postChat, userInput],
);

const handleKeyPress = useCallback(
(e: React.KeyboardEvent<HTMLTextAreaElement>) => {
if (e.key === 'Enter' && !e.shiftKey) {
e.preventDefault();
handleSubmit(e);
}
},
[handleSubmit],
);

useEffect(() => {
// scrolls to the bottom of the chat when messages change
bottomRef.current?.scrollIntoView({ behavior: 'smooth' });
}, [streamEntries]);

return (
<div className={cn('flex flex-col w-1/2 grow md:flex h-full', className)}>
<div className="flex flex-col overflow-y-auto p-4 pb-20 grow">
<p
className={`text-zinc-500 ${
currentLanguage === 'th' ? notoSansThai.className : ''
}`}
>
Ask me something...
</p>
<div className="mt-4 space-y-2" role="log" aria-live="polite">
{streamEntries.map((entry, index) => (
<StreamItem
key={`${entry.timestamp.toDateString()}-${index}`}
entry={entry}
currentLanguage={currentLanguage}
/>
))}
</div>

<div className="mt-3" ref={bottomRef} />
</div>

<ChatInput
currentLanguage={currentLanguage}
userInput={userInput}
handleKeyPress={handleKeyPress}
handleSubmit={handleSubmit}
setUserInput={setUserInput}
/>
</div>
);
}
15 changes: 5 additions & 10 deletions app/components/ChatInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function PremadeChatInput({
<button
type="submit"
onClick={() => setUserInput(text)}
className={`w-full whitespace-nowrap rounded-sm border border-[#5788FA]/50 px-2 py-1 text-[#5788FA] transition-colors hover:bg-zinc-900 hover:text-[#3D7BFF] lg:w-auto lg:grow ${
className={`w-full whitespace-nowrap text-start rounded-sm border border-[#5788FA]/50 px-2 py-1 text-[#5788FA] transition-colors hover:bg-zinc-900 hover:text-[#3D7BFF] lg:w-auto ${
currentLanguage === 'th' ? notoSansThai.className : ''
}`}
>
Expand Down Expand Up @@ -54,15 +54,15 @@ export default function ChatInput({
return (
<form
onSubmit={handleSubmit}
className="flex w-full flex-col border-[#5788FA]/50 border-t bg-black p-4 pb-10 lg:pb-2"
className="flex w-full flex-col border-[#5788FA]/50 border-t bg-black p-4 pb-10 lg:pb-2 mt-auto md:mt-0"
>
<div className="flex flex-col gap-2">
<div className="flex gap-2">
<textarea
value={userInput}
onChange={handleInputChange}
onKeyPress={handleKeyPress}
className={`h-24 w-full bg-black p-2 pr-10 text-[#5788FA] placeholder-[#5788FA] placeholder-opacity-50 lg:h-36 ${
className={`h-24 w-full bg-black p-2 pr-10 text-gray-300 placeholder-[#5788FA] placeholder-opacity-50 lg:h-36 ${
currentLanguage === 'th' ? notoSansThai.className : ''
}`}
placeholder={translations[currentLanguage].chat.placeholder}
Expand All @@ -81,16 +81,11 @@ export default function ChatInput({
</button>
</div>
<div className="flex w-full items-center justify-between gap-4 py-2">
<div className="flex grow flex-col gap-2 overflow-x-auto text-xs lg:flex-row lg:text-sm">
<div className="flex grow flex-col gap-2 overflow-x-auto text-xs lg:flex-row lg:text-sm flex-wrap">
<PremadeChatInput
setUserInput={setUserInput}
currentLanguage={currentLanguage}
text={translations[currentLanguage].chat.suggestions.send}
/>
<PremadeChatInput
setUserInput={setUserInput}
currentLanguage={currentLanguage}
text={translations[currentLanguage].chat.suggestions.create}
text="Create a new token with all the specifications"
/>
<PremadeChatInput
setUserInput={setUserInput}
Expand Down
Loading

0 comments on commit 71e81b6

Please sign in to comment.