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 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 | 1x 1x 1x 1x 1x 24x 20x 3x 1x 1x 1x 1x 8x 1x 17x 17x 17x 17x 1x 3x 3x 2x 2x 1x 1x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 6x 6x 1x 1x 1x 1x 3x 35x 1x 4x 420x 1x 34x 17x 1x 2x | import { Link, useNavigate } from "@tanstack/react-router";
import { Handshake } from "lucide-react";
import { useState } from "react";
import { Button } from "../../../components/ui/button";
import {
Card,
CardContent,
CardHeader,
CardTitle,
} from "../../../components/ui/card";
import { EmptyState } from "../../../components/ui/empty-state";
import { Input } from "../../../components/ui/input";
import { PageHeader } from "../../../components/ui/page-header";
import { StatusBadge } from "../../../components/ui/status-badge";
import { useDebouncedValue } from "../../../hooks";
import {
useAdminPartners,
useAdminReferrals,
} from "../../../hooks/queries/useRrmQueries";
import type {
AdminReferralRow,
ReferralStatus,
} from "../../../lib/api/admin/rrm";
import { cn } from "../../../lib/utils";
// Third consumer of these formatters. They still live in `prospects.tsx`
// because promoting them to a shared module means editing that file, which is
// not this change's to edit — the promotion is now due, though.
import { formatDateTime, formatRelative } from "./prospects";
/**
* Every referral in the partner programme — FR-A-2, `/admin/rrm/referrals`.
*
* Not to be confused with the verification queue: that screen shows the one
* status an operator must act on, this one shows all twelve so a support call
* ("what happened to the lead I sent on Tuesday?") can actually be answered.
*/
const PAGE_SIZE = 25;
// ─── Status vocabulary ───────────────────────────────────────────────────────
/** Ordered as the referral travels, dead ends last. */
export const REFERRAL_STATUS_OPTIONS: readonly ReferralStatus[] = [
"draft",
"forwarded",
"engaged",
"verified",
"contacted",
"quoted",
"converted",
"duplicate",
"rejected",
"expired",
"lost",
"opted_out",
];
/**
* Omitting `status` entirely puts the endpoint into QUEUE mode — engaged only,
* oldest first — which is a different screen. "All statuses" here therefore
* means all twelve, spelled out.
*/
export const ALL_REFERRAL_STATUSES = REFERRAL_STATUS_OPTIONS.join(",");
export const REFERRAL_STATUS_LABELS: Record<ReferralStatus, string> = {
draft: "Draft",
forwarded: "Sent to us",
engaged: "Awaiting check",
verified: "Verified",
contacted: "We called them",
quoted: "Quoted",
converted: "Won",
duplicate: "Duplicate",
rejected: "Rejected",
expired: "Expired",
lost: "Lost",
opted_out: "Opted out",
};
type StatusVariant = "default" | "info" | "warning" | "success" | "error";
/**
* `StatusBadge.getVariantForStatus` reads the label, not the state, and would
* colour "Draft" and "Expired" as warnings — so the variant is always explicit.
*
* Green is money: verified is the ₹100 credit, converted is the payout. Blue is
* live work. Grey is a dead end nobody owes anything for. Red is the one state
* the partner was told the reason for.
*/
const REFERRAL_STATUS_VARIANTS: Record<ReferralStatus, StatusVariant> = {
draft: "default",
forwarded: "info",
engaged: "warning",
verified: "success",
contacted: "info",
quoted: "info",
converted: "success",
duplicate: "default",
rejected: "error",
expired: "default",
lost: "default",
opted_out: "default",
};
export function ReferralStatusBadge({ status }: { status: ReferralStatus }) {
return (
<span data-testid="rrm-referral-status" data-status={status}>
<StatusBadge
status={REFERRAL_STATUS_LABELS[status]}
variant={REFERRAL_STATUS_VARIANTS[status]}
// Verified is the moment ₹100 leaves the programme. In a page of
// same-shaped pills it needs to be findable at a glance.
className={cn(
status === "verified" && "font-semibold ring-1 ring-success",
)}
/>
</span>
);
}
/** `mode` is a free string on the wire; only two values are ever sent. */
export function modeLabel(mode: string): string {
if (mode === "direct") return "Told us directly";
if (mode === "link") return "Shared link";
return mode;
}
// ─── Table pieces ────────────────────────────────────────────────────────────
const COLUMN_COUNT = 6;
const SKELETON_KEYS = ["a", "b", "c", "d", "e", "f", "g", "h"];
function SkeletonRows() {
return (
<>
{SKELETON_KEYS.map((key) => (
<tr key={key} data-testid="rrm-referrals-skeleton-row">
<td colSpan={COLUMN_COUNT} className="px-4 py-3">
<div className="h-5 bg-background-muted rounded animate-pulse" />
</td>
</tr>
))}
</>
);
}
const HEADER_CELL =
"px-4 py-3 text-left text-xs font-medium text-foreground-muted uppercase tracking-wider";
function ReferralRow({
referral,
onOpen,
}: {
referral: AdminReferralRow;
onOpen: (id: string) => void;
}) {
const href = `/admin/rrm/referrals/${referral.id}`;
const partner = referral.partnerName ?? "Unnamed partner";
const society = referral.society ?? "—";
return (
<tr
data-testid="rrm-referral-row"
data-referral-id={referral.id}
tabIndex={0}
className="cursor-pointer hover:bg-background-muted focus-visible:outline-none focus-visible:bg-background-muted transition-colors"
onClick={() => onOpen(referral.id)}
onKeyDown={(e) => {
// The name is a real link and handles its own Enter — only the row
// itself should open the referral.
Iif (e.target !== e.currentTarget) return;
if (e.key === "Enter" || e.key === " ") {
e.preventDefault();
onOpen(referral.id);
}
}}
>
<td className="px-4 py-3">
<Link
to={href}
className="text-sm font-medium text-foreground-default hover:underline"
onClick={(e) => e.stopPropagation()}
>
{referral.contactName}
</Link>
<p className="text-xs text-foreground-muted">
{referral.contactPhoneNorm}
</p>
<p className="text-xs text-foreground-subtle">{referral.code}</p>
{/* The columns hidden on narrow screens fold into this cell instead. */}
<p className="text-xs text-foreground-subtle md:hidden">
{society} · {partner}
</p>
</td>
<td className="hidden md:table-cell px-4 py-3 text-sm text-foreground-muted max-w-xs truncate">
{society}
</td>
<td className="hidden md:table-cell px-4 py-3 text-sm text-foreground-muted max-w-xs truncate">
{partner}
</td>
<td className="px-4 py-3">
<ReferralStatusBadge status={referral.status} />
</td>
<td className="hidden lg:table-cell px-4 py-3 text-sm text-foreground-muted whitespace-nowrap">
{modeLabel(referral.mode)}
</td>
<td
className="hidden sm:table-cell px-4 py-3 text-xs text-foreground-muted whitespace-nowrap"
data-testid="rrm-referral-created"
>
<span title={formatDateTime(referral.dateCreated)}>
{formatRelative(referral.dateCreated)}
</span>
</td>
</tr>
);
}
const SELECT_CLASS =
"h-10 rounded-md border border-border-default bg-background-base px-3 py-2 text-sm text-foreground-default focus:outline-none focus:ring-2 focus:ring-primary-500";
// ─── Page ────────────────────────────────────────────────────────────────────
export function ReferralsPage() {
const navigate = useNavigate();
const [search, setSearch] = useState("");
const [status, setStatus] = useState("");
const [partnerId, setPartnerId] = useState("");
const [offset, setOffset] = useState(0);
const debouncedSearch = useDebouncedValue(search);
const { data, isLoading, error } = useAdminReferrals({
status: status || ALL_REFERRAL_STATUSES,
partnerId: partnerId || undefined,
search: debouncedSearch || undefined,
limit: PAGE_SIZE,
offset,
});
/**
* The partner select is a convenience, not a dependency: if this fails or is
* still in flight the table renders anyway with "All partners" as the only
* option. 200 covers the whole programme for now — see FR-A-2.
*/
const partnersQuery = useAdminPartners({ limit: 200 });
const partners = partnersQuery.data?.data?.partners ?? [];
const items = data?.data?.referrals ?? [];
const total = data?.data?.total;
const hasPrev = offset > 0;
const hasNext =
total !== undefined
? offset + PAGE_SIZE < total
: items.length === PAGE_SIZE;
const countLabel =
total === undefined
? `${items.length} referral${items.length === 1 ? "" : "s"} on this page`
: `${total.toLocaleString("en-IN")} referral${total === 1 ? "" : "s"}`;
const hasFilters = Boolean(search || status || partnerId);
function applyFilter(setter: (value: string) => void, value: string) {
setter(value);
setOffset(0);
}
function clearFilters() {
setSearch("");
// Back to all twelve, never to "no status" — that would silently switch
// the endpoint into queue mode.
setStatus("");
setPartnerId("");
setOffset(0);
}
function openReferral(id: string) {
navigate({ to: `/admin/rrm/referrals/${id}` });
}
return (
<div className="space-y-6" data-testid="rrm-referrals-page">
<PageHeader
title="Referrals"
description="Every lead a partner has sent us, whatever became of it."
/>
<Card>
<CardContent className="pt-6">
<div className="flex flex-col sm:flex-row flex-wrap gap-3">
<div className="sm:w-72">
<Input
type="search"
aria-label="Search referrals"
placeholder="Name, phone, society or code"
data-testid="rrm-referrals-search"
value={search}
onChange={(e) => applyFilter(setSearch, e.target.value)}
/>
</div>
<select
aria-label="Filter by status"
data-testid="rrm-referrals-status-filter"
className={SELECT_CLASS}
value={status}
onChange={(e) => applyFilter(setStatus, e.target.value)}
>
<option value="">All statuses</option>
{REFERRAL_STATUS_OPTIONS.map((value) => (
<option key={value} value={value}>
{REFERRAL_STATUS_LABELS[value]}
</option>
))}
</select>
<select
aria-label="Filter by partner"
data-testid="rrm-referrals-partner-filter"
className={SELECT_CLASS}
value={partnerId}
onChange={(e) => applyFilter(setPartnerId, e.target.value)}
>
<option value="">All partners</option>
{partners.map((partner) => (
<option key={partner.id} value={partner.id}>
{partner.name}
{partner.firmName ? ` · ${partner.firmName}` : ""}
</option>
))}
</select>
{hasFilters && (
<Button
variant="outline"
data-testid="rrm-referrals-clear-filters"
onClick={clearFilters}
>
Clear
</Button>
)}
</div>
</CardContent>
</Card>
<Card>
<CardHeader>
<CardTitle className="text-lg" data-testid="rrm-referrals-total">
{isLoading ? "Loading referrals…" : countLabel}
</CardTitle>
</CardHeader>
<CardContent className="p-0">
{error ? (
<div
data-testid="rrm-referrals-error"
className="p-6 text-sm text-error"
>
Could not load referrals. Refresh the page to try again.
</div>
) : (
<div className="overflow-x-auto">
<table
className="w-full text-sm"
data-testid="rrm-referrals-table"
>
<thead className="bg-background-subtle border-b border-border-default">
<tr>
<th className={HEADER_CELL}>Contact</th>
<th className={cn(HEADER_CELL, "hidden md:table-cell")}>
Society
</th>
<th className={cn(HEADER_CELL, "hidden md:table-cell")}>
Partner
</th>
<th className={HEADER_CELL}>Status</th>
<th className={cn(HEADER_CELL, "hidden lg:table-cell")}>
How it reached us
</th>
<th className={cn(HEADER_CELL, "hidden sm:table-cell")}>
Created
</th>
</tr>
</thead>
<tbody className="divide-y divide-border-default">
{isLoading ? (
<SkeletonRows />
) : items.length === 0 ? (
<tr>
<td colSpan={COLUMN_COUNT} className="px-4 py-12">
<EmptyState
icon={Handshake}
title="No referrals found"
description={
hasFilters
? "No referral matches these filters. Clear them to see the full list."
: "No partner has sent us a lead yet."
}
/>
</td>
</tr>
) : (
items.map((referral) => (
<ReferralRow
key={referral.id}
referral={referral}
onOpen={openReferral}
/>
))
)}
</tbody>
</table>
</div>
)}
{!isLoading && !error && (hasPrev || hasNext) && (
<div
className="flex flex-col sm:flex-row sm:items-center sm:justify-between gap-3 px-4 py-3 border-t border-border-default"
data-testid="rrm-referrals-pagination"
>
<p className="text-sm text-foreground-muted">
{total === undefined
? `Showing ${offset + 1}–${offset + items.length}`
: `Showing ${offset + 1}–${offset + items.length} of ${total.toLocaleString("en-IN")}`}
</p>
<div className="flex gap-2">
<Button
variant="outline"
size="sm"
data-testid="rrm-referrals-prev"
onClick={() => setOffset((o) => Math.max(0, o - PAGE_SIZE))}
disabled={!hasPrev}
>
Previous
</Button>
<Button
variant="outline"
size="sm"
data-testid="rrm-referrals-next"
onClick={() => setOffset((o) => o + PAGE_SIZE)}
disabled={!hasNext}
>
Next
</Button>
</div>
</div>
)}
</CardContent>
</Card>
</div>
);
}
export default ReferralsPage;
|