feat: dashboard Second Brain, essai 7 jours et vérification e-mail
Rendre le dashboard actionnable (inbox, peek, carte mentale), aligner la facturation sur l’essai 7 jours, et bloquer le login e-mail tant que l’adresse n’est pas confirmée. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -8,7 +8,32 @@ import { Checkbox } from '@/components/ui/checkbox'
|
||||
import { updateBillingConfig } from '@/app/actions/admin-billing'
|
||||
import { toast } from 'sonner'
|
||||
import { useLanguage } from '@/lib/i18n'
|
||||
import { CreditCard, Gauge, Coins, Package } from 'lucide-react'
|
||||
import {
|
||||
CreditCard,
|
||||
Gauge,
|
||||
Coins,
|
||||
Package,
|
||||
Activity,
|
||||
CheckCircle2,
|
||||
XCircle,
|
||||
AlertTriangle,
|
||||
Users,
|
||||
BookOpen,
|
||||
ChevronDown,
|
||||
ChevronUp,
|
||||
} from 'lucide-react'
|
||||
import { cn } from '@/lib/utils'
|
||||
|
||||
type PriceHealth = {
|
||||
key: string
|
||||
priceId: string
|
||||
configured: boolean
|
||||
source: string
|
||||
stripeAmount: number | null
|
||||
stripeCurrency: string | null
|
||||
stripeActive: boolean | null
|
||||
error: string | null
|
||||
}
|
||||
|
||||
type BillingAdminData = {
|
||||
billingConfig: Record<string, string>
|
||||
@@ -19,7 +44,6 @@ type BillingAdminData = {
|
||||
topUsers: Array<{ userId: string; email: string; name: string | null; requests: number }>
|
||||
}
|
||||
tiers: string[]
|
||||
/** Allocations mensuelles du solde unique (source de vérité débit) */
|
||||
creditAllocations?: Array<{
|
||||
tier: string
|
||||
monthlyCredits: number | null
|
||||
@@ -31,6 +55,35 @@ type BillingAdminData = {
|
||||
credits: number
|
||||
defaultDisplay: string
|
||||
}>
|
||||
stripeHealth?: {
|
||||
secretConfigured: boolean
|
||||
secretMode: 'test' | 'live' | 'missing' | 'placeholder'
|
||||
publishableConfigured: boolean
|
||||
webhookSecretConfigured: boolean
|
||||
billingEnabled: boolean
|
||||
trialDays: number
|
||||
prices: PriceHealth[]
|
||||
}
|
||||
subscriptionStats?: {
|
||||
byTier: Record<string, number>
|
||||
byStatus: Record<string, number>
|
||||
cancelAtPeriodEnd: number
|
||||
usersWithoutSub: number
|
||||
trialing: number
|
||||
pastDue: number
|
||||
paidActive: number
|
||||
recent: Array<{
|
||||
email: string
|
||||
name: string | null
|
||||
tier: string
|
||||
status: string
|
||||
trialEndsAt: string | null
|
||||
currentPeriodEnd: string | null
|
||||
cancelAtPeriodEnd: boolean
|
||||
hasStripeSub: boolean
|
||||
updatedAt: string
|
||||
}>
|
||||
}
|
||||
}
|
||||
|
||||
const SUBSCRIPTION_PRICE_KEYS = [
|
||||
@@ -46,7 +99,6 @@ const PACK_PRICE_KEYS = [
|
||||
'STRIPE_PRICE_CREDITS_L',
|
||||
] as const
|
||||
|
||||
/** Date stable SSR/client (pas de toLocaleString — mismatch locale + fuseau). */
|
||||
function formatStableUtc(iso: string): string {
|
||||
const d = new Date(iso)
|
||||
if (Number.isNaN(d.getTime())) return iso
|
||||
@@ -54,10 +106,21 @@ function formatStableUtc(iso: string): string {
|
||||
return `${d.getUTCFullYear()}-${pad(d.getUTCMonth() + 1)}-${pad(d.getUTCDate())} ${pad(d.getUTCHours())}:${pad(d.getUTCMinutes())}:${pad(d.getUTCSeconds())} UTC`
|
||||
}
|
||||
|
||||
function StatusDot({ ok, warn }: { ok: boolean; warn?: boolean }) {
|
||||
if (ok) return <CheckCircle2 className="h-4 w-4 text-emerald-600 shrink-0" />
|
||||
if (warn) return <AlertTriangle className="h-4 w-4 text-amber-500 shrink-0" />
|
||||
return <XCircle className="h-4 w-4 text-rose-500 shrink-0" />
|
||||
}
|
||||
|
||||
export function BillingAdminClient({ initialData }: { initialData: BillingAdminData }) {
|
||||
const { t } = useLanguage()
|
||||
const [billingEnabled, setBillingEnabled] = useState(initialData.billingConfig.BILLING_ENABLED === 'true')
|
||||
const [isSavingBilling, setIsSavingBilling] = useState(false)
|
||||
const [showTestGuide, setShowTestGuide] = useState(true)
|
||||
|
||||
const health = initialData.stripeHealth
|
||||
const stats = initialData.subscriptionStats
|
||||
const priceMap = Object.fromEntries((health?.prices ?? []).map((p) => [p.key, p]))
|
||||
|
||||
const handleSaveBilling = async (formData: FormData) => {
|
||||
setIsSavingBilling(true)
|
||||
@@ -77,6 +140,15 @@ export function BillingAdminClient({ initialData }: { initialData: BillingAdminD
|
||||
}
|
||||
}
|
||||
|
||||
const modeLabel =
|
||||
health?.secretMode === 'test'
|
||||
? t('admin.billing.modeTest')
|
||||
: health?.secretMode === 'live'
|
||||
? t('admin.billing.modeLive')
|
||||
: health?.secretMode === 'placeholder'
|
||||
? t('admin.billing.modePlaceholder')
|
||||
: t('admin.billing.modeMissing')
|
||||
|
||||
return (
|
||||
<div className="space-y-8">
|
||||
<div>
|
||||
@@ -84,6 +156,246 @@ export function BillingAdminClient({ initialData }: { initialData: BillingAdminD
|
||||
<p className="text-sm text-muted-foreground mt-1">{t('admin.billing.description')}</p>
|
||||
</div>
|
||||
|
||||
{/* Stripe health */}
|
||||
{health && (
|
||||
<div className="bg-card rounded-lg border border-border shadow-sm overflow-hidden">
|
||||
<div className="flex items-center gap-3 p-6 border-b border-border">
|
||||
<div className="w-10 h-10 rounded-full bg-primary/10 flex items-center justify-center text-primary">
|
||||
<Activity className="h-5 w-5" />
|
||||
</div>
|
||||
<div>
|
||||
<h2 className="font-semibold">{t('admin.billing.healthTitle')}</h2>
|
||||
<p className="text-sm text-muted-foreground">{t('admin.billing.healthDescription')}</p>
|
||||
</div>
|
||||
</div>
|
||||
<div className="p-6 grid gap-3 sm:grid-cols-2 lg:grid-cols-3">
|
||||
<div className="flex items-start gap-2 rounded-xl border border-border/60 bg-muted/20 p-3">
|
||||
<StatusDot ok={health.secretConfigured} warn={health.secretMode === 'placeholder'} />
|
||||
<div className="min-w-0">
|
||||
<p className="text-xs font-semibold">{t('admin.billing.healthSecret')}</p>
|
||||
<p className="text-[11px] text-muted-foreground">{modeLabel}</p>
|
||||
</div>
|
||||
</div>
|
||||
<div className="flex items-start gap-2 rounded-xl border border-border/60 bg-muted/20 p-3">
|
||||
<StatusDot ok={health.publishableConfigured} />
|
||||
<div>
|
||||
<p className="text-xs font-semibold">{t('admin.billing.healthPublishable')}</p>
|
||||
<p className="text-[11px] text-muted-foreground">
|
||||
{health.publishableConfigured ? t('admin.billing.configured') : t('admin.billing.missing')}
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
<div className="flex items-start gap-2 rounded-xl border border-border/60 bg-muted/20 p-3">
|
||||
<StatusDot ok={health.webhookSecretConfigured} />
|
||||
<div>
|
||||
<p className="text-xs font-semibold">{t('admin.billing.healthWebhook')}</p>
|
||||
<p className="text-[11px] text-muted-foreground">
|
||||
{health.webhookSecretConfigured ? t('admin.billing.configured') : t('admin.billing.missing')}
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
<div className="flex items-start gap-2 rounded-xl border border-border/60 bg-muted/20 p-3">
|
||||
<StatusDot ok={health.billingEnabled} warn={!health.billingEnabled} />
|
||||
<div>
|
||||
<p className="text-xs font-semibold">{t('admin.billing.healthBillingFlag')}</p>
|
||||
<p className="text-[11px] text-muted-foreground">
|
||||
{health.billingEnabled ? t('admin.billing.enabled') : t('admin.billing.disabled')}
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
<div className="flex items-start gap-2 rounded-xl border border-border/60 bg-muted/20 p-3">
|
||||
<StatusDot ok />
|
||||
<div>
|
||||
<p className="text-xs font-semibold">{t('admin.billing.healthTrial')}</p>
|
||||
<p className="text-[11px] text-muted-foreground">
|
||||
{t('admin.billing.trialDaysValue', { days: health.trialDays })}
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div className="px-6 pb-6">
|
||||
<h3 className="text-sm font-medium mb-3">{t('admin.billing.priceStatusTitle')}</h3>
|
||||
<div className="overflow-x-auto rounded-xl border border-border/60">
|
||||
<table className="w-full text-xs">
|
||||
<thead className="bg-muted/40 text-muted-foreground">
|
||||
<tr>
|
||||
<th className="text-start p-2.5 font-medium">{t('admin.billing.colKey')}</th>
|
||||
<th className="text-start p-2.5 font-medium">{t('admin.billing.colPriceId')}</th>
|
||||
<th className="text-start p-2.5 font-medium">{t('admin.billing.colSource')}</th>
|
||||
<th className="text-start p-2.5 font-medium">{t('admin.billing.colStripe')}</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{(health.prices ?? []).map((row) => (
|
||||
<tr key={row.key} className="border-t border-border/50">
|
||||
<td className="p-2.5 font-mono">{t(`admin.billing.${row.key}`)}</td>
|
||||
<td className="p-2.5 font-mono truncate max-w-[180px]">
|
||||
{row.configured ? row.priceId : '—'}
|
||||
</td>
|
||||
<td className="p-2.5">{row.source}</td>
|
||||
<td className="p-2.5">
|
||||
{!row.configured ? (
|
||||
<span className="text-rose-600">{t('admin.billing.missing')}</span>
|
||||
) : row.error ? (
|
||||
<span className="text-rose-600" title={row.error}>{t('admin.billing.priceError')}</span>
|
||||
) : row.stripeAmount != null ? (
|
||||
<span className={cn(row.stripeActive === false && 'text-amber-600')}>
|
||||
{row.stripeAmount.toLocaleString('fr-FR', { minimumFractionDigits: 2 })} {row.stripeCurrency}
|
||||
{row.stripeActive === false ? ` (${t('admin.billing.inactive')})` : ''}
|
||||
</span>
|
||||
) : (
|
||||
<span className="text-muted-foreground">{t('admin.billing.notChecked')}</span>
|
||||
)}
|
||||
</td>
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Subscription stats */}
|
||||
{stats && (
|
||||
<div className="bg-card rounded-lg border border-border shadow-sm overflow-hidden">
|
||||
<div className="flex items-center gap-3 p-6 border-b border-border">
|
||||
<div className="w-10 h-10 rounded-full bg-primary/10 flex items-center justify-center text-primary">
|
||||
<Users className="h-5 w-5" />
|
||||
</div>
|
||||
<div>
|
||||
<h2 className="font-semibold">{t('admin.billing.subsTitle')}</h2>
|
||||
<p className="text-sm text-muted-foreground">{t('admin.billing.subsDescription')}</p>
|
||||
</div>
|
||||
</div>
|
||||
<div className="p-6 grid grid-cols-2 sm:grid-cols-4 gap-3">
|
||||
<div className="rounded-xl border border-border/60 bg-muted/20 p-4">
|
||||
<p className="text-[10px] uppercase tracking-widest text-muted-foreground font-bold">{t('admin.billing.statPaid')}</p>
|
||||
<p className="text-2xl font-semibold tabular-nums mt-1">{stats.paidActive}</p>
|
||||
</div>
|
||||
<div className="rounded-xl border border-border/60 bg-muted/20 p-4">
|
||||
<p className="text-[10px] uppercase tracking-widest text-muted-foreground font-bold">{t('admin.billing.statTrialing')}</p>
|
||||
<p className="text-2xl font-semibold tabular-nums mt-1">{stats.trialing}</p>
|
||||
</div>
|
||||
<div className="rounded-xl border border-border/60 bg-muted/20 p-4">
|
||||
<p className="text-[10px] uppercase tracking-widest text-muted-foreground font-bold">{t('admin.billing.statPastDue')}</p>
|
||||
<p className="text-2xl font-semibold tabular-nums mt-1">{stats.pastDue}</p>
|
||||
</div>
|
||||
<div className="rounded-xl border border-border/60 bg-muted/20 p-4">
|
||||
<p className="text-[10px] uppercase tracking-widest text-muted-foreground font-bold">{t('admin.billing.statCanceling')}</p>
|
||||
<p className="text-2xl font-semibold tabular-nums mt-1">{stats.cancelAtPeriodEnd}</p>
|
||||
</div>
|
||||
</div>
|
||||
<div className="px-6 pb-4 grid gap-4 sm:grid-cols-2">
|
||||
<div>
|
||||
<h3 className="text-sm font-medium mb-2">{t('admin.billing.byTier')}</h3>
|
||||
<ul className="space-y-1.5 text-sm">
|
||||
{Object.entries(stats.byTier).map(([tier, count]) => (
|
||||
<li key={tier} className="flex justify-between border-b border-border/40 pb-1">
|
||||
<span>{tier}</span>
|
||||
<span className="tabular-nums text-muted-foreground">{count}</span>
|
||||
</li>
|
||||
))}
|
||||
<li className="flex justify-between text-xs text-muted-foreground pt-1">
|
||||
<span>{t('admin.billing.usersWithoutSub')}</span>
|
||||
<span className="tabular-nums">{stats.usersWithoutSub}</span>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
<div>
|
||||
<h3 className="text-sm font-medium mb-2">{t('admin.billing.byStatus')}</h3>
|
||||
<ul className="space-y-1.5 text-sm">
|
||||
{Object.keys(stats.byStatus).length === 0 ? (
|
||||
<li className="text-muted-foreground text-xs">{t('admin.billing.noSubs')}</li>
|
||||
) : (
|
||||
Object.entries(stats.byStatus).map(([status, count]) => (
|
||||
<li key={status} className="flex justify-between border-b border-border/40 pb-1">
|
||||
<span>{status}</span>
|
||||
<span className="tabular-nums text-muted-foreground">{count}</span>
|
||||
</li>
|
||||
))
|
||||
)}
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
<div className="px-6 pb-6">
|
||||
<h3 className="text-sm font-medium mb-2">{t('admin.billing.recentSubs')}</h3>
|
||||
{stats.recent.length === 0 ? (
|
||||
<p className="text-sm text-muted-foreground">{t('admin.billing.noSubs')}</p>
|
||||
) : (
|
||||
<div className="overflow-x-auto rounded-xl border border-border/60">
|
||||
<table className="w-full text-xs">
|
||||
<thead className="bg-muted/40 text-muted-foreground">
|
||||
<tr>
|
||||
<th className="text-start p-2.5 font-medium">{t('admin.billing.colUser')}</th>
|
||||
<th className="text-start p-2.5 font-medium">{t('admin.billing.colTier')}</th>
|
||||
<th className="text-start p-2.5 font-medium">{t('admin.billing.colStatus')}</th>
|
||||
<th className="text-start p-2.5 font-medium">{t('admin.billing.colPeriod')}</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{stats.recent.map((row) => (
|
||||
<tr key={`${row.email}-${row.updatedAt}`} className="border-t border-border/50">
|
||||
<td className="p-2.5 truncate max-w-[200px]">{row.email}</td>
|
||||
<td className="p-2.5">{row.tier}</td>
|
||||
<td className="p-2.5">
|
||||
{row.status}
|
||||
{row.cancelAtPeriodEnd ? ` · ${t('admin.billing.canceling')}` : ''}
|
||||
{!row.hasStripeSub ? ` · ${t('admin.billing.manualTier')}` : ''}
|
||||
</td>
|
||||
<td className="p-2.5 text-muted-foreground">
|
||||
{row.trialEndsAt
|
||||
? t('admin.billing.trialUntil', { date: formatStableUtc(row.trialEndsAt) })
|
||||
: row.currentPeriodEnd
|
||||
? formatStableUtc(row.currentPeriodEnd)
|
||||
: '—'}
|
||||
</td>
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* How to test Stripe */}
|
||||
<div className="bg-card rounded-lg border border-border shadow-sm overflow-hidden">
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setShowTestGuide((v) => !v)}
|
||||
className="w-full flex items-center gap-3 p-6 border-b border-border text-start hover:bg-muted/20 transition-colors"
|
||||
>
|
||||
<div className="w-10 h-10 rounded-full bg-primary/10 flex items-center justify-center text-primary">
|
||||
<BookOpen className="h-5 w-5" />
|
||||
</div>
|
||||
<div className="flex-1">
|
||||
<h2 className="font-semibold">{t('admin.billing.testGuideTitle')}</h2>
|
||||
<p className="text-sm text-muted-foreground">{t('admin.billing.testGuideDescription')}</p>
|
||||
</div>
|
||||
{showTestGuide ? <ChevronUp className="h-4 w-4 text-muted-foreground" /> : <ChevronDown className="h-4 w-4 text-muted-foreground" />}
|
||||
</button>
|
||||
{showTestGuide && (
|
||||
<div className="p-6 space-y-3 text-sm text-muted-foreground leading-relaxed">
|
||||
<ol className="list-decimal ps-5 space-y-2">
|
||||
<li>{t('admin.billing.testStep1')}</li>
|
||||
<li>{t('admin.billing.testStep2')}</li>
|
||||
<li>
|
||||
<code className="text-[11px] bg-muted px-1.5 py-0.5 rounded">stripe listen --forward-to localhost:3000/api/billing/webhook</code>
|
||||
{' — '}{t('admin.billing.testStep3')}
|
||||
</li>
|
||||
<li>{t('admin.billing.testStep4')}</li>
|
||||
<li>{t('admin.billing.testStep5')}</li>
|
||||
<li>{t('admin.billing.testStep6')}</li>
|
||||
</ol>
|
||||
<p className="text-xs border-t border-border/50 pt-3">
|
||||
{t('admin.billing.testCardHint')}
|
||||
</p>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<div className="bg-card rounded-lg border border-border shadow-sm overflow-hidden">
|
||||
<div className="flex items-center gap-3 p-6 border-b border-border">
|
||||
<div className="w-10 h-10 rounded-full bg-primary/10 flex items-center justify-center text-primary">
|
||||
@@ -106,17 +418,28 @@ export function BillingAdminClient({ initialData }: { initialData: BillingAdminD
|
||||
<div>
|
||||
<h3 className="text-sm font-medium mb-3">{t('admin.billing.subscriptionPricesTitle')}</h3>
|
||||
<div className="grid gap-4 sm:grid-cols-2">
|
||||
{SUBSCRIPTION_PRICE_KEYS.map((key) => (
|
||||
<div key={key} className="space-y-2">
|
||||
<Label htmlFor={key}>{t(`admin.billing.${key}`)}</Label>
|
||||
<Input
|
||||
id={key}
|
||||
name={key}
|
||||
defaultValue={initialData.billingConfig[key] ?? ''}
|
||||
placeholder="price_..."
|
||||
/>
|
||||
</div>
|
||||
))}
|
||||
{SUBSCRIPTION_PRICE_KEYS.map((key) => {
|
||||
const meta = priceMap[key]
|
||||
return (
|
||||
<div key={key} className="space-y-2">
|
||||
<Label htmlFor={key}>{t(`admin.billing.${key}`)}</Label>
|
||||
<Input
|
||||
id={key}
|
||||
name={key}
|
||||
defaultValue={initialData.billingConfig[key] ?? ''}
|
||||
placeholder="price_..."
|
||||
/>
|
||||
{meta?.stripeAmount != null && (
|
||||
<p className="text-[11px] text-muted-foreground">
|
||||
Stripe: {meta.stripeAmount.toLocaleString('fr-FR', { minimumFractionDigits: 2 })} {meta.stripeCurrency}
|
||||
</p>
|
||||
)}
|
||||
{meta?.error && (
|
||||
<p className="text-[11px] text-rose-600">{meta.error}</p>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
<div className="border-t border-border/50 pt-4">
|
||||
@@ -144,7 +467,6 @@ export function BillingAdminClient({ initialData }: { initialData: BillingAdminD
|
||||
</form>
|
||||
</div>
|
||||
|
||||
{/* Solde unique — source de vérité du débit */}
|
||||
<div className="bg-card rounded-lg border border-border shadow-sm overflow-hidden">
|
||||
<div className="flex items-center gap-3 p-6 border-b border-border">
|
||||
<div className="w-10 h-10 rounded-full bg-primary/10 flex items-center justify-center text-primary">
|
||||
@@ -216,7 +538,6 @@ export function BillingAdminClient({ initialData }: { initialData: BillingAdminD
|
||||
{t('admin.billing.usagePeriod', { period: initialData.usageOverview.period })}
|
||||
{initialData.usageOverview.lastSyncedAt
|
||||
? ` · ${t('admin.billing.lastSync', {
|
||||
// Format fixe UTC (évite mismatch SSR locale/fuseau vs client)
|
||||
date: formatStableUtc(initialData.usageOverview.lastSyncedAt),
|
||||
})}`
|
||||
: ` · ${t('admin.billing.notSynced')}`}
|
||||
|
||||
Reference in New Issue
Block a user