All files / web/src/components/agents agents-list-view.tsx

99.29% Statements 141/142
89.47% Branches 17/19
100% Functions 6/6
99.29% Lines 141/142

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168    1x 1x   1x 1x 1x           1x 1x 1x 1x 1x 1x 1x   1x   1x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x   1x 12x 12x 12x   12x 12x 1x 1x 2x 1x 1x 12x   12x 1x 1x   12x 1x 1x   12x 12x 12x 12x 12x 12x 12x 12x 12x   12x   12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x   12x 1x 1x 6x 6x 6x 6x 6x 6x 6x 1x 1x 11x 1x 1x 1x 1x   1x 1x 1x 1x 1x   1x   1x   10x 10x 19x 19x 19x 19x 19x   19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x   10x 10x   12x 12x   12x  
'use client';
 
import { botttsNeutral } from '@dicebear/collection';
import { createAvatar } from '@dicebear/core';
import type { Agent } from '@shared/types/data';
import { AgentStatusIndicator } from '@web/components/agents/agent-status-indicator';
import { Button } from '@web/components/ui/button';
import {
  Card,
  CardDescription,
  CardHeader,
  CardTitle,
} from '@web/components/ui/card';
import { Input } from '@web/components/ui/input';
import { PageHeader } from '@web/components/ui/page-header';
import { Skeleton } from '@web/components/ui/skeleton';
import { usePermissiveNavigate } from '@web/hooks/use-permissive-navigate';
import { useAgents } from '@web/providers/agents';
import { PlusIcon, SearchIcon } from 'lucide-react';
import { nanoid } from 'nanoid';
import type { ReactElement } from 'react';
import { useMemo, useState } from 'react';
 
const createAgentAvatar = (agentName: string) => {
  const svg = createAvatar(botttsNeutral, {
    seed: agentName,
    size: 64,
    backgroundColor: [
      '00acc1',
      '039be5',
      '1e88e5',
      '43a047',
      '546e7a',
      '5e35b1',
      '6d4c41',
      '757575',
      '7cb342',
      '8e24aa',
      'c0ca33',
      'd81b60',
      'e53935',
      'f4511e',
      'fb8c00',
      'fdd835',
      'ffb300',
      '00897b',
      '3949ab',
    ],
  }).toString();
  return `data:image/svg+xml,${encodeURIComponent(svg)}`;
};
 
export function AgentsListView(): ReactElement {
  const navigate = usePermissiveNavigate();
  const [searchQuery, setSearchQuery] = useState('');
  const { agents, isLoading } = useAgents();
 
  const filteredAgents = useMemo(() => {
    if (!searchQuery) return agents;
    return agents.filter(
      (agent) =>
        agent.name.toLowerCase().includes(searchQuery.toLowerCase()) ||
        agent.description?.toLowerCase().includes(searchQuery.toLowerCase()),
    );
  }, [agents, searchQuery]);
 
  const handleAgentSelect = (agent: Agent) => {
    navigate({ to: `/agents/${encodeURIComponent(agent.name)}` });
  };
 
  const handleCreateAgent = () => {
    navigate({ to: '/agents/create' });
  };
 
  return (
    <>
      <PageHeader
        title="Agents"
        description="Manage your AI agents"
        showBackButton={false}
        actions={
          <Button onClick={handleCreateAgent}>
            <PlusIcon className="h-4 w-4 mr-2" />
            Create Agent
          </Button>
        }
      />
      <div className="p-6 space-y-6">
        <div className="relative">
          <SearchIcon className="absolute left-3 top-3 h-4 w-4 text-muted-foreground" />
          <Input
            placeholder="Search agents..."
            value={searchQuery}
            onChange={(e) => setSearchQuery(e.target.value)}
            className="pl-10"
          />
        </div>
 
        {isLoading ? (
          <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 3xl:grid-cols-5 gap-4">
            {Array.from({ length: 6 }).map(() => (
              <Card key={nanoid()}>
                <CardHeader>
                  <Skeleton className="h-12 w-12 rounded-lg" />
                  <Skeleton className="h-6 w-3/4" />
                  <Skeleton className="h-4 w-full" />
                </CardHeader>
              </Card>
            ))}
          </div>
        ) : filteredAgents.length === 0 ? (
          <div className="text-center py-12">
            <h3 className="text-lg font-semibold mb-2">No agents found</h3>
            <p className="text-muted-foreground mb-4">
              {searchQuery
                ? 'No agents match your search criteria.'
                : "You don't have any agents yet."}
            </p>
            {!searchQuery && (
              <Button onClick={handleCreateAgent}>
                <PlusIcon className="h-4 w-4 mr-2" />
                Create your first agent
              </Button>
            )}
          </div>
        ) : (
          <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 3xl:grid-cols-5 gap-4">
            {filteredAgents.map((agent) => {
              return (
                <Card
                  key={agent.id}
                  className="cursor-pointer hover:shadow-lg hover:border-primary/50 transition-all"
                  onClick={() => handleAgentSelect(agent)}
                >
                  <CardHeader className="pb-3">
                    <div className="flex items-start gap-3 mb-2">
                      <img
                        src={createAgentAvatar(agent.name)}
                        alt={`${agent.name} avatar`}
                        width={48}
                        height={48}
                        className="rounded-lg shrink-0"
                      />
                      <div className="flex-1 min-w-0">
                        <CardTitle className="text-base truncate leading-normal mb-1">
                          {agent.name}
                        </CardTitle>
                        <AgentStatusIndicator
                          agent={agent}
                          variant="badge"
                          tooltipSide="top"
                        />
                      </div>
                    </div>
                    <CardDescription className="line-clamp-3 text-sm">
                      {agent.description || 'No description available'}
                    </CardDescription>
                  </CardHeader>
                </Card>
              );
            })}
          </div>
        )}
      </div>
    </>
  );
}