|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import React, { useState } from 'react'; |
| 4 | +import { useQuery } from '@tanstack/react-query'; |
| 5 | +import type { inferRouterOutputs } from '@trpc/server'; |
| 6 | +import { ExternalLink } from 'lucide-react'; |
| 7 | +import Link from 'next/link'; |
| 8 | + |
| 9 | +import type { RootRouter } from '@/routers/root-router'; |
| 10 | +import { useTRPC } from '@/lib/trpc/utils'; |
| 11 | +import { formatCents } from '@/lib/utils'; |
| 12 | +import { Badge } from '@/components/ui/badge'; |
| 13 | +import { Button } from '@/components/ui/button'; |
| 14 | +import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'; |
| 15 | +import { |
| 16 | + Table, |
| 17 | + TableBody, |
| 18 | + TableCell, |
| 19 | + TableHead, |
| 20 | + TableHeader, |
| 21 | + TableRow, |
| 22 | +} from '@/components/ui/table'; |
| 23 | + |
| 24 | +const PAGE_SIZE = 25; |
| 25 | +type RouterOutputs = inferRouterOutputs<RootRouter>; |
| 26 | +export type EarlyFraudWarningRow = |
| 27 | + RouterOutputs['admin']['earlyFraudWarnings']['list']['rows'][number]; |
| 28 | + |
| 29 | +export function EarlyFraudWarningsContent() { |
| 30 | + const trpc = useTRPC(); |
| 31 | + const [page, setPage] = useState(1); |
| 32 | + const casesQuery = useQuery( |
| 33 | + trpc.admin.earlyFraudWarnings.list.queryOptions({ page, limit: PAGE_SIZE }) |
| 34 | + ); |
| 35 | + const rows = casesQuery.data?.rows ?? []; |
| 36 | + const pagination = casesQuery.data?.pagination; |
| 37 | + |
| 38 | + return ( |
| 39 | + <div className="flex w-full flex-col gap-6"> |
| 40 | + <div className="space-y-2"> |
| 41 | + <h2 className="text-2xl font-bold">Early Fraud Warnings</h2> |
| 42 | + <p className="text-muted-foreground max-w-4xl"> |
| 43 | + Review new Stripe warnings captured during the observation rollout. This view is |
| 44 | + read-only; captured cases do not restrict access, refund payments, or schedule automated |
| 45 | + actions. |
| 46 | + </p> |
| 47 | + </div> |
| 48 | + |
| 49 | + <Card> |
| 50 | + <CardHeader> |
| 51 | + <CardTitle>Captured warnings</CardTitle> |
| 52 | + <CardDescription> |
| 53 | + One row is stored per newly delivered warning. Personal matches remain manual-review |
| 54 | + cases during observation. |
| 55 | + </CardDescription> |
| 56 | + </CardHeader> |
| 57 | + <CardContent className="flex flex-col gap-4"> |
| 58 | + {casesQuery.isError ? ( |
| 59 | + <p className="text-destructive text-sm" role="alert"> |
| 60 | + Warning cases could not be loaded. Refresh the page to try again. |
| 61 | + </p> |
| 62 | + ) : ( |
| 63 | + <> |
| 64 | + <EarlyFraudWarningsTable rows={rows} isLoading={casesQuery.isLoading} /> |
| 65 | + <div className="flex flex-col items-start justify-between gap-3 text-sm sm:flex-row sm:items-center"> |
| 66 | + <p className="text-muted-foreground"> |
| 67 | + {pagination |
| 68 | + ? `${pagination.total} captured warning${pagination.total === 1 ? '' : 's'}` |
| 69 | + : 'Loading warning count...'} |
| 70 | + </p> |
| 71 | + <div className="flex gap-2"> |
| 72 | + <Button |
| 73 | + variant="secondary" |
| 74 | + size="sm" |
| 75 | + onClick={() => setPage(current => Math.max(1, current - 1))} |
| 76 | + disabled={page <= 1 || casesQuery.isFetching} |
| 77 | + > |
| 78 | + Previous |
| 79 | + </Button> |
| 80 | + <Button |
| 81 | + variant="secondary" |
| 82 | + size="sm" |
| 83 | + onClick={() => setPage(current => current + 1)} |
| 84 | + disabled={!pagination || page >= pagination.totalPages || casesQuery.isFetching} |
| 85 | + > |
| 86 | + Next |
| 87 | + </Button> |
| 88 | + </div> |
| 89 | + </div> |
| 90 | + </> |
| 91 | + )} |
| 92 | + </CardContent> |
| 93 | + </Card> |
| 94 | + </div> |
| 95 | + ); |
| 96 | +} |
| 97 | + |
| 98 | +export function EarlyFraudWarningsTable({ |
| 99 | + rows, |
| 100 | + isLoading, |
| 101 | +}: { |
| 102 | + rows: EarlyFraudWarningRow[]; |
| 103 | + isLoading: boolean; |
| 104 | +}) { |
| 105 | + return ( |
| 106 | + <div className="overflow-x-auto rounded-lg border"> |
| 107 | + <Table> |
| 108 | + <TableHeader> |
| 109 | + <TableRow> |
| 110 | + <TableHead>Received</TableHead> |
| 111 | + <TableHead>Status</TableHead> |
| 112 | + <TableHead>Owner</TableHead> |
| 113 | + <TableHead>Amount</TableHead> |
| 114 | + <TableHead>Linked account</TableHead> |
| 115 | + <TableHead>Stripe identifiers</TableHead> |
| 116 | + <TableHead>Review reason</TableHead> |
| 117 | + </TableRow> |
| 118 | + </TableHeader> |
| 119 | + <TableBody> |
| 120 | + {rows.length === 0 ? ( |
| 121 | + <TableRow> |
| 122 | + <TableCell colSpan={7} className="text-muted-foreground h-24 text-center"> |
| 123 | + {isLoading |
| 124 | + ? 'Loading captured warnings...' |
| 125 | + : 'No early fraud warnings captured yet.'} |
| 126 | + </TableCell> |
| 127 | + </TableRow> |
| 128 | + ) : ( |
| 129 | + rows.map(row => ( |
| 130 | + <TableRow key={row.id}> |
| 131 | + <TableCell className="whitespace-nowrap text-sm"> |
| 132 | + {formatTimestamp(row.warningCreatedAt ?? row.createdAt)} |
| 133 | + </TableCell> |
| 134 | + <TableCell> |
| 135 | + <Badge variant={row.status === 'failed' ? 'destructive' : 'secondary'}> |
| 136 | + {formatStatus(row.status)} |
| 137 | + </Badge> |
| 138 | + </TableCell> |
| 139 | + <TableCell> |
| 140 | + <Badge |
| 141 | + variant={row.ownerClassification === 'ambiguous' ? 'destructive' : 'outline'} |
| 142 | + > |
| 143 | + {formatOwnerClassification(row.ownerClassification)} |
| 144 | + </Badge> |
| 145 | + </TableCell> |
| 146 | + <TableCell className="whitespace-nowrap font-mono text-sm tabular-nums"> |
| 147 | + {formatAmount(row.amountMinorUnits, row.currency)} |
| 148 | + </TableCell> |
| 149 | + <TableCell className="min-w-48 text-sm">{renderLinkedAccount(row)}</TableCell> |
| 150 | + <TableCell className="min-w-64 text-xs"> |
| 151 | + <StripeIdentifiers row={row} /> |
| 152 | + </TableCell> |
| 153 | + <TableCell className="text-muted-foreground min-w-64 text-sm"> |
| 154 | + {row.reason ?? 'Manual review required'} |
| 155 | + {row.failureContext ? ( |
| 156 | + <div className="text-destructive mt-1">{row.failureContext}</div> |
| 157 | + ) : null} |
| 158 | + </TableCell> |
| 159 | + </TableRow> |
| 160 | + )) |
| 161 | + )} |
| 162 | + </TableBody> |
| 163 | + </Table> |
| 164 | + </div> |
| 165 | + ); |
| 166 | +} |
| 167 | + |
| 168 | +function StripeIdentifiers({ row }: { row: EarlyFraudWarningRow }) { |
| 169 | + return ( |
| 170 | + <div className="flex flex-col gap-1 font-mono"> |
| 171 | + <span>{row.stripeEarlyFraudWarningId}</span> |
| 172 | + {row.stripeChargeId ? ( |
| 173 | + <a |
| 174 | + href={stripePaymentUrl(row.stripeChargeId)} |
| 175 | + target="_blank" |
| 176 | + rel="noopener noreferrer" |
| 177 | + className="text-blue-400 hover:text-blue-300 inline-flex items-center gap-1" |
| 178 | + > |
| 179 | + {row.stripeChargeId} |
| 180 | + <ExternalLink className="size-3 shrink-0" /> |
| 181 | + </a> |
| 182 | + ) : null} |
| 183 | + {row.stripePaymentIntentId ? <span>{row.stripePaymentIntentId}</span> : null} |
| 184 | + {row.stripeCustomerId ? ( |
| 185 | + <a |
| 186 | + href={stripeCustomerUrl(row.stripeCustomerId)} |
| 187 | + target="_blank" |
| 188 | + rel="noopener noreferrer" |
| 189 | + className="text-blue-400 hover:text-blue-300 inline-flex items-center gap-1" |
| 190 | + > |
| 191 | + {row.stripeCustomerId} |
| 192 | + <ExternalLink className="size-3 shrink-0" /> |
| 193 | + </a> |
| 194 | + ) : null} |
| 195 | + </div> |
| 196 | + ); |
| 197 | +} |
| 198 | + |
| 199 | +function renderLinkedAccount(row: EarlyFraudWarningRow) { |
| 200 | + if (row.user) { |
| 201 | + return ( |
| 202 | + <Link |
| 203 | + className="text-blue-400 hover:text-blue-300" |
| 204 | + href={`/admin/users/${encodeURIComponent(row.user.id)}`} |
| 205 | + > |
| 206 | + {row.user.email} |
| 207 | + </Link> |
| 208 | + ); |
| 209 | + } |
| 210 | + |
| 211 | + if (row.organization) { |
| 212 | + return ( |
| 213 | + <Link |
| 214 | + className="text-blue-400 hover:text-blue-300" |
| 215 | + href={`/admin/organizations/${encodeURIComponent(row.organization.id)}`} |
| 216 | + > |
| 217 | + {row.organization.name} |
| 218 | + </Link> |
| 219 | + ); |
| 220 | + } |
| 221 | + |
| 222 | + return <span className="text-muted-foreground">No owner linked</span>; |
| 223 | +} |
| 224 | + |
| 225 | +function formatStatus(status: string): string { |
| 226 | + return status.replaceAll('_', ' ').replace(/^./, value => value.toUpperCase()); |
| 227 | +} |
| 228 | + |
| 229 | +function formatOwnerClassification(classification: string): string { |
| 230 | + return classification === 'personal' |
| 231 | + ? 'Personal observation' |
| 232 | + : classification.replace(/^./, value => value.toUpperCase()); |
| 233 | +} |
| 234 | + |
| 235 | +function formatTimestamp(value: string | null): string { |
| 236 | + if (!value) return 'Not available'; |
| 237 | + const date = new Date(value); |
| 238 | + return Number.isNaN(date.getTime()) ? 'Not available' : date.toLocaleString(); |
| 239 | +} |
| 240 | + |
| 241 | +function formatAmount(amountMinorUnits: number | null, currency: string | null): string { |
| 242 | + if (amountMinorUnits === null || !currency) return 'Not available'; |
| 243 | + return formatCents(amountMinorUnits, currency); |
| 244 | +} |
| 245 | + |
| 246 | +function stripeDashboardPrefix(): string { |
| 247 | + return process.env.NODE_ENV === 'development' ? 'test/' : ''; |
| 248 | +} |
| 249 | + |
| 250 | +function stripePaymentUrl(chargeId: string): string { |
| 251 | + return `https://dashboard.stripe.com/${stripeDashboardPrefix()}payments/${chargeId}`; |
| 252 | +} |
| 253 | + |
| 254 | +function stripeCustomerUrl(customerId: string): string { |
| 255 | + return `https://dashboard.stripe.com/${stripeDashboardPrefix()}customers/${customerId}`; |
| 256 | +} |
0 commit comments