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 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 | 1x 1x 1x 32x 18x 6x 32x 1x 8x 7x 7x 1x 1x 1x 1x 1x 32x 1x 1x | import {
ChevronLeft,
ChevronRight,
FilterX,
RotateCcw,
Search,
SearchX,
} from "lucide-react";
import {Badge} from "@/components/ui/badge/badge";
import {Button} from "@/components/ui/button/button";
import {MeterBar, type MeterTone} from "@/components/ui/meter-bar/meter-bar";
import {Skeleton} from "@/components/ui/skeleton/skeleton";
import {uiStyles} from "@/components/ui/style-primitives";
import {
cn,
formatCurrency,
formatDate,
formatNumber,
formatPercent,
} from "@/lib/utils/utils";
import type {
CustomerFilters,
CustomerPlan,
CustomersResponse,
CustomerStatus,
} from "@/types/dashboard";
/** Props for the customer account table. */
export type CustomerTableProps = {
filters: Required<Pick<CustomerFilters, "page">> &
Pick<CustomerFilters, "plan" | "query" | "status">;
data?: CustomersResponse;
isError: boolean;
isFetching: boolean;
isLoading: boolean;
onChangeFilters: (filters: Partial<CustomerFilters>) => void;
onRetry: () => void;
};
const statusCopy: Record<CustomerStatus, string> = {
active: "Active",
churned: "Churned",
past_due: "Past due",
trial: "Trial",
};
const planCopy: Record<CustomerPlan, string> = {
enterprise: "Enterprise",
pro: "Pro",
scale: "Scale",
starter: "Starter",
};
const statusTone: Record<CustomerStatus, "amber" | "emerald" | "rose" | "slate"> = {
active: "emerald",
churned: "slate",
past_due: "rose",
trial: "amber",
};
function healthTone(value: number): MeterTone {
if (value >= 80) return "emerald";
if (value >= 60) return "amber";
return "rose";
}
function HealthBar({label, value}: { label: string; value: number }) {
return (
<div className="flex min-w-32 items-center gap-3">
<MeterBar
className="flex-1"
label={`${label} health score`}
tone={healthTone(value)}
value={value}
valueText={`${value} out of 100`}
/>
<span aria-hidden="true" className="w-8 text-right text-sm font-medium text-slate-700 dark:text-slate-200">
{value}
</span>
</div>
);
}
function TableSkeleton() {
return (
<div
aria-label="Loading customer accounts"
aria-live="polite"
className="space-y-3 p-4"
role="status"
>
{Array.from({length: 8}).map((_, index) => (
<Skeleton className="h-12 w-full" key={index}/>
))}
</div>
);
}
/** Searchable, paginated customer table with explicit API states. */
export function CustomerTable({
data,
filters,
isError,
isFetching,
isLoading,
onChangeFilters,
onRetry,
}: CustomerTableProps) {
const hasFilters =
Boolean(filters.query) || filters.status !== "all" || filters.plan !== "all";
// This component keeps expected API states explicit: error, loading, empty, and data.
return (
<section
aria-busy={isLoading || isFetching}
aria-labelledby="customer-accounts-title"
className={uiStyles.surface}>
<div className="border-b border-slate-200 p-4 dark:border-slate-800">
<div className="flex flex-col gap-3 xl:flex-row xl:items-center xl:justify-between">
<div>
<h2 className={uiStyles.sectionHeading} id="customer-accounts-title">
Customer accounts
</h2>
<p className={cn("mt-1", uiStyles.subtleText)}>
Search, segment, and page through customer accounts.
</p>
</div>
<div className="flex flex-col gap-2 sm:flex-row">
<label className="relative block sm:w-72">
<span className="sr-only">Search customers</span>
<Search
aria-hidden="true"
className="pointer-events-none absolute left-3 top-1/2 -translate-y-1/2 text-slate-400"
size={16}
/>
<input
className={cn("w-full pl-9 pr-3 placeholder:text-slate-400", uiStyles.field)}
onChange={(event) =>
onChangeFilters({page: 1, query: event.currentTarget.value})
}
placeholder="Search company, owner, region"
value={filters.query ?? ""}
/>
</label>
<label className="sr-only" htmlFor="status-filter">
Status
</label>
<select
className={cn("px-3", uiStyles.field)}
id="status-filter"
onChange={(event) =>
onChangeFilters({
page: 1,
status: event.currentTarget.value as CustomerStatus | "all",
})
}
value={filters.status}
>
<option value="all">All statuses</option>
<option value="active">Active</option>
<option value="trial">Trial</option>
<option value="past_due">Past due</option>
<option value="churned">Churned</option>
</select>
<label className="sr-only" htmlFor="plan-filter">
Plan
</label>
<select
className={cn("px-3", uiStyles.field)}
id="plan-filter"
onChange={(event) =>
onChangeFilters({
page: 1,
plan: event.currentTarget.value as CustomerPlan | "all",
})
}
value={filters.plan}
>
<option value="all">All plans</option>
<option value="starter">Starter</option>
<option value="pro">Pro</option>
<option value="scale">Scale</option>
<option value="enterprise">Enterprise</option>
</select>
<Button
aria-label="Clear filters"
disabled={!hasFilters}
onClick={() => onChangeFilters({page: 1, plan: "all", query: "", status: "all"})}
title="Clear filters"
variant="ghost"
>
<FilterX aria-hidden="true" size={16}/>
Clear
</Button>
</div>
</div>
</div>
{isError ? (
<div className="flex min-h-64 flex-col items-center justify-center p-8 text-center" role="alert">
<SearchX className="text-rose-500" size={32}/>
<h3 className="mt-4 text-base font-semibold text-slate-950 dark:text-white">
Customers failed to load
</h3>
<p className={cn("mt-2 max-w-md", uiStyles.bodyText)}>
Retry the customer data request without losing your active filters.
</p>
<Button className="mt-5" onClick={onRetry} variant="danger">
<RotateCcw aria-hidden="true" size={16}/>
Retry
</Button>
</div>
) : isLoading ? (
<TableSkeleton/>
) : data?.data.length === 0 ? (
<div className="flex min-h-64 flex-col items-center justify-center p-8 text-center" role="status">
<SearchX className="text-slate-400" size={34}/>
<h3 className="mt-4 text-base font-semibold text-slate-950 dark:text-white">
No customers match these filters
</h3>
<p className={cn("mt-2 max-w-md", uiStyles.bodyText)}>
Adjust the search term, status, or plan to widen the result set.
</p>
<Button
className="mt-5"
onClick={() => onChangeFilters({page: 1, plan: "all", query: "", status: "all"})}
variant="secondary"
>
<FilterX aria-hidden="true" size={16}/>
Clear filters
</Button>
</div>
) : (
<>
<div className="overflow-x-auto">
<table className="w-full min-w-225 border-collapse text-left">
<caption className="sr-only">
Customer account list with plan, status, recurring revenue, health, usage,
and activity columns.
</caption>
<thead
className="bg-slate-50 text-xs font-semibold uppercase text-slate-500 dark:bg-slate-950/60 dark:text-slate-400">
<tr>
<th className="px-4 py-3">Customer</th>
<th className="px-4 py-3">Plan</th>
<th className="px-4 py-3">Status</th>
<th className="px-4 py-3 text-right">MRR</th>
<th className="px-4 py-3">Health</th>
<th className="px-4 py-3">Usage</th>
<th className="px-4 py-3">Last seen</th>
</tr>
</thead>
<tbody className="divide-y divide-slate-200 dark:divide-slate-800">
{data?.data.map((customer) => (
<tr
className={cn(
"transition-colors hover:bg-slate-50 dark:hover:bg-slate-800/50",
isFetching && "opacity-70",
)}
key={customer.id}
>
<td className="px-4 py-4">
<div className="flex items-center gap-3">
<div
className="flex h-9 w-9 shrink-0 items-center justify-center rounded-md bg-cyan-50 text-sm font-semibold text-cyan-700 dark:bg-cyan-950/40 dark:text-cyan-200">
{customer.company.slice(0, 2).toUpperCase()}
</div>
<div className="min-w-0">
<p className="font-medium text-slate-950 dark:text-white">
{customer.company}
</p>
<p className="text-sm text-slate-500 dark:text-slate-400">
{customer.owner} - {customer.region}
</p>
</div>
</div>
</td>
<td className="px-4 py-4 text-sm text-slate-700 dark:text-slate-200">
{planCopy[customer.plan]}
</td>
<td className="px-4 py-4">
<Badge tone={statusTone[customer.status]}>
{statusCopy[customer.status]}
</Badge>
</td>
<td className="px-4 py-4 text-right text-sm font-semibold text-slate-950 dark:text-white">
{formatCurrency(customer.mrr)}
</td>
<td className="px-4 py-4">
<HealthBar label={customer.company} value={customer.healthScore}/>
</td>
<td className="px-4 py-4 text-sm text-slate-700 dark:text-slate-200">
{formatPercent(customer.usage / 100, true)}
</td>
<td className="px-4 py-4 text-sm text-slate-600 dark:text-slate-300">
{formatDate(customer.lastSeen)}
</td>
</tr>
))}
</tbody>
</table>
</div>
<div
className="flex flex-col gap-3 border-t border-slate-200 p-4 dark:border-slate-800 sm:flex-row sm:items-center sm:justify-between">
<p className="text-sm text-slate-500 dark:text-slate-400">
Showing {formatNumber(data?.data.length ?? 0)} of {formatNumber(data?.total ?? 0)}{" "}
customers
{isFetching ? " - refreshing" : ""}
</p>
<nav aria-label="Customer pagination" className="flex items-center gap-2">
<Button
aria-label="Previous page"
disabled={(data?.page ?? 1) <= 1}
onClick={() => onChangeFilters({page: Math.max((data?.page ?? 1) - 1, 1)})}
size="icon"
title="Previous page"
variant="secondary"
>
<ChevronLeft aria-hidden="true" size={18}/>
</Button>
<span
aria-live="polite"
className="min-w-24 text-center text-sm font-medium text-slate-700 dark:text-slate-200">
Page {data?.page ?? 1} / {data?.totalPages ?? 1}
</span>
<Button
aria-label="Next page"
disabled={(data?.page ?? 1) >= (data?.totalPages ?? 1)}
onClick={() =>
onChangeFilters({
page: Math.min((data?.page ?? 1) + 1, data?.totalPages ?? 1),
})
}
size="icon"
title="Next page"
variant="secondary"
>
<ChevronRight aria-hidden="true" size={18}/>
</Button>
</nav>
</div>
</>
)}
</section>
);
}
|