import Layout from "@/components/Layout"; import { useAuth } from "@/contexts/AuthContext"; import { useEffect, useMemo, useState } from "react"; import { useNavigate } from "react-router-dom"; import { Card, CardHeader, CardTitle, CardDescription, CardContent, } from "@/components/ui/card"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Textarea } from "@/components/ui/textarea"; import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar"; import { Badge } from "@/components/ui/badge"; import { aethexCollabService } from "@/lib/aethex-collab-service"; import LoadingScreen from "@/components/LoadingScreen"; import { aethexToast } from "@/lib/aethex-toast"; export default function Teams() { const { user, profile, loading } = useAuth(); const navigate = useNavigate(); const [isLoading, setIsLoading] = useState(true); const [teams, setTeams] = useState([]); const [name, setName] = useState(""); const [description, setDescription] = useState(""); const [creating, setCreating] = useState(false); useEffect(() => { if (!loading && !user) navigate("/login", { replace: true }); }, [loading, user, navigate]); useEffect(() => { const load = async () => { if (!user?.id) return; setIsLoading(true); try { const myTeams = await aethexCollabService.listMyTeams(user.id); setTeams(myTeams); } finally { setIsLoading(false); } }; load(); }, [user?.id]); const canCreate = useMemo( () => name.trim().length > 2 && !creating, [name, creating], ); const handleCreate = async () => { if (!user?.id) return; if (!canCreate) return; setCreating(true); const tempId = `temp-${Date.now()}`; const optimistic = { team_id: tempId, teams: { id: tempId, name: name.trim(), description: description.trim() || null, visibility: "private", }, } as any; setTeams((prev) => [optimistic, ...prev]); setName(""); setDescription(""); let created: any | null = null; try { created = await aethexCollabService.createTeam( user.id, optimistic.teams.name, optimistic.teams.description, "private", ); setTeams((prev) => prev.map((t: any) => t.team_id === tempId ? { team_id: created.id, teams: created } : t, ), ); aethexToast.success({ title: "Team created" }); } catch (e: any) { setTeams((prev) => prev.filter((t: any) => t.team_id !== tempId)); aethexToast.error({ title: "Failed to create team", description: e?.message || "Try again later.", }); } finally { setCreating(false); } }; if (loading || isLoading) return ( ); if (!user) return null; return (

Teams

Create a team and collaborate with members across projects.

Your teams Teams you belong to {teams.length === 0 ? (

No teams yet. Create one to get started.

) : ( teams.map((t) => { const team = (t as any).teams || t; return (
{team.name?.[0]?.toUpperCase() || "T"}
{team.name}
{team.visibility || "private"}
Team
); }) )}