"use client"; import { ShieldCheck } from "lucide-react"; import { useRouter } from "next/navigation"; import { type FormEvent, useState } from "react"; async function api(path: string, method: string, body: Record) { const response = await fetch(path, { method, headers: { "Content-Type": "application/json" }, body: JSON.stringify(body), }); const payload = await response.json().catch(() => ({})); if (!response.ok) throw new Error(payload.error || "操作失败,请稍后再试。"); return payload; } function Notice({ text }: { text: string }) { return text ?

{text}

: null; } export function FamilyPostForm({ deviceUuid }: { deviceUuid: string }) { const router = useRouter(); const [notice, setNotice] = useState(""); const [busy, setBusy] = useState(false); async function submit(event: FormEvent) { event.preventDefault(); setBusy(true); const form = new FormData(event.currentTarget); try { await api("/api/family-feed", "POST", { deviceUuid, content: form.get("content"), visibility: form.get("visibility"), }); event.currentTarget.reset(); setNotice("已发布到家庭动态。"); router.refresh(); } catch (error) { setNotice(error instanceof Error ? error.message : "发布失败。"); } finally { setBusy(false); } } return (