|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { useCallback } from "react"; |
| 4 | +import { Download } from "lucide-react"; |
| 5 | +import { Badge } from "@/components/ui/badge"; |
| 6 | +import { Button } from "@/components/ui/button"; |
| 7 | +import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table"; |
| 8 | +import type { MonthlyUsage } from "@/features/billing/seatUsageReport"; |
| 9 | +import { SettingsCard } from "../components/settingsCard"; |
| 10 | + |
| 11 | +const DOCS_URL = "https://docs.sourcebot.dev/docs/seat-reconciliation"; |
| 12 | +const REPORT_EMAIL = "ar@sourcebot.dev"; |
| 13 | + |
| 14 | +interface OfflineUsageReportCardProps { |
| 15 | + licenseId: string; |
| 16 | + // ISO 8601 subscription start date. |
| 17 | + startDate: string; |
| 18 | + months: MonthlyUsage[]; |
| 19 | +} |
| 20 | + |
| 21 | +export function OfflineUsageReportCard({ licenseId, startDate, months }: OfflineUsageReportCardProps) { |
| 22 | + // Most recent first; the in-progress Month (if any) sits at the top. |
| 23 | + const rows = [...months].reverse(); |
| 24 | + const completedMonths = months.filter((m) => m.isComplete); |
| 25 | + |
| 26 | + const handleExport = useCallback(() => { |
| 27 | + const report = { |
| 28 | + licenseId, |
| 29 | + startDate, |
| 30 | + // Only completed Months are reportable; an in-progress Month's peak |
| 31 | + // can still rise before the Month closes. |
| 32 | + months: completedMonths.map((m) => ({ |
| 33 | + monthNumber: m.monthNumber, |
| 34 | + windowStart: m.windowStart.toISOString(), |
| 35 | + windowEnd: m.windowEnd.toISOString(), |
| 36 | + peakProvisioned: m.peakProvisioned, |
| 37 | + peakAt: m.peakAt.toISOString(), |
| 38 | + })), |
| 39 | + }; |
| 40 | + |
| 41 | + const blob = new Blob([JSON.stringify(report, null, 2)], { type: "application/json" }); |
| 42 | + const url = URL.createObjectURL(blob); |
| 43 | + const anchor = document.createElement("a"); |
| 44 | + anchor.href = url; |
| 45 | + anchor.download = `sourcebot-usage-${licenseId}.json`; |
| 46 | + anchor.click(); |
| 47 | + URL.revokeObjectURL(url); |
| 48 | + }, [licenseId, startDate, completedMonths]); |
| 49 | + |
| 50 | + return ( |
| 51 | + <div className="flex flex-col gap-3"> |
| 52 | + <div> |
| 53 | + <h3 className="text-lg font-medium">Usage</h3> |
| 54 | + <p className="text-sm text-muted-foreground"> |
| 55 | + The greatest number of users provisioned during each subscription month. |
| 56 | + Within five business days of each month's end, send the report to{" "} |
| 57 | + <a href={`mailto:${REPORT_EMAIL}`} className="text-link hover:underline"> |
| 58 | + {REPORT_EMAIL} |
| 59 | + </a>{" "} |
| 60 | + for reconciliation.{" "} |
| 61 | + <a |
| 62 | + href={DOCS_URL} |
| 63 | + target="_blank" |
| 64 | + rel="noopener noreferrer" |
| 65 | + className="text-link hover:underline" |
| 66 | + > |
| 67 | + Learn more |
| 68 | + </a> |
| 69 | + </p> |
| 70 | + </div> |
| 71 | + <SettingsCard> |
| 72 | + <div className="flex flex-col gap-4"> |
| 73 | + <div className="flex items-center justify-end"> |
| 74 | + <Button |
| 75 | + variant="outline" |
| 76 | + size="sm" |
| 77 | + onClick={handleExport} |
| 78 | + disabled={completedMonths.length === 0} |
| 79 | + > |
| 80 | + <Download className="h-3.5 w-3.5" /> |
| 81 | + Export report |
| 82 | + </Button> |
| 83 | + </div> |
| 84 | + <Table> |
| 85 | + <TableHeader> |
| 86 | + <TableRow> |
| 87 | + <TableHead>Month</TableHead> |
| 88 | + <TableHead className="text-right">Peak users</TableHead> |
| 89 | + <TableHead className="text-right">Reached</TableHead> |
| 90 | + <TableHead className="text-right">At month end</TableHead> |
| 91 | + </TableRow> |
| 92 | + </TableHeader> |
| 93 | + <TableBody> |
| 94 | + {rows.map((month) => ( |
| 95 | + <TableRow key={month.monthNumber}> |
| 96 | + <TableCell className="flex items-center gap-2"> |
| 97 | + {formatWindow(month.windowStart, month.windowEnd)} |
| 98 | + {!month.isComplete && ( |
| 99 | + <Badge variant="outline" className="text-muted-foreground"> |
| 100 | + In progress |
| 101 | + </Badge> |
| 102 | + )} |
| 103 | + </TableCell> |
| 104 | + <TableCell className="text-right font-medium"> |
| 105 | + {month.peakProvisioned} |
| 106 | + </TableCell> |
| 107 | + <TableCell className="text-right text-muted-foreground"> |
| 108 | + {formatDate(month.peakAt)} |
| 109 | + </TableCell> |
| 110 | + <TableCell className="text-right text-muted-foreground"> |
| 111 | + {month.endProvisioned} |
| 112 | + </TableCell> |
| 113 | + </TableRow> |
| 114 | + ))} |
| 115 | + </TableBody> |
| 116 | + </Table> |
| 117 | + </div> |
| 118 | + </SettingsCard> |
| 119 | + </div> |
| 120 | + ); |
| 121 | +} |
| 122 | + |
| 123 | +// Month boundaries are UTC instants, so format in UTC to avoid the local |
| 124 | +// timezone shifting the displayed day across a midnight boundary. |
| 125 | +function formatDate(date: Date): string { |
| 126 | + return new Date(date).toLocaleDateString('en-US', { |
| 127 | + month: 'short', |
| 128 | + day: 'numeric', |
| 129 | + year: 'numeric', |
| 130 | + timeZone: 'UTC', |
| 131 | + }); |
| 132 | +} |
| 133 | + |
| 134 | +// The window is half-open [start, end); display the inclusive last day. |
| 135 | +function formatWindow(start: Date, end: Date): string { |
| 136 | + const inclusiveEnd = new Date(end.getTime() - 1); |
| 137 | + return `${formatDate(start)} – ${formatDate(inclusiveEnd)}`; |
| 138 | +} |
0 commit comments