All files / src/components TeamList.tsx

0% Statements 0/228
0% Branches 0/1
0% Functions 0/1
0% Lines 0/228

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 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
/**
 * TeamList - Sidebar Team List Component
 *
 * Displays a compact list of teams for the sidebar navigation.
 *
 * @module components/TeamList
 */
 
import { useEffect, useState } from 'react';
import { useTeamStore } from '../store/teamStore';
import { useAgentStore } from '../store/agentStore';
import { useChatStore } from '../store/chatStore';
import { Users, Plus, Activity, CheckCircle, AlertTriangle, X, Bot } from 'lucide-react';
import type { TeamMemberRole } from '../types/team';
 
interface TeamListProps {
  onSelectTeam?: (teamId: string) => void;
  selectedTeamId?: string;
}
 
export function TeamList({ onSelectTeam, selectedTeamId }: TeamListProps) {
  const { teams, loadTeams, setActiveTeam, createTeam, isLoading } = useTeamStore();
  const clones = useAgentStore((s) => s.clones);
  const { agents } = useChatStore();
  const [showCreateModal, setShowCreateModal] = useState(false);
  const [teamName, setTeamName] = useState('');
  const [teamDescription, setTeamDescription] = useState('');
  const [teamPattern, setTeamPattern] = useState<'sequential' | 'parallel' | 'pipeline'>('sequential');
  const [selectedAgents, setSelectedAgents] = useState<string[]>([]);
  const [isCreating, setIsCreating] = useState(false);
 
  useEffect(() => {
    try {
      loadTeams();
    } catch (err) {
      console.error('[TeamList] Failed to load teams:', err);
    }
  }, [loadTeams]);
 
  const handleSelectTeam = (teamId: string) => {
    const team = teams.find(t => t.id === teamId);
    if (team) {
      setActiveTeam(team);
      onSelectTeam?.(teamId);
    }
  };
 
  const handleCreateTeam = async () => {
    if (!teamName.trim() || selectedAgents.length === 0) return;
 
    setIsCreating(true);
    try {
      const roleAssignments: { agentId: string; role: TeamMemberRole }[] = selectedAgents.map((agentId, index) => ({
        agentId,
        role: (index === 0 ? 'orchestrator' : index === 1 ? 'reviewer' : 'worker') as TeamMemberRole,
      }));
 
      const team = await createTeam({
        name: teamName.trim(),
        description: teamDescription.trim() || undefined,
        pattern: teamPattern,
        memberAgents: roleAssignments,
      });
 
      if (team) {
        setShowCreateModal(false);
        setTeamName('');
        setTeamDescription('');
        setSelectedAgents([]);
        setTeamPattern('sequential');
        setActiveTeam(team);
        onSelectTeam?.(team.id);
      }
    } finally {
      setIsCreating(false);
    }
  };
 
  const toggleAgentSelection = (agentId: string) => {
    setSelectedAgents(prev =>
      prev.includes(agentId)
        ? prev.filter(id => id !== agentId)
        : [...prev, agentId]
    );
  };
 
  const getStatusIcon = (status: string) => {
    switch (status) {
      case 'active':
        return <Activity className="w-3 h-3 text-green-500" />;
      case 'paused':
        return <AlertTriangle className="w-3 h-3 text-yellow-500" />;
      case 'completed':
        return <CheckCircle className="w-3 h-3 text-blue-500" />;
      default:
        return <Activity className="w-3 h-3 text-gray-400" />;
    }
  };
 
  // Merge clones and agents for display - normalize to common type with defensive checks
  const availableAgents: Array<{ id: string; name: string; role?: string }> =
    (clones && clones.length > 0)
      ? clones.map(c => ({ id: c.id, name: c.name, role: c.role }))
      : (agents && agents.length > 0)
        ? agents.map(a => ({
            id: a.id,
            name: a.name,
            role: '默认助手',
          }))
        : [];
 
  return (
    <div className="h-full flex flex-col">
      {/* Header */}
      <div className="p-3 border-b border-gray-200 dark:border-gray-700">
        <div className="flex items-center justify-between">
          <h3 className="text-xs font-semibold text-gray-500 dark:text-gray-400 uppercase tracking-wider">
            团队
          </h3>
          <button
            onClick={() => setShowCreateModal(true)}
            className="p-1 hover:bg-gray-100 dark:hover:bg-gray-800 rounded transition-colors"
            title="创建团队"
          >
            <Plus className="w-4 h-4 text-gray-400 hover:text-gray-600 dark:hover:text-gray-200" />
          </button>
        </div>
      </div>
 
      {/* Create Team Modal */}
      {showCreateModal && (
        <div className="absolute inset-0 bg-black/50 flex items-center justify-center z-50">
          <div className="bg-white dark:bg-gray-800 rounded-xl shadow-xl w-80 max-h-[90vh] overflow-y-auto">
            <div className="p-4 border-b border-gray-200 dark:border-gray-700">
              <div className="flex items-center justify-between">
                <h3 className="text-sm font-semibold text-gray-900 dark:text-white">创建团队</h3>
                <button
                  onClick={() => setShowCreateModal(false)}
                  className="p-1 hover:bg-gray-100 dark:hover:bg-gray-700 rounded"
                >
                  <X className="w-4 h-4 text-gray-400" />
                </button>
              </div>
            </div>
 
            <div className="p-4 space-y-4">
              {/* Team Name */}
              <div>
                <label className="block text-xs font-medium text-gray-700 dark:text-gray-300 mb-1">
                  团队名称 *
                </label>
                <input
                  type="text"
                  value={teamName}
                  onChange={(e) => setTeamName(e.target.value)}
                  placeholder="例如:开发团队 Alpha"
                  className="w-full px-3 py-2 text-sm border border-gray-200 dark:border-gray-600 rounded-lg bg-white dark:bg-gray-700 text-gray-900 dark:text-white focus:outline-none focus:ring-2 focus:ring-gray-400"
                />
              </div>
 
              {/* Team Description */}
              <div>
                <label className="block text-xs font-medium text-gray-700 dark:text-gray-300 mb-1">
                  描述
                </label>
                <textarea
                  value={teamDescription}
                  onChange={(e) => setTeamDescription(e.target.value)}
                  placeholder="这个团队将负责什么工作?"
                  rows={2}
                  className="w-full px-3 py-2 text-sm border border-gray-200 dark:border-gray-600 rounded-lg bg-white dark:bg-gray-700 text-gray-900 dark:text-white focus:outline-none focus:ring-2 focus:ring-gray-400 resize-none"
                />
              </div>
 
              {/* Collaboration Pattern */}
              <div>
                <label className="block text-xs font-medium text-gray-700 dark:text-gray-300 mb-1">
                  协作模式
                </label>
                <select
                  value={teamPattern}
                  onChange={(e) => setTeamPattern(e.target.value as typeof teamPattern)}
                  className="w-full px-3 py-2 text-sm border border-gray-200 dark:border-gray-600 rounded-lg bg-white dark:bg-gray-700 text-gray-900 dark:text-white focus:outline-none focus:ring-2 focus:ring-gray-400"
                >
                  <option value="sequential">顺序执行(逐个任务)</option>
                  <option value="parallel">并行执行(同时工作)</option>
                  <option value="pipeline">流水线(输出传递给下一步)</option>
                </select>
              </div>
 
              {/* Agent Selection */}
              <div>
                <label className="block text-xs font-medium text-gray-700 dark:text-gray-300 mb-2">
                  选择智能体 (已选择 {selectedAgents.length} 个) *
                </label>
                <div className="space-y-2 max-h-40 overflow-y-auto">
                  {availableAgents.map((agent) => (
                    <button
                      key={agent.id}
                      onClick={() => toggleAgentSelection(agent.id)}
                      className={`w-full p-2 rounded-lg text-left text-sm transition-colors flex items-center gap-2 ${
                        selectedAgents.includes(agent.id)
                          ? 'bg-blue-50 dark:bg-blue-900/30 border border-blue-200 dark:border-blue-800'
                          : 'bg-gray-50 dark:bg-gray-700 border border-transparent hover:bg-gray-100 dark:hover:bg-gray-600'
                      }`}
                    >
                      <div className="w-6 h-6 rounded-full bg-gray-600 flex items-center justify-center text-white text-xs">
                        <Bot className="w-3 h-3" />
                      </div>
                      <span className="text-gray-900 dark:text-white truncate">{agent.name}</span>
                      {selectedAgents.includes(agent.id) && (
                        <CheckCircle className="w-4 h-4 text-blue-500 ml-auto" />
                      )}
                    </button>
                  ))}
                  {availableAgents.length === 0 && (
                    <p className="text-xs text-gray-500 dark:text-gray-400 text-center py-2">
                      暂无可用智能体,请先创建一个智能体。
                    </p>
                  )}
                </div>
              </div>
            </div>
 
            {/* Footer */}
            <div className="p-4 border-t border-gray-200 dark:border-gray-700 flex gap-2">
              <button
                onClick={() => setShowCreateModal(false)}
                className="flex-1 px-4 py-2 text-sm text-gray-700 dark:text-gray-300 bg-gray-100 dark:bg-gray-700 rounded-lg hover:bg-gray-200 dark:hover:bg-gray-600 transition-colors"
              >
                取消
              </button>
              <button
                onClick={handleCreateTeam}
                disabled={!teamName.trim() || selectedAgents.length === 0 || isCreating}
                className="flex-1 px-4 py-2 text-sm text-white bg-gray-700 dark:bg-gray-600 rounded-lg hover:bg-gray-800 dark:hover:bg-gray-500 disabled:opacity-50 disabled:cursor-not-allowed transition-colors"
              >
                {isCreating ? '创建中...' : '创建'}
              </button>
            </div>
          </div>
        </div>
      )}
 
      {/* Team List */}
      <div className="flex-1 overflow-y-auto">
        {isLoading ? (
          <div className="p-4 text-center text-gray-400 text-sm">加载中...</div>
        ) : !Array.isArray(teams) || teams.length === 0 ? (
          <div className="p-4 text-center">
            <Users className="w-8 h-8 mx-auto mb-2 text-gray-300 dark:text-gray-600" />
            <p className="text-xs text-gray-400 dark:text-gray-500">
              暂无团队
            </p>
            <p className="text-xs text-gray-400 dark:text-gray-500 mt-1">
              点击 + 创建一个团队
            </p>
          </div>
        ) : (
          <div className="space-y-1 p-2">
            {teams.map((team) => (
              <button
                key={team.id}
                onClick={() => handleSelectTeam(team.id)}
                className={`w-full p-2 rounded-lg text-left transition-colors ${
                  selectedTeamId === team.id
                    ? 'bg-blue-50 dark:bg-blue-900/30 border border-blue-200 dark:border-blue-800'
                    : 'hover:bg-gray-100 dark:hover:bg-gray-800'
                }`}
              >
                <div className="flex items-center gap-2">
                  {getStatusIcon(team.status)}
                  <span className="text-sm font-medium text-gray-900 dark:text-white truncate">
                    {team.name}
                  </span>
                </div>
                <div className="mt-1 flex items-center gap-2 text-xs text-gray-500 dark:text-gray-400">
                  <span className="flex items-center gap-1">
                    <Users className="w-3 h-3" />
                    {team.members.length}
                  </span>
                  <span>·</span>
                  <span>{team.tasks.length} 个任务</span>
                </div>
              </button>
            ))}
          </div>
        )}
      </div>
    </div>
  );
}
 
export default TeamList;