"use client"; import { useState } from "react"; import { Heart } from "lucide-react"; import { apiToggleLike } from "@/lib/api"; interface LikeButtonProps { postId: string; liked: boolean; likeCount: number; } export default function LikeButton({ postId, liked: initialLiked, likeCount: initialCount }: LikeButtonProps) { const [liked, setLiked] = useState(initialLiked); const [count, setCount] = useState(initialCount); const [loading, setLoading] = useState(false); const handleClick = async () => { if (loading) return; setLoading(true); try { const res = await apiToggleLike(postId); if (res.error === "未登录") { window.location.href = "/login"; return; } if (typeof res.liked === "boolean") { setLiked(res.liked); setCount(res.like_count ?? count); } } finally { setLoading(false); } }; return ( ); }