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 | /** * CohortTable Component * * Displays customer cohort retention data in a heatmap-style table. */ "use client"; import { cn } from "@/lib/core"; export interface CohortData { cohort: string; size: number; retention: number[]; customerLifetimeValue: number; } export interface CohortTableProps { cohorts: CohortData[]; isLoading?: boolean; className?: string; } function getRetentionColor(value: number): string { if (value >= 50) return "bg-green-500 text-white"; if (value >= 30) return "bg-green-400 text-white"; if (value >= 20) return "bg-green-300 text-gray-900"; if (value >= 10) return "bg-yellow-300 text-gray-900"; if (value >= 5) return "bg-yellow-200 text-gray-900"; if (value > 0) return "bg-red-200 text-gray-900"; return "bg-gray-100 dark:bg-gray-800 text-gray-400"; } function formatCurrency(value: number): string { return new Intl.NumberFormat("en-US", { style: "currency", currency: "USD", minimumFractionDigits: 0, maximumFractionDigits: 0, }).format(value); } function formatCohortMonth(cohort: string): string { const [year, month] = cohort.split("-"); const date = new Date(parseInt(year), parseInt(month) - 1); return date.toLocaleDateString("en-US", { month: "short", year: "2-digit" }); } export function CohortTable({ cohorts, isLoading, className }: CohortTableProps) { if (isLoading) { return ( <div className={cn("bg-white dark:bg-gray-900 rounded-lg border border-gray-200 dark:border-gray-800 p-6", className)}> <div className="animate-pulse space-y-4"> <div className="h-6 bg-gray-200 dark:bg-gray-700 rounded w-1/4"></div> <div className="space-y-2"> {[...Array(6)].map((_, i) => ( <div key={i} className="h-10 bg-gray-200 dark:bg-gray-700 rounded"></div> ))} </div> </div> </div> ); } if (!cohorts || cohorts.length === 0) { return ( <div className={cn("bg-white dark:bg-gray-900 rounded-lg border border-gray-200 dark:border-gray-800 p-6", className)}> <h3 className="text-lg font-semibold text-gray-900 dark:text-white mb-4"> Customer Cohorts </h3> <p className="text-gray-500 dark:text-gray-400 text-center py-8"> No cohort data available </p> </div> ); } // Determine max periods across all cohorts const maxPeriods = Math.max(...cohorts.map((c) => c.retention.length), 0); return ( <div className={cn("bg-white dark:bg-gray-900 rounded-lg border border-gray-200 dark:border-gray-800 overflow-hidden", className)}> <div className="p-6 border-b border-gray-200 dark:border-gray-800"> <h3 className="text-lg font-semibold text-gray-900 dark:text-white"> Customer Retention by Cohort </h3> <p className="mt-1 text-sm text-gray-500 dark:text-gray-400"> Percentage of customers who made repeat purchases </p> </div> <div className="overflow-x-auto"> <table className="w-full"> <thead> <tr className="bg-gray-50 dark:bg-gray-800"> <th className="px-4 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider"> Cohort </th> <th className="px-4 py-3 text-right text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider"> Users </th> {[...Array(maxPeriods)].map((_, i) => ( <th key={i} className="px-4 py-3 text-center text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider min-w-[60px]" > M{i} </th> ))} <th className="px-4 py-3 text-right text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider"> CLV </th> </tr> </thead> <tbody className="divide-y divide-gray-200 dark:divide-gray-700"> {cohorts.map((cohort) => ( <tr key={cohort.cohort} className="hover:bg-gray-50 dark:hover:bg-gray-800/50"> <td className="px-4 py-3 whitespace-nowrap text-sm font-medium text-gray-900 dark:text-white"> {formatCohortMonth(cohort.cohort)} </td> <td className="px-4 py-3 whitespace-nowrap text-sm text-gray-500 dark:text-gray-400 text-right"> {cohort.size.toLocaleString()} </td> {[...Array(maxPeriods)].map((_, i) => { const value = cohort.retention[i]; const hasValue = value !== undefined && value !== null; return ( <td key={i} className="px-1 py-1"> {hasValue ? ( <div className={cn( "px-2 py-2 text-center text-xs font-medium rounded", getRetentionColor(value) )} > {value.toFixed(1)}% </div> ) : ( <div className="px-2 py-2 text-center text-xs text-gray-300 dark:text-gray-600"> — </div> )} </td> ); })} <td className="px-4 py-3 whitespace-nowrap text-sm font-medium text-gray-900 dark:text-white text-right"> {formatCurrency(cohort.customerLifetimeValue)} </td> </tr> ))} </tbody> </table> </div> {/* Legend */} <div className="p-4 border-t border-gray-200 dark:border-gray-800 bg-gray-50 dark:bg-gray-800/50"> <div className="flex items-center justify-center gap-4 flex-wrap"> <span className="text-xs text-gray-500 dark:text-gray-400">Retention:</span> <div className="flex items-center gap-2"> <div className="w-4 h-4 rounded bg-red-200"></div> <span className="text-xs text-gray-500 dark:text-gray-400">0-5%</span> </div> <div className="flex items-center gap-2"> <div className="w-4 h-4 rounded bg-yellow-200"></div> <span className="text-xs text-gray-500 dark:text-gray-400">5-10%</span> </div> <div className="flex items-center gap-2"> <div className="w-4 h-4 rounded bg-yellow-300"></div> <span className="text-xs text-gray-500 dark:text-gray-400">10-20%</span> </div> <div className="flex items-center gap-2"> <div className="w-4 h-4 rounded bg-green-300"></div> <span className="text-xs text-gray-500 dark:text-gray-400">20-30%</span> </div> <div className="flex items-center gap-2"> <div className="w-4 h-4 rounded bg-green-400"></div> <span className="text-xs text-gray-500 dark:text-gray-400">30-50%</span> </div> <div className="flex items-center gap-2"> <div className="w-4 h-4 rounded bg-green-500"></div> <span className="text-xs text-gray-500 dark:text-gray-400">50%+</span> </div> </div> </div> </div> ); } export default CohortTable; |