peppa/apps/web/components/PoolSubscriptions.tsx
Асель 34a4024224 Merge main into task/pep-98: PEP-107 subscription texts through the dictionary
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 16:16:16 +05:00

56 lines
3.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import type { ReactNode } from "react";
import type { PoolOverview, PoolSubscriptionState } from "@peppa/core";
import { intlLocale, type Locale } from "@peppa/shared/i18n";
import { Badge } from "@/components/ui/badge";
import { getT } from "@/lib/i18n";
const hhmm = (d: Date, locale: Locale) => d.toLocaleTimeString(intlLocale(locale), { hour: "2-digit", minute: "2-digit", timeZone: process.env.PEPPA_TZ || undefined });
const KIND: Record<PoolSubscriptionState["provider"], string> = { claude: "Claude", codex: "ChatGPT", opencode: "OpenCode" };
function tone(s: PoolSubscriptionState): "ok" | "warn" | "danger" | "muted" | "primary" {
if (s.status === "auth_error") return "danger";
if (s.status === "pending") return "muted";
if (s.limitedUntil) return "warn";
return s.activeSessions ? "primary" : "ok";
}
/**
* PEP-107: the subscriptions of a pool as people see them — which one is at work, which is
* free, when a limit resets, how full the usage window is; `action` adds a button per row.
*/
export async function PoolSubscriptions({ o, action }: { o: PoolOverview; action?: (s: PoolSubscriptionState) => ReactNode }) {
const t = await getT();
if (!o.subscriptions.length) return <p className="m-0 text-sm text-muted-foreground">{t("В пуле нет подписок.")}</p>;
return (
<ul className="m-0 flex list-none flex-col gap-1.5 p-0" aria-label={t("Подписки пула «{name}»", { name: o.pool.name })}>
{o.subscriptions.map((s) => (
<li key={s.id} className="flex flex-wrap items-center gap-x-2 gap-y-1 text-sm" data-pool-subscription={s.id}>
<span className="font-medium">{s.name}</span>
<span className="text-xs text-muted-foreground">{KIND[s.provider]}</span>
<Badge variant={tone(s)}>{t.text(s.label)}</Badge>
{s.usage && !s.limitedUntil && (
<span className={`text-xs ${s.usage.pct! >= 85 ? "text-warn" : "text-muted-foreground"}`}>
{s.usage.resetsAt
? t("использовано {pct}% · окно {window} · сброс {time}", { pct: Math.round(s.usage.pct!), window: t(s.usage.label), time: hhmm(s.usage.resetsAt, t.locale) })
: t("использовано {pct}% · окно {window}", { pct: Math.round(s.usage.pct!), window: t(s.usage.label) })}
</span>
)}
{s.loan && <Badge variant="info">{t("запасная до {time}", { time: hhmm(s.loan.until, t.locale) })}</Badge>}
{action?.(s)}
</li>
))}
</ul>
);
}
/** One line under the list: is there a spare, does anyone wait. */
export async function PoolSummary({ o }: { o: PoolOverview }) {
const t = await getT();
const parts: string[] = [];
if (o.usableCount === 0) parts.push(t("рабочих подписок нет — агенты ждут сброса"));
else if (o.usableCount === 1) parts.push(t("запасной подписки нет: на лимите агенты встанут до сброса"));
else parts.push(t("рабочих подписок: {n} — на лимите агент переедет на свободную", { n: o.usableCount }));
if (o.waiting) parts.push(t("ждут сброса: {n}", { n: o.waiting }));
return <p className={`m-0 text-xs ${o.usableCount <= 1 ? "text-warn" : "text-muted-foreground"}`}>{parts.join(" · ")}</p>;
}