"use client"; import { useState } from "react"; type PushTestPanelProps = { initialSubscriptionCount: number; deviceCount: number; }; type PushFailureDetail = { endpointHost?: string; statusCode?: number | null; message?: string; body?: string | null; reason?: string | null; }; export function PushTestPanel({ initialSubscriptionCount, deviceCount, }: PushTestPanelProps) { const [submitting, setSubmitting] = useState(false); const [subscriptionCount, setSubscriptionCount] = useState( initialSubscriptionCount, ); const [notice, setNotice] = useState(null); const [failureDetails, setFailureDetails] = useState([]); async function handleTestPush() { setSubmitting(true); setNotice(null); setFailureDetails([]); try { const response = await fetch("/api/push/test", { method: "POST", }); const payload = (await response.json().catch(() => null)) as | { error?: string; targetedCount?: number; sentCount?: number; removedCount?: number; failedCount?: number; failureDetails?: PushFailureDetail[]; } | null; if (!response.ok) { throw new Error(payload?.error || "测试推送发送失败。",); } const targetedCount = payload?.targetedCount || 0; const removedCount = payload?.removedCount || 0; const sentCount = payload?.sentCount || 0; const failedCount = payload?.failedCount || 0; const nextFailureDetails = Array.isArray(payload?.failureDetails) ? payload.failureDetails : []; setSubscriptionCount(Math.max(0, targetedCount - removedCount)); setFailureDetails(nextFailureDetails); if (targetedCount === 0) { setNotice("当前账号还没有任何开启消息提醒的设备,先在要接收通知的设备上打开消息提醒。\n"); return; } const fragments = [`已向 ${targetedCount} 台设备发出测试通知。`]; if (sentCount > 0) { fragments.push(`成功送出 ${sentCount} 条。`); } if (removedCount > 0) { fragments.push(`清理了 ${removedCount} 个失效订阅。`); } if (failedCount > 0) { fragments.push(`${failedCount} 台设备本次发送失败。`); } setNotice(`${fragments.join(" ")}\n`); } catch (error) { setNotice( error instanceof Error ? error.message : "测试推送发送失败。", ); } finally { setSubmitting(false); } } return (

推送测试

给当前账号的所有设备发一条测试通知

用来确认这位家属账号下已经开启提醒的设备,是否都能正常收到推送。

已连接设备 {deviceCount} 已开启提醒的设备 {subscriptionCount}
{notice ? (

{notice}

) : null} {failureDetails.length > 0 ? (
{failureDetails.map((detail, index) => (

{detail.endpointHost || "unknown"} {typeof detail.statusCode === "number" ? ` · HTTP ${detail.statusCode}` : ""}

{detail.reason || detail.message || "推送发送失败"}

{detail.body ?

{detail.body}

: null}
))}
) : null}
); }