"use client" import { useOptimistic } from "react" import { followUser, unfollowUser } from "@/app/actions/social" import { Button } from "@/components/ui/button" import { toast } from "sonner" import { UserPlus, UserMinus } from "lucide-react" interface FollowButtonProps { username: string isInitiallyFollowing: boolean } export function FollowButton({ username, isInitiallyFollowing }: FollowButtonProps) { const [optimisticFollowing, setOptimisticFollowing] = useOptimistic(isInitiallyFollowing) async function handleClick() { const next = !optimisticFollowing setOptimisticFollowing(next) try { await (next ? followUser(username) : unfollowUser(username)) } catch { setOptimisticFollowing(!next) // revert toast.error(`Failed to ${next ? "follow" : "unfollow"} user.`) } } return ( ) }