e07686f29d
Expose active subscription summary on user list and detail so operators can see plan type, total/used/remaining tokens without opening billing subscriptions. Co-authored-by: Cursor <cursoragent@cursor.com>
37 lines
1.3 KiB
TypeScript
37 lines
1.3 KiB
TypeScript
import type { AdminSubscription, PlanDefinition, UserSubscriptionSummary } from '../../types';
|
||
|
||
export function formatTokenCount(value: number) {
|
||
return value.toLocaleString('zh-CN');
|
||
}
|
||
|
||
export function formatTokenQuota(limit: number, used: number) {
|
||
if (limit === 0) {
|
||
return `已用 ${formatTokenCount(used)} / 无限`;
|
||
}
|
||
const remaining = Math.max(0, limit - used);
|
||
return `${formatTokenCount(remaining)} / ${formatTokenCount(limit)}(已用 ${formatTokenCount(used)})`;
|
||
}
|
||
|
||
export function resolvePlanLabel(planType: string, plans: PlanDefinition[]) {
|
||
return plans.find((plan) => plan.planType === planType)?.name ?? planType;
|
||
}
|
||
|
||
export function subscriptionStatusLabel(status: AdminSubscription['status']) {
|
||
if (status === 'active') return '有效';
|
||
if (status === 'expired') return '已到期';
|
||
if (status === 'cancelled') return '已取消';
|
||
return status;
|
||
}
|
||
|
||
export function summarizeSubscription(
|
||
subscription: Pick<UserSubscriptionSummary, 'planType' | 'periodTokensLimit' | 'periodTokensUsed'> | null | undefined,
|
||
fallbackPlanType?: string,
|
||
) {
|
||
if (!subscription) {
|
||
return fallbackPlanType && fallbackPlanType !== 'free'
|
||
? `${fallbackPlanType}(无有效订阅)`
|
||
: '免费 / 无订阅';
|
||
}
|
||
return formatTokenQuota(subscription.periodTokensLimit, subscription.periodTokensUsed);
|
||
}
|