peppa/apps/web/components/PoolSubscriptions.tsx
2026-11-15 19:11:04 +05:00

51 lines
2.8 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 { Badge } from "@/components/ui/badge";
const hhmm = (d: Date) => d.toLocaleTimeString("ru-RU", { 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 function PoolSubscriptions({ o, action }: { o: PoolOverview; action?: (s: PoolSubscriptionState) => ReactNode }) {
if (!o.subscriptions.length) return <p className="m-0 text-sm text-muted-foreground">В пуле нет подписок.</p>;
return (
<ul className="m-0 flex list-none flex-col gap-1.5 p-0" aria-label="Подписки проекта">
{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)}>{s.label}</Badge>
{s.usage && !s.limitedUntil && (
<span className={`text-xs ${s.usage.pct! >= 85 ? "text-warn" : "text-muted-foreground"}`}>
использовано {Math.round(s.usage.pct!)}% · окно {s.usage.label}
{s.usage.resetsAt ? ` · сброс ${hhmm(s.usage.resetsAt)}` : ""}
</span>
)}
{s.loan && <Badge variant="info">запасная до {hhmm(s.loan.until)}</Badge>}
{action?.(s)}
</li>
))}
</ul>
);
}
/** One line under the list: is there a spare, does anyone wait. */
export function PoolSummary({ o }: { o: PoolOverview }) {
const parts: string[] = [];
if (o.usableCount === 0) parts.push("рабочих подписок нет — агенты ждут сброса");
else if (o.usableCount === 1) parts.push("запасной подписки нет: на лимите агенты встанут до сброса");
else parts.push(`рабочих подписок: ${o.usableCount} — на лимите агент переедет на свободную`);
if (o.waiting) parts.push(`ждут сброса: ${o.waiting}`);
return <p className={`m-0 text-xs ${o.usableCount <= 1 ? "text-warn" : "text-muted-foreground"}`}>{parts.join(" · ")}</p>;
}