|
| 1 | +import { Head, Link } from '@inertiajs/react'; |
| 2 | +import AppLayout from '@/Layouts/AppLayout'; |
| 3 | +import type { PageProps } from '@/types'; |
| 4 | + |
| 5 | +interface Stats { |
| 6 | + totalLeads: number; |
| 7 | + totalOpportunities: number; |
| 8 | + totalExpectedRevenue: number; |
| 9 | + wonThisMonth: number; |
| 10 | + wonRevenueThisMonth: number; |
| 11 | + conversionRate: number; |
| 12 | +} |
| 13 | + |
| 14 | +interface PipelineStage { |
| 15 | + id: number; |
| 16 | + name: string; |
| 17 | + color: string | null; |
| 18 | + lead_count: number; |
| 19 | + expected_revenue_sum: number | null; |
| 20 | +} |
| 21 | + |
| 22 | +interface RecentLead { |
| 23 | + id: number; |
| 24 | + reference: string | null; |
| 25 | + title: string; |
| 26 | + status: string; |
| 27 | + priority: string; |
| 28 | + expected_close_date: string | null; |
| 29 | + stage: { id: number; name: string } | null; |
| 30 | + assignee: { id: number; name: string } | null; |
| 31 | +} |
| 32 | + |
| 33 | +interface Props extends PageProps { |
| 34 | + stats: Stats; |
| 35 | + pipelineByStage: PipelineStage[]; |
| 36 | + recentLeads: RecentLead[]; |
| 37 | +} |
| 38 | + |
| 39 | +const priorityBadge: Record<string, string> = { |
| 40 | + low: 'bg-slate-100 text-slate-700', |
| 41 | + normal: 'bg-blue-100 text-blue-700', |
| 42 | + high: 'bg-orange-100 text-orange-700', |
| 43 | + urgent: 'bg-red-100 text-red-700', |
| 44 | +}; |
| 45 | + |
| 46 | +const statusBadge: Record<string, string> = { |
| 47 | + open: 'bg-blue-100 text-blue-700', |
| 48 | + won: 'bg-green-100 text-green-700', |
| 49 | + lost: 'bg-red-100 text-red-700', |
| 50 | +}; |
| 51 | + |
| 52 | +function fmt(n: number): string { |
| 53 | + return new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD', maximumFractionDigits: 0 }).format(n); |
| 54 | +} |
| 55 | + |
| 56 | +export default function CrmDashboard({ stats, pipelineByStage, recentLeads }: Props) { |
| 57 | + const kpis = [ |
| 58 | + { label: 'Open Leads', value: stats.totalLeads, color: 'text-blue-600' }, |
| 59 | + { label: 'Open Opportunities', value: stats.totalOpportunities, color: 'text-indigo-600' }, |
| 60 | + { label: 'Expected Revenue', value: fmt(stats.totalExpectedRevenue), color: 'text-emerald-600' }, |
| 61 | + { label: 'Won This Month', value: stats.wonThisMonth, color: 'text-green-600' }, |
| 62 | + { label: 'Won Revenue/Month', value: fmt(stats.wonRevenueThisMonth), color: 'text-teal-600' }, |
| 63 | + { label: 'Conversion Rate', value: `${stats.conversionRate}%`, color: 'text-purple-600' }, |
| 64 | + ]; |
| 65 | + |
| 66 | + return ( |
| 67 | + <AppLayout> |
| 68 | + <Head title="CRM Dashboard" /> |
| 69 | + <div className="space-y-6"> |
| 70 | + <h1 className="text-2xl font-semibold text-slate-900">CRM Dashboard</h1> |
| 71 | + |
| 72 | + {/* KPI Cards */} |
| 73 | + <div className="grid grid-cols-2 gap-4 sm:grid-cols-3 lg:grid-cols-6"> |
| 74 | + {kpis.map((kpi) => ( |
| 75 | + <div key={kpi.label} className="rounded-lg border border-slate-200 bg-white p-4 shadow-sm"> |
| 76 | + <p className="text-xs font-medium uppercase text-slate-500">{kpi.label}</p> |
| 77 | + <p className={`mt-2 text-xl font-bold ${kpi.color}`}>{kpi.value}</p> |
| 78 | + </div> |
| 79 | + ))} |
| 80 | + </div> |
| 81 | + |
| 82 | + <div className="grid grid-cols-1 gap-6 lg:grid-cols-2"> |
| 83 | + {/* Pipeline by Stage */} |
| 84 | + <div className="rounded-lg border border-slate-200 bg-white shadow-sm"> |
| 85 | + <div className="border-b border-slate-200 px-6 py-4"> |
| 86 | + <h2 className="text-base font-semibold text-slate-800">Pipeline by Stage</h2> |
| 87 | + </div> |
| 88 | + <div className="overflow-x-auto"> |
| 89 | + <table className="min-w-full divide-y divide-slate-200"> |
| 90 | + <thead className="bg-slate-50"> |
| 91 | + <tr> |
| 92 | + <th className="px-4 py-3 text-left text-xs font-medium uppercase text-slate-500">Stage</th> |
| 93 | + <th className="px-4 py-3 text-right text-xs font-medium uppercase text-slate-500">Leads</th> |
| 94 | + <th className="px-4 py-3 text-right text-xs font-medium uppercase text-slate-500">Expected Rev.</th> |
| 95 | + </tr> |
| 96 | + </thead> |
| 97 | + <tbody className="divide-y divide-slate-100"> |
| 98 | + {pipelineByStage.map((stage) => ( |
| 99 | + <tr key={stage.id} className="hover:bg-slate-50"> |
| 100 | + <td className="px-4 py-3 text-sm text-slate-800 flex items-center gap-2"> |
| 101 | + {stage.color && ( |
| 102 | + <span |
| 103 | + className="inline-block h-3 w-3 rounded-full flex-shrink-0" |
| 104 | + style={{ backgroundColor: stage.color }} |
| 105 | + /> |
| 106 | + )} |
| 107 | + {stage.name} |
| 108 | + </td> |
| 109 | + <td className="px-4 py-3 text-right text-sm text-slate-700">{stage.lead_count}</td> |
| 110 | + <td className="px-4 py-3 text-right text-sm text-slate-700"> |
| 111 | + {fmt(stage.expected_revenue_sum ?? 0)} |
| 112 | + </td> |
| 113 | + </tr> |
| 114 | + ))} |
| 115 | + {pipelineByStage.length === 0 && ( |
| 116 | + <tr> |
| 117 | + <td colSpan={3} className="px-4 py-8 text-center text-sm text-slate-400">No stages configured</td> |
| 118 | + </tr> |
| 119 | + )} |
| 120 | + </tbody> |
| 121 | + </table> |
| 122 | + </div> |
| 123 | + </div> |
| 124 | + |
| 125 | + {/* Recent Leads */} |
| 126 | + <div className="rounded-lg border border-slate-200 bg-white shadow-sm"> |
| 127 | + <div className="border-b border-slate-200 px-6 py-4 flex items-center justify-between"> |
| 128 | + <h2 className="text-base font-semibold text-slate-800">Recent Leads</h2> |
| 129 | + <Link href="/crm/leads" className="text-sm text-indigo-600 hover:text-indigo-800">View all</Link> |
| 130 | + </div> |
| 131 | + <div className="overflow-x-auto"> |
| 132 | + <table className="min-w-full divide-y divide-slate-200"> |
| 133 | + <thead className="bg-slate-50"> |
| 134 | + <tr> |
| 135 | + <th className="px-4 py-3 text-left text-xs font-medium uppercase text-slate-500">Reference</th> |
| 136 | + <th className="px-4 py-3 text-left text-xs font-medium uppercase text-slate-500">Title</th> |
| 137 | + <th className="px-4 py-3 text-left text-xs font-medium uppercase text-slate-500">Stage</th> |
| 138 | + <th className="px-4 py-3 text-left text-xs font-medium uppercase text-slate-500">Priority</th> |
| 139 | + <th className="px-4 py-3 text-left text-xs font-medium uppercase text-slate-500">Close Date</th> |
| 140 | + </tr> |
| 141 | + </thead> |
| 142 | + <tbody className="divide-y divide-slate-100"> |
| 143 | + {recentLeads.map((lead) => ( |
| 144 | + <tr key={lead.id} className="hover:bg-slate-50"> |
| 145 | + <td className="px-4 py-3 text-sm"> |
| 146 | + <Link href={`/crm/leads/${lead.id}`} className="text-indigo-600 hover:text-indigo-800 font-mono text-xs"> |
| 147 | + {lead.reference ?? `#${lead.id}`} |
| 148 | + </Link> |
| 149 | + </td> |
| 150 | + <td className="px-4 py-3 text-sm text-slate-800 max-w-[140px] truncate">{lead.title}</td> |
| 151 | + <td className="px-4 py-3 text-sm text-slate-600">{lead.stage?.name ?? '—'}</td> |
| 152 | + <td className="px-4 py-3"> |
| 153 | + <span className={`inline-block rounded px-2 py-0.5 text-xs font-medium ${priorityBadge[lead.priority] ?? priorityBadge.normal}`}> |
| 154 | + {lead.priority} |
| 155 | + </span> |
| 156 | + </td> |
| 157 | + <td className="px-4 py-3 text-sm text-slate-600">{lead.expected_close_date ?? '—'}</td> |
| 158 | + </tr> |
| 159 | + ))} |
| 160 | + {recentLeads.length === 0 && ( |
| 161 | + <tr> |
| 162 | + <td colSpan={5} className="px-4 py-8 text-center text-sm text-slate-400">No leads yet</td> |
| 163 | + </tr> |
| 164 | + )} |
| 165 | + </tbody> |
| 166 | + </table> |
| 167 | + </div> |
| 168 | + </div> |
| 169 | + </div> |
| 170 | + </div> |
| 171 | + </AppLayout> |
| 172 | + ); |
| 173 | +} |
0 commit comments