"use client"; import { useRef, useState } from "react"; import Link from "next/link"; import { Loader2, Upload } from "lucide-react"; import { apiApplyFriendLink, apiUploadPostImage } from "@/lib/api"; import { densitySrcSet, isOwnImage, pickVariantWidth, variantURL } from "@/lib/responsiveImage"; interface Props { isLoggedIn: boolean; canManage: boolean; /** 关闭所在弹窗(取消按钮);弹窗外渲染时可不传 */ onClose?: () => void; } interface FormState { name: string; url: string; reciprocal_url: string; logo_url: string; } const EMPTY_FORM: FormState = { name: "", url: "", reciprocal_url: "", logo_url: "", }; const FIELD_CLASS = "w-full rounded-lg px-3 py-2 text-[14px] outline-none transition-colors"; const FIELD_STYLE = { background: "var(--panel-2)", color: "var(--ink)", border: "1px solid var(--line)", }; export default function LinksClient({ isLoggedIn, canManage, onClose }: Props) { const [form, setForm] = useState(EMPTY_FORM); const [submitting, setSubmitting] = useState(false); const [uploading, setUploading] = useState(false); const [msg, setMsg] = useState<{ ok: boolean; text: string } | null>(null); const fileRef = useRef(null); if (!isLoggedIn) { return (

请先 登录 后申请友情链接。

); } const set = (k: keyof FormState, v: string) => setForm((f) => ({ ...f, [k]: v })); const onPickFile = async (file: File | undefined) => { if (!file || uploading) return; setUploading(true); setMsg(null); try { const res = await apiUploadPostImage(file, file.name); if (res.url) { set("logo_url", res.url); } else { setMsg({ ok: false, text: res.error || "上传失败,请稍后重试" }); } } catch { setMsg({ ok: false, text: "上传失败,请稍后重试" }); } finally { setUploading(false); if (fileRef.current) fileRef.current.value = ""; } }; const onSubmit = async (e: React.FormEvent) => { e.preventDefault(); if (submitting) return; setMsg(null); setSubmitting(true); try { await apiApplyFriendLink({ name: form.name.trim(), url: form.url.trim(), reciprocal_url: form.reciprocal_url.trim() || undefined, logo_url: form.logo_url.trim() || undefined, }); setMsg({ ok: true, text: "申请已提交,待管理员审核通过后展示。" }); setForm(EMPTY_FORM); } catch (e) { setMsg({ ok: false, text: e instanceof Error ? e.message : "提交失败,请重试" }); } finally { setSubmitting(false); } }; return (
填写或上传本站LOGO
{/* 预览 */} {form.logo_url ? ( // 64px 预览位:本站图走 96/160 变体;外链地址保持原样 // eslint-disable-next-line @next/next/no-img-element { (e.target as HTMLImageElement).style.display = "none"; }} /> ) : ( "预览" )} set("logo_url", e.target.value)} className={FIELD_CLASS} style={FIELD_STYLE} placeholder="LOGO图片地址" /> onPickFile(e.target.files?.[0])} />
{msg ? (

{msg.text}

) : null}

提交后进入待审核状态,可在「我的申请」中查看进度或修改

{canManage ? (

管理员可前往{" "} 友情链接管理 {" "} 审批

) : null}
); }