|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useEffect, useState } from "react"; |
| 4 | +import { useAuth } from "@/lib/use-auth"; |
| 5 | + |
| 6 | +interface Props { |
| 7 | + /** 目标用户的 identifier(/u/{identifier},数字或 username 都行) */ |
| 8 | + identifier: string; |
| 9 | + /** 目标用户的 user_accounts.id,用于判断"不能关注自己" */ |
| 10 | + targetUserId: number; |
| 11 | +} |
| 12 | + |
| 13 | +interface StatsResponse { |
| 14 | + success: boolean; |
| 15 | + data?: { |
| 16 | + userId: number; |
| 17 | + followerCount: number; |
| 18 | + followingCount: number; |
| 19 | + isFollowing: boolean; |
| 20 | + }; |
| 21 | +} |
| 22 | + |
| 23 | +function readToken(): string | null { |
| 24 | + if (typeof window === "undefined") return null; |
| 25 | + try { |
| 26 | + return localStorage.getItem("satoken"); |
| 27 | + } catch { |
| 28 | + return null; |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +/** |
| 33 | + * 个人主页上的关注按钮 + 统计。 |
| 34 | + * - 匿名用户显示 followers/following 数字但按钮置灰提示"登录后关注" |
| 35 | + * - 自己访问自己主页时不显示按钮,只显示统计 |
| 36 | + * - 乐观更新:点击立即切换 UI,失败回滚 |
| 37 | + */ |
| 38 | +export function FollowButton({ identifier, targetUserId }: Props) { |
| 39 | + const { user, status } = useAuth(); |
| 40 | + const [followerCount, setFollowerCount] = useState<number | null>(null); |
| 41 | + const [followingCount, setFollowingCount] = useState<number | null>(null); |
| 42 | + const [isFollowing, setIsFollowing] = useState(false); |
| 43 | + const [pending, setPending] = useState(false); |
| 44 | + |
| 45 | + const isSelf = user?.id === targetUserId; |
| 46 | + |
| 47 | + // 初次载入拉 stats |
| 48 | + useEffect(() => { |
| 49 | + const token = readToken(); |
| 50 | + fetch(`/api/user-center/follows/stats/${encodeURIComponent(identifier)}`, { |
| 51 | + headers: token ? { satoken: token } : {}, |
| 52 | + }) |
| 53 | + .then((r) => (r.ok ? (r.json() as Promise<StatsResponse>) : null)) |
| 54 | + .then((json) => { |
| 55 | + if (!json?.success || !json.data) return; |
| 56 | + setFollowerCount(json.data.followerCount); |
| 57 | + setFollowingCount(json.data.followingCount); |
| 58 | + setIsFollowing(json.data.isFollowing); |
| 59 | + }) |
| 60 | + .catch(() => {}); |
| 61 | + }, [identifier]); |
| 62 | + |
| 63 | + async function toggle() { |
| 64 | + if (status !== "authenticated" || isSelf || pending) return; |
| 65 | + const token = readToken(); |
| 66 | + if (!token) return; |
| 67 | + |
| 68 | + const nextFollowing = !isFollowing; |
| 69 | + // 乐观更新 |
| 70 | + setPending(true); |
| 71 | + setIsFollowing(nextFollowing); |
| 72 | + setFollowerCount((c) => |
| 73 | + c == null ? c : Math.max(0, c + (nextFollowing ? 1 : -1)), |
| 74 | + ); |
| 75 | + |
| 76 | + try { |
| 77 | + const res = await fetch( |
| 78 | + `/api/user-center/follows/${encodeURIComponent(identifier)}`, |
| 79 | + { |
| 80 | + method: nextFollowing ? "POST" : "DELETE", |
| 81 | + headers: { satoken: token }, |
| 82 | + }, |
| 83 | + ); |
| 84 | + if (!res.ok) { |
| 85 | + // 回滚 |
| 86 | + setIsFollowing(!nextFollowing); |
| 87 | + setFollowerCount((c) => |
| 88 | + c == null ? c : Math.max(0, c - (nextFollowing ? 1 : -1)), |
| 89 | + ); |
| 90 | + } else { |
| 91 | + // 以服务端为准修正计数 |
| 92 | + const json = (await res.json()) as StatsResponse; |
| 93 | + if (json.success && json.data) { |
| 94 | + setFollowerCount(json.data.followerCount); |
| 95 | + setFollowingCount(json.data.followingCount); |
| 96 | + setIsFollowing(json.data.isFollowing); |
| 97 | + } |
| 98 | + } |
| 99 | + } catch { |
| 100 | + // 网络异常回滚 |
| 101 | + setIsFollowing(!nextFollowing); |
| 102 | + setFollowerCount((c) => |
| 103 | + c == null ? c : Math.max(0, c - (nextFollowing ? 1 : -1)), |
| 104 | + ); |
| 105 | + } finally { |
| 106 | + setPending(false); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + return ( |
| 111 | + <div className="flex items-center gap-4 border-t border-[var(--foreground)] pt-4"> |
| 112 | + <div className="flex gap-4 font-mono text-[10px] uppercase tracking-widest text-neutral-500"> |
| 113 | + <span> |
| 114 | + <strong className="text-[var(--foreground)] font-serif text-base font-black"> |
| 115 | + {followerCount?.toLocaleString() ?? "—"} |
| 116 | + </strong>{" "} |
| 117 | + 粉丝 |
| 118 | + </span> |
| 119 | + <span> |
| 120 | + <strong className="text-[var(--foreground)] font-serif text-base font-black"> |
| 121 | + {followingCount?.toLocaleString() ?? "—"} |
| 122 | + </strong>{" "} |
| 123 | + 关注 |
| 124 | + </span> |
| 125 | + </div> |
| 126 | + {!isSelf && ( |
| 127 | + <button |
| 128 | + type="button" |
| 129 | + disabled={status !== "authenticated" || pending} |
| 130 | + onClick={toggle} |
| 131 | + className={[ |
| 132 | + "font-mono text-[10px] uppercase tracking-widest px-3 py-1.5 border transition-colors", |
| 133 | + isFollowing |
| 134 | + ? "border-[var(--foreground)] text-[var(--foreground)] hover:bg-[#CC0000] hover:border-[#CC0000] hover:text-[var(--background)]" |
| 135 | + : "border-[var(--foreground)] bg-[var(--foreground)] text-[var(--background)] hover:bg-[#CC0000] hover:border-[#CC0000]", |
| 136 | + "disabled:opacity-50 disabled:cursor-not-allowed", |
| 137 | + ].join(" ")} |
| 138 | + data-umami-event={isFollowing ? "unfollow_click" : "follow_click"} |
| 139 | + > |
| 140 | + {status !== "authenticated" |
| 141 | + ? "登录后关注" |
| 142 | + : pending |
| 143 | + ? "..." |
| 144 | + : isFollowing |
| 145 | + ? "已关注" |
| 146 | + : "+ 关注"} |
| 147 | + </button> |
| 148 | + )} |
| 149 | + </div> |
| 150 | + ); |
| 151 | +} |
0 commit comments