'use client'; import React from 'react'; import type { Locale } from '@/lib/locale'; import { pickLocaleText } from '@/lib/locale'; export interface UserSub { id: number; group_id: number; starts_at: string; expires_at: string; status: string; daily_usage_usd: number; weekly_usage_usd: number; monthly_usage_usd: number; } interface UserSubscriptionsProps { subscriptions: UserSub[]; onRenew: (groupId: number) => void; isDark: boolean; locale: Locale; } function formatDate(iso: string): string { const d = new Date(iso); if (isNaN(d.getTime())) return iso; return d.toLocaleDateString('zh-CN', { year: 'numeric', month: '2-digit', day: '2-digit' }); } function daysUntil(iso: string): number { const now = new Date(); const target = new Date(iso); return Math.ceil((target.getTime() - now.getTime()) / (1000 * 60 * 60 * 24)); } function getStatusBadge(status: string, isDark: boolean, locale: Locale): { text: string; className: string } { const statusMap: Record = { active: { zh: '生效中', en: 'Active', cls: 'bg-emerald-100 text-emerald-700', clsDark: 'bg-emerald-900/40 text-emerald-300' }, expired: { zh: '已过期', en: 'Expired', cls: 'bg-slate-100 text-slate-600', clsDark: 'bg-slate-700 text-slate-400' }, cancelled: { zh: '已取消', en: 'Cancelled', cls: 'bg-red-100 text-red-700', clsDark: 'bg-red-900/40 text-red-300' }, }; const entry = statusMap[status] || { zh: status, en: status, cls: 'bg-slate-100 text-slate-600', clsDark: 'bg-slate-700 text-slate-400' }; return { text: pickLocaleText(locale, entry.zh, entry.en), className: isDark ? entry.clsDark : entry.cls, }; } export default function UserSubscriptions({ subscriptions, onRenew, isDark, locale }: UserSubscriptionsProps) { if (subscriptions.length === 0) { return (

{pickLocaleText(locale, '暂无订阅', 'No Subscriptions')}

); } return (
{subscriptions.map((sub) => { const remaining = daysUntil(sub.expires_at); const isExpiringSoon = remaining > 0 && remaining <= 7; const badge = getStatusBadge(sub.status, isDark, locale); return (
{/* Header */}
{pickLocaleText(locale, `渠道 #${sub.group_id}`, `Channel #${sub.group_id}`)} {badge.text}
{sub.status === 'active' && ( )}
{/* Dates */}
{pickLocaleText(locale, '开始', 'Start')}

{formatDate(sub.starts_at)}

{pickLocaleText(locale, '到期', 'Expires')}

{formatDate(sub.expires_at)}

{/* Expiry warning */} {isExpiringSoon && (
{pickLocaleText( locale, `即将到期,剩余 ${remaining} 天`, `Expiring soon, ${remaining} days remaining`, )}
)} {/* Usage stats */}
{pickLocaleText(locale, '日用量', 'Daily')}

${sub.daily_usage_usd.toFixed(2)}

{pickLocaleText(locale, '周用量', 'Weekly')}

${sub.weekly_usage_usd.toFixed(2)}

{pickLocaleText(locale, '月用量', 'Monthly')}

${sub.monthly_usage_usd.toFixed(2)}

); })}
); }