"use client";
import { useEffect, useState } from "react";
import { useRouter, useSearchParams, usePathname } from "next/navigation";
import { SubscribeWithHandle, AtSubscribeSuccess } from "./HandleSubscribe";
import { EmailInput, EmailButton, EmailConfirm } from "./EmailSubscribe";
import { EmailSubscribeSuccess } from "./EmailSubscribeSuccess";
import { LinkIdentityModal } from "./LinkIdentityModal";
import {
SUBSCRIBE_ERROR_MESSAGES as ERROR_MESSAGES,
type SubscribeError,
} from "./subscribeErrors";
import { Modal } from "components/Modal";
import { ButtonPrimary } from "components/Buttons";
import { ManageSubscription } from "./ManageSubscribe";
import { useToaster } from "components/Toast";
import { useIdentityData } from "components/IdentityProvider";
import { AtmosphereAccount } from "components/Icons/AtmosphereAccount";
import { EmailTiny } from "components/Icons/EmailTiny";
import { Menu, MenuItem, RadioMenuGroup, RadioMenuItem } from "components/Menu";
import { RSSTiny } from "components/Icons/RSSTiny";
import {
requestPublicationEmailSubscription,
confirmPublicationEmailSubscription,
} from "actions/publications/subscribeEmail";
import { useViewerSubscription } from "./viewerSubscription";
import { Separator } from "components/Layout";
import { ArrowDownTiny } from "components/Icons/ArrowDownTiny";
type SubscribeMode = "email" | "atproto";
export type SubscribeProps = {
autoFocus?: boolean;
compact?: boolean;
publicationUri: string;
publicationUrl?: string;
publicationName: string;
publicationDescription?: string;
newsletterMode: boolean;
};
export const SubscribePanel = (props: SubscribeProps) => {
return (
{props.publicationName}
{props.publicationDescription && (
{props.publicationDescription}
)}
);
};
export const SubscribeInput = (props: SubscribeProps) => {
let toaster = useToaster();
let router = useRouter();
let pathname = usePathname();
let searchParams = useSearchParams();
const user = useViewerSubscription(props.publicationUri);
const { identity, mutate: mutateIdentity } = useIdentityData();
let [email, setEmail] = useState(user.email ?? "");
let [confirmState, setConfirmState] = useState<"confirm" | "success">(
"confirm",
);
let [confirmOpen, setConfirmOpen] = useState(false);
let [requesting, setRequesting] = useState(false);
let [confirming, setConfirming] = useState(false);
let [locallySubscribed, setLocallySubscribed] = useState(false);
let [linkModalOpen, setLinkModalOpen] = useState(false);
let [subscribeMode, setSubscribeMode] = useState("email");
let [atSuccessOpen, setAtSuccessOpen] = useState(false);
// Tracks that the user passed through LinkIdentityModal — when they enter
// the confirmation code we attach the email to their current atp identity
// (or merge from any existing email-only identity) instead of creating a
// disconnected email-only account.
let [linkToCurrent, setLinkToCurrent] = useState(false);
const viewerHandle = identity?.bsky_profiles?.handle;
const viewerAtpDid = identity?.atp_did;
const viewerEmail = identity?.email;
// The atp-only-but-subscribing-via-email case: signed in as a Bluesky
// account with no email yet. The modal asks them to link the typed email
// (or log out) before we send a confirmation code.
const needsLinkConfirmation = !!viewerAtpDid && !viewerEmail && !!email;
// Embedded subscribe forms (see /api/subscribe_email) redirect back here
// with `subscribe_email=` after sending the confirmation code. Open
// the confirm modal so the user can paste the code from their inbox without
// re-entering their email. `subscribe_email_confirmed=1` means the user was
// already verified server-side and the modal jumps straight to success.
useEffect(() => {
if (!props.newsletterMode) return;
const incomingEmail = searchParams.get("subscribe_email");
const alreadyConfirmed =
searchParams.get("subscribe_email_confirmed") === "1";
const errorCode = searchParams.get("subscribe_email_error");
if (!incomingEmail && !errorCode) return;
if (errorCode) {
const message =
ERROR_MESSAGES[errorCode as SubscribeError] ??
"We couldn't process that subscription. Try again.";
toaster({ type: "error", content: message });
} else if (incomingEmail) {
setEmail(incomingEmail);
setConfirmState(alreadyConfirmed ? "success" : "confirm");
setConfirmOpen(true);
if (alreadyConfirmed) {
setLocallySubscribed(true);
router.refresh();
}
}
const next = new URLSearchParams(searchParams.toString());
next.delete("subscribe_email");
next.delete("subscribe_email_confirmed");
next.delete("subscribe_email_error");
const qs = next.toString();
router.replace(qs ? `${pathname}?${qs}` : pathname);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [props.newsletterMode]);
// The atproto subscribe flow pops oauth into a new tab when the form is
// embedded in an iframe (see HandleSubscribe.redirectToOauthForSubscribe).
// The oauth callback writes the subscription and redirects back here with
// `showSubscribeSuccess=true`; open the success modal and clear the param.
useEffect(() => {
if (searchParams.get("showSubscribeSuccess") !== "true") return;
setAtSuccessOpen(true);
setLocallySubscribed(true);
mutateIdentity();
const next = new URLSearchParams(searchParams.toString());
next.delete("showSubscribeSuccess");
const qs = next.toString();
router.replace(qs ? `${pathname}?${qs}` : pathname);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
const sendRequest = async (link: boolean) => {
setRequesting(true);
setLinkToCurrent(link);
let res = await requestPublicationEmailSubscription(
props.publicationUri,
email,
);
setRequesting(false);
if (!res.ok) {
toaster({ type: "error", content: ERROR_MESSAGES[res.error] });
return;
}
if (res.value.confirmed) {
setConfirmState("success");
router.refresh();
}
setConfirmOpen(true);
};
const showManage = props.newsletterMode
? user.emailSubscribed
: user.atprotoSubscribed;
const isSubscribed = showManage || locallySubscribed;
const modeMenu = (
);
return (
<>
{isSubscribed ? (
<>
{props.newsletterMode &&
user.atprotoSubscribed &&
!user.emailSubscribed ? (
Opt in to get updates via email!
{
if (requesting) return;
if (needsLinkConfirmation) {
setLinkModalOpen(true);
return;
}
await sendRequest(false);
}}
>
Get Emails
}
/>
) : null}
>
) : props.newsletterMode ? (
{user.loggedIn && user.email ? (
setLocallySubscribed(true)}
/>
) : subscribeMode === "email" ? (
{
if (!email || requesting) return;
if (needsLinkConfirmation) {
setLinkModalOpen(true);
return;
}
await sendRequest(false);
}}
>
Subscribe
}
/>
) : (
setAtSuccessOpen(true)}
leading={modeMenu}
/>
)}
) : (
setLocallySubscribed(true)}
/>
)}
{props.newsletterMode && needsLinkConfirmation && (
{
setLinkModalOpen(false);
await sendRequest(true);
}}
/>
)}
{/* Shown after an atproto subscribe completes — either inline (the
newsletter "subscribe via Atmosphere" branch) or when the oauth flow
redirects back with `showSubscribeSuccess` after popping login into a
new tab from an embedded iframe. Rendered for every pub, not just
newsletters, so the iframe success path works on atproto-only pubs. */}
{
setAtSuccessOpen(open);
if (!open) {
setLocallySubscribed(true);
mutateIdentity();
router.refresh();
}
}}
>
{props.newsletterMode && (
<>
{
setConfirmOpen(open);
if (!open) {
if (confirmState === "success") setLocallySubscribed(true);
setConfirmState("confirm");
setLinkToCurrent(false);
}
}}
>
{confirmState === "success" ? (
) : (
setConfirmOpen(false)}
emailValue={email}
onSubmit={async (code) => {
if (confirming) return;
setConfirming(true);
let res = await confirmPublicationEmailSubscription(
props.publicationUri,
email,
code,
linkToCurrent,
);
setConfirming(false);
if (!res.ok) {
toaster({
type: "error",
content: ERROR_MESSAGES[res.error],
});
return;
}
setConfirmState("success");
router.refresh();
}}
/>
)}
>
)}
>
);
};
export const SubscribeButton = (props: SubscribeProps) => {
const user = useViewerSubscription(props.publicationUri);
let [locallySubscribed, setLocallySubscribed] = useState(false);
const showManage = props.newsletterMode
? user.emailSubscribed
: user.atprotoSubscribed;
if (showManage || locallySubscribed) {
return (
);
}
if (!props.newsletterMode && user.loggedIn && user.handle) {
return (
setLocallySubscribed(true)}
/>
);
}
if (props.newsletterMode && user.loggedIn && user.email) {
return (
setLocallySubscribed(true)}
/>
);
}
// Nothing to one-click with — either logged out, or logged in but missing the
// identity this pub needs (a handle for atproto pubs, an email for
// newsletters). Both open the full SubscribePanel (pub name/description +
// form) in a modal.
return (
Subscribe
}
>
);
};
const SubscribeInputModeMenu = (props: {
mode: SubscribeMode;
onChange: (mode: SubscribeMode) => void;
}) => {
return (
);
};
export type SubscribeButtonModeMenuAccount = {
value: SubscribeMode;
label: string;
icon: React.ReactNode;
// The account the main button currently subscribes with — marked in the menu.
selected: boolean;
// Subscribe with this account directly, rather than just toggling selection.
onSelect: () => void;
};
// The caret dropdown beside the compact subscribe button. Each account is a
// one-click subscribe (it runs the subscribe action, not just a toggle), and
// the optional RSS item links the feed. Shared by the atproto (HandleSubscribe)
// and email (EmailSubscribe) buttons.
export const SubscribeButtonModeMenu = (props: {
disabled: boolean;
publicationUrl?: string;
accounts: SubscribeButtonModeMenuAccount[];
}) => {
let selectedValue =
props.accounts.find((a) => a.selected)?.value ?? props.accounts[0]?.value;
return (
);
};