Skip to content

Commit

Permalink
auto refetch nfts and tokens after deployment
Browse files Browse the repository at this point in the history
  • Loading branch information
alissacrane-cb committed Nov 9, 2024
1 parent 0679f54 commit 955edb1
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 12 deletions.
18 changes: 16 additions & 2 deletions app/components/Agent.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { useState } from 'react';
import type { Address } from 'viem';
import useGetNFTs from '../hooks/useGetNFTs';
import useGetTokens from '../hooks/useGetTokens';
import AgentAssets from './AgentAssets';
import AgentProfile from './AgentProfile';
import Chat from './Chat';
Expand All @@ -9,6 +12,12 @@ export default function Agent() {
const [isMobileMenuOpen, setIsMobileMenuOpen] = useState(false);
const [isMobileChatOpen, setIsMobileChatOpen] = useState(false);

const [nfts, setNFTs] = useState<Address[]>([]);
const [tokens, setTokens] = useState<Address[]>([]);

const { getTokens } = useGetTokens({ onSuccess: setTokens });
const { getNFTs } = useGetNFTs({ onSuccess: setNFTs });

return (
<div className="relative flex h-screen flex-col overflow-hidden bg-black font-mono text-[#5788FA]">
<Navbar
Expand All @@ -26,11 +35,16 @@ export default function Agent() {
} 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 />
<AgentAssets />
<AgentAssets
getTokens={getTokens}
getNFTs={getNFTs}
tokens={tokens}
nfts={nfts}
/>
</div>

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

Expand Down
22 changes: 14 additions & 8 deletions app/components/AgentAssets.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import { type Token, TokenRow } from '@coinbase/onchainkit/token';
import { useCallback, useEffect, useMemo, useState } from 'react';
import { type Address, erc721Abi } from 'viem';
import { useContractRead, useToken } from 'wagmi';
import useGetNFTs from '../hooks/useGetNFTs';
import useGetTokens from '../hooks/useGetTokens';

type AgentAssetProps = {
tokenAddress: Address;
Expand Down Expand Up @@ -57,13 +55,21 @@ function AgentNFT({ index = 0, tokenAddress }: AgentAssetProps) {
</NFTMintCard>
);
}
export default function AgentAssets() {
const [tab, setTab] = useState('tokens');
const [nfts, setNFTs] = useState<Address[]>([]);
const [tokens, setTokens] = useState<Address[]>([]);

const { getTokens } = useGetTokens({ onSuccess: setTokens });
const { getNFTs } = useGetNFTs({ onSuccess: setNFTs });
type AgentAssetsProps = {
getTokens: () => void;
getNFTs: () => void;
nfts: Address[];
tokens: Address[];
};

export default function AgentAssets({
getTokens,
getNFTs,
tokens,
nfts,
}: AgentAssetsProps) {
const [tab, setTab] = useState('tokens');

const handleTabChange = useCallback((tab: string) => {
return () => setTab(tab);
Expand Down
31 changes: 29 additions & 2 deletions app/components/Chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,47 @@ import ChatInput from './ChatInput';
import StreamItem from './StreamItem';

type ChatProps = {
enableLiveStream?: boolean;
className?: string;
getNFTs: () => void;
getTokens: () => void;
};

export default function Chat({ className }: ChatProps) {
export default function Chat({ className, getNFTs, getTokens }: ChatProps) {
const [userInput, setUserInput] = useState('');
const [streamEntries, setStreamEntries] = useState<StreamEntry[]>([]);
const conversationId = useMemo(() => {
return generateUUID();
}, []);

const [shouldRefetchNFTs, setShouldRefetchNFTs] = useState(false);
const [shouldRefetchTokens, setShouldRefetchTokens] = useState(false);

useEffect(() => {
if (shouldRefetchNFTs) {
getNFTs();
setShouldRefetchNFTs(false);
}
}, [getNFTs, shouldRefetchNFTs]);

useEffect(() => {
if (shouldRefetchTokens) {
getTokens();
setShouldRefetchTokens(false);
}
}, [getTokens, shouldRefetchTokens]);

const bottomRef = useRef<HTMLDivElement>(null);

const handleSuccess = useCallback((messages: AgentMessage[]) => {
const functions =
messages?.find((msg) => msg.event === 'tools')?.functions || [];
if (functions?.includes('deploy_nft')) {
setShouldRefetchNFTs(true);
}
if (functions?.includes('deploy_token')) {
setShouldRefetchTokens(true);
}

let message = messages.find((res) => res.event === 'agent');
if (!message) {
message = messages.find((res) => res.event === 'tools');
Expand Down
1 change: 1 addition & 0 deletions app/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ export type AnimatedData = {
export type AgentMessage = {
data?: string;
event: 'agent' | 'tools' | 'completed' | 'error';
functions?: string[];
};

0 comments on commit 955edb1

Please sign in to comment.