feat: establish NekoNest Cloud control and relay

This commit is contained in:
2026-08-12 23:25:43 +08:00
commit f27606b709
222 changed files with 71456 additions and 0 deletions
@@ -0,0 +1,97 @@
"use client";
import { useRef, useState } from "react";
import { useRouter } from "next/navigation";
import type { BetaAccessRequestRecord } from "@/db/repository";
type SubmitState = { loading: boolean; message: string; error: boolean };
export function AccessRequestForm({
pendingRequest,
}: {
pendingRequest: BetaAccessRequestRecord | null;
}) {
const router = useRouter();
const pendingKey = useRef<string | null>(null);
const [preferredOs, setPreferredOs] = useState("windows");
const [requestedSlots, setRequestedSlots] = useState(1);
const [useCase, setUseCase] = useState("");
const [state, setState] = useState<SubmitState>({ loading: false, message: "", error: false });
async function post(payload: Record<string, unknown>) {
pendingKey.current ??= crypto.randomUUID();
const response = await fetch("/api/beta-access", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({ ...payload, idempotencyKey: pendingKey.current }),
});
const body = (await response.json()) as { message?: string };
if (!response.ok) throw new Error(body.message || "操作失败");
pendingKey.current = null;
}
async function submit(event: React.FormEvent) {
event.preventDefault();
setState({ loading: true, message: "", error: false });
try {
await post({ action: "request", preferredOs, requestedSlots, useCase });
setState({ loading: false, message: "申请已提交,处理结果会显示在本页。", error: false });
router.refresh();
} catch (error) {
setState({ loading: false, message: error instanceof Error ? error.message : "提交失败", error: true });
}
}
async function cancel() {
if (!pendingRequest) return;
setState({ loading: true, message: "", error: false });
try {
await post({ action: "cancel", requestId: pendingRequest.id });
setState({ loading: false, message: "申请已撤回。", error: false });
router.refresh();
} catch (error) {
setState({ loading: false, message: error instanceof Error ? error.message : "撤回失败", error: true });
}
}
if (pendingRequest) {
return (
<div className="cloud-form feedback-form">
<p></p>
<button className="button button-secondary" type="button" onClick={cancel} disabled={state.loading}>
{state.loading ? "正在撤回…" : "撤回这条申请"}
</button>
{state.message && <p className={state.error ? "form-error" : "form-success"} role="status">{state.message}</p>}
</div>
);
}
return (
<form className="cloud-form feedback-form" onSubmit={submit}>
<div className="admin-form-grid">
<label>
<span>使</span>
<select value={preferredOs} onChange={(event) => setPreferredOs(event.target.value)}>
<option value="windows">Windows</option>
<option value="linux">Linux</option>
<option value="both">Windows Linux</option>
</select>
</label>
<label>
<span></span>
<input type="number" min={1} max={3} value={requestedSlots} onChange={(event) => setRequestedSlots(Number(event.target.value))} required />
<small> 13 </small>
</label>
</div>
<label>
<span>使 NekoNest</span>
<textarea minLength={20} maxLength={1000} value={useCase} onChange={(event) => setUseCase(event.target.value)} placeholder="例如:我主要在 Windows 主机上使用 Codex,希望从手机查看并继续已有任务。请勿粘贴令牌、密码、项目代码或私密会话内容。" required />
<small>{useCase.length}/1000 · 使</small>
</label>
<button className="button button-primary" type="submit" disabled={state.loading}>
{state.loading ? "正在提交…" : "申请免费闭测资格"}
</button>
{state.message && <p className={state.error ? "form-error" : "form-success"} role="status">{state.message}</p>}
</form>
);
}
+125
View File
@@ -0,0 +1,125 @@
import { requireCloudViewer } from "../../cloud-auth";
import { DashboardShell, PageHeading, StatusPill, formatDate } from "../../components/Shells";
import { getDashboardSnapshot, getOrCreateAccount } from "@/db/repository";
import { getBillingEntitlementPresentation } from "@/db/domain";
import { deriveInvitationDisplayState } from "@/db/invitations";
import { AccessRequestForm } from "./AccessRequestForm";
export const dynamic = "force-dynamic";
export default async function BillingPage() {
const viewer = await requireCloudViewer("/dashboard/billing");
const account = await getOrCreateAccount(viewer);
const snapshot = await getDashboardSnapshot(account);
const billingCopy = getBillingEntitlementPresentation(
snapshot.entitlement.mode,
snapshot.entitlement.publicBetaState,
);
const now = new Date();
const nowIso = now.toISOString();
const invitations = snapshot.grants.filter((grant) => grant.source === "admin_exemption");
const pendingAccessRequest = snapshot.accessRequests.find((request) => request.status === "requested") ?? null;
const endingSoon = invitations.find((invitation) => {
if (deriveInvitationDisplayState(invitation, nowIso) !== "active" || !invitation.ends_at) return false;
const remaining = new Date(invitation.ends_at).getTime() - now.getTime();
return remaining > 0 && remaining <= 7 * 24 * 60 * 60_000;
});
const available = snapshot.entitlement.unlimited
? "当前不按槽位限额"
: `${snapshot.entitlement.availableSlots ?? 0}`;
return (
<DashboardShell viewer={viewer} active="/dashboard/billing">
<div className="cloud-page">
<PageHeading
eyebrow="PUBLIC BETA / 公测权益"
title={snapshot.entitlement.mode === "none" ? "这里说明当前免费资格状态。" : "当前免费,不需要处理账单。"}
description="这里仅显示公测资格、主机占用和透明容量规则。报价、订单与支付功能暂不开放。"
/>
<section className="billing-status-panel">
<div>
<StatusPill tone={billingCopy.tone}>{billingCopy.status}</StatusPill>
<h2>{billingCopy.title}</h2>
<p>{billingCopy.description}</p>
</div>
<dl>
<div><dt></dt><dd>{snapshot.entitlement.activeSlots}</dd></div>
<div><dt></dt><dd>{snapshot.entitlement.reservedSlots}</dd></div>
<div><dt></dt><dd>{available}</dd></div>
<div><dt></dt><dd>{formatDate(snapshot.entitlement.effectiveUntil)}</dd></div>
</dl>
</section>
{endingSoon && (
<section className="panel full-panel attention-panel" role="status">
<div className="panel-heading"><div><span>CLOSED BETA NOTICE</span><h2></h2></div><StatusPill tone="warn"></StatusPill></div>
<p> {formatDate(endingSoon.ends_at, true)} </p>
</section>
)}
<div className="dashboard-columns billing-columns">
<section className="panel">
<div className="panel-heading"><div><span></span><h2></h2></div></div>
<div className="billing-rules">
<span> </span>
<span> </span>
<span> </span>
<span> </span>
</div>
</section>
<section className="panel">
<div className="panel-heading"><div><span></span><h2></h2></div></div>
<p>使</p>
<div className="empty-inline"></div>
</section>
</div>
<section className="panel full-panel">
<div className="panel-heading">
<div><span>CLOSED BETA / </span><h2></h2></div>
<StatusPill tone={pendingAccessRequest ? "warn" : snapshot.entitlement.mode === "none" ? "neutral" : "good"}>{pendingAccessRequest ? "等待处理" : snapshot.entitlement.mode === "none" ? "可申请" : "已有资格"}</StatusPill>
</div>
{snapshot.entitlement.mode !== "none" ? (
<div className="empty-inline"></div>
) : (
<AccessRequestForm pendingRequest={pendingAccessRequest} />
)}
{snapshot.accessRequests.length > 0 && (
<div className="admin-gate-table invitation-history">
{snapshot.accessRequests.map((request) => (
<article key={request.id}>
<StatusPill tone={request.status === "approved" ? "good" : request.status === "requested" ? "warn" : "neutral"}>{request.status === "approved" ? "已批准" : request.status === "requested" ? "审核中" : request.status === "declined" ? "暂未批准" : "已撤回"}</StatusPill>
<div><strong>{request.requested_slots} · {request.preferred_os === "both" ? "Windows 和 Linux" : request.preferred_os === "windows" ? "Windows" : "Linux"}</strong><small>{request.id}</small></div>
<span>{formatDate(request.requested_at, true)}</span>
<span>{request.admin_response || "尚无处理说明"}</span>
</article>
))}
</div>
)}
</section>
<section className="panel full-panel">
<div className="panel-heading"><div><span>CLOSED BETA / </span><h2></h2></div><StatusPill tone={invitations.some((invitation) => deriveInvitationDisplayState(invitation, nowIso) === "active") ? "good" : "neutral"}>{invitations.filter((invitation) => deriveInvitationDisplayState(invitation, nowIso) === "active").length} </StatusPill></div>
<p></p>
<div className="admin-gate-table invitation-history">
{invitations.length ? invitations.map((invitation) => {
const state = deriveInvitationDisplayState(invitation, nowIso);
return <article key={invitation.id}><StatusPill tone={state === "active" ? "good" : "neutral"}>{state === "active" ? "有效" : state === "expired" ? "已到期" : "已撤销"}</StatusPill><div><strong>{invitation.capacity_slots === null ? "当前不按主机槽位限额" : `${invitation.capacity_slots} 个主机槽位`}</strong><small>{invitation.id}</small></div><span>{formatDate(invitation.starts_at, true)}</span><span>{formatDate(invitation.ends_at, true)}</span></article>;
}) : <div className="empty-inline"></div>}
</div>
</section>
<section className="panel full-panel">
<div className="panel-heading"><div><span></span><h2></h2></div></div>
<div className="billing-rules">
<span>1. </span>
<span>2. </span>
<span>3. </span>
<span>4. 退</span>
</div>
</section>
</div>
</DashboardShell>
);
}