"use client"; import { useEffect, useState } from "react"; import { Bell, BellOff } from "lucide-react"; import { useAuth } from "@/hooks/useAuth"; import { errorMessage } from "@/lib/utils"; import { apiV2Data } from "@/lib/apiV2"; /** Follow / unfollow a series. Signed out, it links to the login page and back. */ export function FollowSeriesButton({ slug }: { slug: string }) { const { user, token, loading } = useAuth(); const [following, setFollowing] = useState(null); const [seriesId, setSeriesId] = useState(null); const [busy, setBusy] = useState(false); const [error, setError] = useState(null); // The page itself is cached for everyone, so whether this person follows (and the series as it // is now) is asked separately. useEffect(() => { if (!token) return; (apiV2Data("get", "/series/by-slug/{slug}", { params: { slug }, authToken: token }) as Promise<{ id: string; following: boolean }>) .then(s => { setSeriesId(s.id); setFollowing(!!s.following); }) .catch(() => setFollowing(null)); }, [slug, token]); if (loading) return null; if (!user) { return ( Log in to follow ); } const toggle = async () => { setBusy(true); setError(null); try { const res = await (apiV2Data(following ? "delete" : "put", "/series/{id}/follow", { params: { id: seriesId! }, authToken: token }) as Promise<{ following: boolean }>); setFollowing(res.following); } catch (e) { setError(errorMessage(e) || "Couldn't change that"); } finally { setBusy(false); } }; return (

{following ? "You'll get an email when a new event in this series is out (if you get news and announcements from us)." : "Get an email when the next one is out."}

{error &&

{error}

}
); }