douyinWEB/frontend/src/components/OpsNekoHubCredentialsCard.tsx
eric dea036c50d Prefer YouTube receiving/Studio on-air for live-map and sync assign lives to hub.
Tighten http/youtubei live probes, classify Studio encoder_lost vs waiting, and keep dy/dylive public live counts aligned via edge assign_lives.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-05 22:57:52 -05:00

160 lines
5.7 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.

"use client";
import { useCallback, useEffect, useState } from "react";
import { Check, Copy, KeyRound, Link2, Loader2, RefreshCw } from "lucide-react";
import { useToast } from "@/components/Toast";
import { fetchJson } from "@/lib/api";
type Creds = {
profile?: string;
domain?: string;
token?: string;
ready?: boolean;
role?: string;
};
/** VPS assign hero: copyable hub domain + token for physical Neko pairing. */
export function OpsNekoHubCredentialsCard() {
const toast = useToast();
const [creds, setCreds] = useState<Creds | null>(null);
const [loading, setLoading] = useState(true);
const [busy, setBusy] = useState(false);
const [copied, setCopied] = useState<"domain" | "token" | "both" | "">("");
const load = useCallback(async () => {
try {
const d = (await fetchJson("/api/ops/neko/hub/credentials")) as Creds;
setCreds(d);
} catch (e) {
toast.push((e as Error).message, "err");
} finally {
setLoading(false);
}
}, [toast]);
useEffect(() => {
void load();
}, [load]);
async function rotate() {
setBusy(true);
try {
const d = (await fetchJson("/api/ops/neko/hub/credentials", {
method: "POST",
body: JSON.stringify({ rotate_token: true }),
})) as Creds;
setCreds(d);
toast.push("已生成新 Token(请同步到物理机)", "ok");
} catch (e) {
toast.push((e as Error).message, "err");
} finally {
setBusy(false);
}
}
async function copy(text: string, which: "domain" | "token" | "both") {
try {
await navigator.clipboard.writeText(text);
setCopied(which);
toast.push("已复制", "ok");
window.setTimeout(() => setCopied(""), 1500);
} catch {
toast.push("复制失败", "err");
}
}
// Only show on VPS profile
if (!loading && creds?.profile && creds.profile !== "vps") {
return null;
}
if (loading && !creds) {
return (
<div className="rounded-2xl border border-teal-500/20 bg-teal-500/[0.06] px-3.5 py-3 text-xs text-slate-400">
<Loader2 className="mr-1 inline h-3.5 w-3.5 animate-spin" />
加载物理机配对凭证…
</div>
);
}
const domain = String(creds?.domain || "").trim();
const token = String(creds?.token || "").trim();
return (
<div className="rounded-2xl border border-teal-500/25 bg-gradient-to-br from-teal-500/[0.12] to-black/30 px-3.5 py-3">
<div className="flex flex-wrap items-start justify-between gap-2">
<div className="min-w-0">
<div className="flex items-center gap-1.5 text-[10px] font-medium uppercase tracking-[0.16em] text-teal-200/80">
<KeyRound className="h-3.5 w-3.5" />
物理机 Neko 配对
</div>
<p className="mt-0.5 text-xs text-slate-400">
粘贴一次域名 + Token 并连接后,后台每 5 秒自动同步;启停实例立刻推送。VPS 分配页下拉会实时更新。
</p>
</div>
<div className="flex flex-wrap gap-1.5">
<button
type="button"
disabled={busy || !domain || !token}
onClick={() => void copy(`${domain}\n${token}`, "both")}
className="inline-flex items-center gap-1 rounded-lg border border-teal-400/30 bg-teal-500/15 px-2.5 py-1.5 text-[11px] text-teal-50 hover:bg-teal-500/25 disabled:opacity-40"
>
{copied === "both" ? <Check className="h-3 w-3" /> : <Copy className="h-3 w-3" />}
复制全部
</button>
<button
type="button"
disabled={busy}
onClick={() => void rotate()}
className="inline-flex items-center gap-1 rounded-lg border border-white/10 bg-white/5 px-2.5 py-1.5 text-[11px] text-slate-300 hover:bg-white/10"
>
{busy ? <Loader2 className="h-3 w-3 animate-spin" /> : <RefreshCw className="h-3 w-3" />}
换 Token
</button>
</div>
</div>
<div className="mt-3 grid gap-2 sm:grid-cols-2">
<label className="block min-w-0">
<span className="text-[10px] text-slate-500">域名(Hub)</span>
<div className="mt-1 flex gap-1.5">
<input
readOnly
value={domain}
className="min-w-0 flex-1 truncate rounded-xl border border-white/10 bg-black/40 px-3 py-2 font-mono text-xs text-slate-100"
/>
<button
type="button"
disabled={!domain}
onClick={() => void copy(domain, "domain")}
className="shrink-0 rounded-xl border border-white/10 bg-white/5 px-2.5 text-slate-300 hover:bg-white/10"
title="复制域名"
>
{copied === "domain" ? <Check className="h-3.5 w-3.5" /> : <Link2 className="h-3.5 w-3.5" />}
</button>
</div>
</label>
<label className="block min-w-0">
<span className="text-[10px] text-slate-500">Token</span>
<div className="mt-1 flex gap-1.5">
<input
readOnly
value={token}
className="min-w-0 flex-1 truncate rounded-xl border border-white/10 bg-black/40 px-3 py-2 font-mono text-xs text-slate-100"
/>
<button
type="button"
disabled={!token}
onClick={() => void copy(token, "token")}
className="shrink-0 rounded-xl border border-white/10 bg-white/5 px-2.5 text-slate-300 hover:bg-white/10"
title="复制 Token"
>
{copied === "token" ? <Check className="h-3.5 w-3.5" /> : <Copy className="h-3.5 w-3.5" />}
</button>
</div>
</label>
</div>
</div>
);
}