'use client' import { useState } from 'react' import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card' import { Button } from '@/components/ui/button' import { Badge } from '@/components/ui/badge' import { Progress } from '@/components/ui/progress' import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from '@/components/ui/select' import { Users, Send, CheckCircle2, AlertCircle, Loader2, } from 'lucide-react' import { trpc } from '@/lib/trpc/client' import { toast } from 'sonner' const categoryLabels: Record = { STARTUP: 'Startup', BUSINESS_CONCEPT: 'Business Concept', UNKNOWN: 'Unknown', } type RoundUserTrackerProps = { editionId: string } export function RoundUserTracker({ editionId }: RoundUserTrackerProps) { const [selectedRoundId, setSelectedRoundId] = useState(undefined) const { data, isLoading } = trpc.dashboard.getRoundUserStats.useQuery( { editionId, roundId: selectedRoundId }, { enabled: !!editionId, refetchInterval: 120_000 } ) const utils = trpc.useUtils() const sendReminders = trpc.dashboard.sendAccountReminders.useMutation({ onSuccess: (result) => { toast.success(`Sent ${result.sent} reminder${result.sent !== 1 ? 's' : ''}${result.failed > 0 ? `, ${result.failed} failed` : ''}`) utils.dashboard.getRoundUserStats.invalidate() }, onError: (err) => { toast.error(`Failed to send reminders: ${err.message}`) }, }) const [sendingTarget, setSendingTarget] = useState(null) if (isLoading || !data) return null const { rounds, byCategory } = data const effectiveRoundId = data.selectedRoundId // Don't render if no rounds at all if (rounds.length === 0) return null const totalProjects = byCategory.reduce((sum, c) => sum + c.total, 0) const totalActivated = byCategory.reduce((sum, c) => sum + c.accountsSet, 0) const totalPending = byCategory.reduce((sum, c) => sum + c.accountsNotSet, 0) const selectedRound = effectiveRoundId ? rounds.find(r => r.id === effectiveRoundId) : undefined const handleSendReminder = async (target: string, opts: { category?: 'STARTUP' | 'BUSINESS_CONCEPT' }) => { setSendingTarget(target) try { await sendReminders.mutateAsync({ editionId, roundId: effectiveRoundId!, category: opts.category, }) } finally { setSendingTarget(null) } } return (
Round User Tracker {totalProjects > 0 && ( {totalActivated}/{totalProjects} activated )}
{/* Round selector */}
{totalProjects === 0 ? (

No projects have passed {selectedRound?.name ?? 'this round'} yet

) : ( <> {/* Subtitle showing round context */}

Projects that passed {selectedRound?.name ?? 'this round'} — account activation status

{/* Per-category rows */} {byCategory.map((cat) => { const pct = cat.total > 0 ? Math.round((cat.accountsSet / cat.total) * 100) : 0 const allSet = cat.accountsNotSet === 0 return (
{categoryLabels[cat.category] || cat.category}
{cat.accountsSet} of {cat.total} activated {allSet ? ( ) : ( )}
{cat.accountsNotSet > 0 && (

{cat.accountsNotSet} pending account setup

)}
) })} {/* Summary */}
Total: {totalActivated} of {totalProjects} activated {totalPending > 0 && ( )}
)}
) }