Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove agent from agents list after successful deletion #452

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions apps/chat/components/accounts/profile/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ interface AgentImage {
}

interface Agent {
id: string;
id: number;
name: string;
images: AgentImage[];
}
Expand All @@ -31,6 +31,10 @@ export function AccountProfile({ account }: AccountProps) {
const agentUrl = `${origin}/accounts/${account.id}`;
const randomAgentImage = account.agents[Math.floor(Math.random() * account.agents.length)]?.images[0].url;

const onDelete = (agentId: number) => {
account.agents = account.agents.filter((agent) => agent.id !== agentId);
};

return (
<div className="w-full mx-auto">
<HeaderMaskFrame background={randomAgentImage} wrapperClass="bg-blue-500" backgroundOpacity={0.2}>
Expand Down Expand Up @@ -63,7 +67,7 @@ export function AccountProfile({ account }: AccountProps) {
<div className="w-full max-w-6xl mx-auto relative px-4 mt-8">
<div className="grid grid-cols-1 md:grid-cols-2 gap-4 py-8">
{account.agents.map((agent) => (
<AgentRow key={agent.id} agent={agent} author={account.name} user={null} />
<AgentRow key={agent.id} agent={agent} author={account.name} user={null} onDelete={onDelete}/>
))}
</div>
</div>
Expand Down
5 changes: 3 additions & 2 deletions apps/chat/components/agents/list/AgentList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ export interface AgentListProps {
loading: boolean
range: number
user: any
onAgentDelete: (agentId: number) => void
}

export function AgentList({ agents, loading, range, user }: AgentListProps) {
export function AgentList({ agents, loading, range, user, onAgentDelete }: AgentListProps) {

if (loading) return (
<>
Expand All @@ -20,5 +21,5 @@ export function AgentList({ agents, loading, range, user }: AgentListProps) {
);

if (!agents.length) return 'No agents found.';
return agents.map((agent: any, index: number) => <AgentRow user={user} agent={agent} key={index} author={agent?.author?.name} />);
return agents.map((agent: any, index: number) => <AgentRow user={user} agent={agent} key={index} author={agent?.author?.name} onDelete={onAgentDelete}/>);
}
4 changes: 3 additions & 1 deletion apps/chat/components/agents/list/AgentRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ export interface AgentListProps {
agent: any
user: any
author: string
onDelete: (agentId: number) => void;
}

export function AgentRow({ agent, user, author }: AgentListProps) {
export function AgentRow({ agent, user, author, onDelete }: AgentListProps) {
const [open, setOpen] = useState(false);

const updateOpenState = () => {
Expand Down Expand Up @@ -43,6 +44,7 @@ export function AgentRow({ agent, user, author }: AgentListProps) {
const handleDelete = async () => {
updateOpenState();
await deleteAgent();
onDelete(agent.id);
};

const handleDeleteClick = () => {
Expand Down
12 changes: 10 additions & 2 deletions apps/chat/components/agents/list/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ export function Agents({ loadmore = false, search = true, range = 5, row = false
fetchData(false, newRangeFrom, newRangeTo);
};

const handleAgentDelete = (deletedAgentId: number) => {
setAgents(agents => agents.filter(agent => agent.id !== deletedAgentId));
};

return (
<>
{search && (
Expand All @@ -127,7 +131,7 @@ export function Agents({ loadmore = false, search = true, range = 5, row = false
)}

<div className={`grid ${row ? 'grid-cols-1' : 'grid-cols-1 md:grid-cols-2 lg:grid-cols-3'} gap-4`}>
<AgentList agents={agents} loading={loading} range={range} user={null} />
<AgentList agents={agents} loading={loading} range={range} user={null} onAgentDelete={handleAgentDelete}/>
</div>
{loadmore && (
<div className='text-center pt-8'>
Expand All @@ -150,10 +154,14 @@ export function UserAgents({ agents = [], user, range = 5, row = false }: UserAg
const [loading, setLoading] = useState(false);
const [searchTerm, setSearchTerm] = useState('');

const handleUserAgentDelete = (deletedAgentId: number) => {
setUserAgents(userAgents => userAgents.filter(agent => agent.id !== deletedAgentId));
};

return (
<>
<div className={`grid ${row ? 'grid-cols-1' : 'grid-cols-1 md:grid-cols-2'} gap-4`}>
<AgentList agents={userAgents} loading={loading} range={range} user={user} />
<AgentList agents={userAgents} loading={loading} range={range} user={user} onAgentDelete={handleUserAgentDelete} />
</div>
</>
);
Expand Down
Loading