import { Link } from "@tanstack/react-router"; import { useState } from "react"; import { listFollows, setFollow } from "~/fns/profile"; import { useOptimisticMutation } from "~/hooks/use-optimistic-mutation"; import { Button } from "~/ui/Button"; import { Avatar } from "~/ui/Avatar"; type Person = { name: string; display_name: string | null; avatar_key: string | null }; type Props = { name: string; followers: number; following: number; isFollowing: boolean; canFollow: boolean }; export function FollowPanel({ name, followers, following, isFollowing, canFollow }: Props) { const follow = useOptimisticMutation( { isFollowing, followers }, (state, next: boolean) => ({ isFollowing: next, followers: Math.max(0, state.followers + (next ? 1 : -1)) }), (next: boolean) => setFollow({ data: { name, follow: next } }), ); const [list, setList] = useState<{ direction: "followers" | "following"; people: Person[] } | null>(null); async function show(direction: "followers" | "following") { if (list?.direction === direction) return setList(null); setList({ direction, people: await listFollows({ data: { name, direction } }) }); } return (
{canFollow && ( )}
{follow.error &&

{follow.error}

} {list && ( )}
); }