"use client"; import Link from "next/link"; import { useRouter } from "next/navigation"; import { startTransition, useRef, useState } from "react"; import { useNavigationProgress } from "@/components/navigation-progress"; export function BindDeviceForm() { const router = useRouter(); const { beginNavigationProgress } = useNavigationProgress(); const [rawCode, setRawCode] = useState(""); const [submitting, setSubmitting] = useState(false); const [notice, setNotice] = useState(null); async function handleSubmit(event: React.FormEvent) { event.preventDefault(); if (!rawCode.trim()) { setNotice("请先粘贴长辈手机上的设备码。\n"); return; } setSubmitting(true); setNotice("正在前往绑定页面,请稍候……"); startTransition(() => { beginNavigationProgress(); router.push(`/bind?deviceUuid=${encodeURIComponent(rawCode.trim())}`); }); } return (

用设备码添加

setRawCode(event.target.value)} placeholder="粘贴长辈屏幕下方的设备码" className="field" style={{ height: 36, flex: 1, minWidth: 0 }} />
{notice ? (

{notice}

) : null}
); } type SendMessageFormProps = { deviceUuid: string; }; export function SendMessageForm({ deviceUuid }: SendMessageFormProps) { const router = useRouter(); const [content, setContent] = useState(""); const [importance, setImportance] = useState("NORMAL"); const [submitting, setSubmitting] = useState(false); const [notice, setNotice] = useState(null); const [createdMessagePublicId, setCreatedMessagePublicId] = useState(null); async function handleSubmit(event: React.FormEvent) { event.preventDefault(); if (!content.trim()) { setNotice("请先写下您想说的话。\n"); setCreatedMessagePublicId(null); return; } setSubmitting(true); setNotice("正在送达这条留言……"); try { const response = await fetch("/api/family/messages", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ deviceUuid, content, importance, }), }); const payload = (await response.json().catch(() => null)) as | { error?: string; publicId?: number } | null; if (!response.ok) { throw new Error(payload?.error || "留言发送失败,请稍后再试。"); } setNotice("留言已发出,长辈下次使用时就能看到。\n"); setCreatedMessagePublicId( typeof payload?.publicId === "number" ? payload.publicId : null, ); setContent(""); setImportance("NORMAL"); startTransition(() => { router.refresh(); }); } catch (error) { setCreatedMessagePublicId(null); setNotice( error instanceof Error ? error.message : "留言发送失败,请稍后再试。", ); } finally { setSubmitting(false); } } return (

给长辈留言