"use client"; import { ButtonPrimary } from "components/Buttons"; import { useActionState, useEffect, useState } from "react"; import { Input } from "components/Input"; import { useIdentityData } from "components/IdentityProvider"; import { confirmEmailAuthToken, requestAuthEmailToken, } from "actions/emailAuth"; import { subscribeToPublicationWithEmail } from "actions/subscribeToPublicationWithEmail"; import { ArrowRightTiny } from "components/Icons/ArrowRightTiny"; import { ShareSmall } from "components/Icons/ShareSmall"; import { Popover } from "components/Popover"; import { BlueskyTiny } from "components/Icons/BlueskyTiny"; import { useToaster } from "components/Toast"; import * as Dialog from "@radix-ui/react-dialog"; import { subscribeToPublication, unsubscribeToPublication, } from "./subscribeToPublication"; import { DotLoader } from "components/utils/DotLoader"; import { addFeed } from "./addFeed"; import { useSearchParams } from "next/navigation"; import LoginForm from "app/login/LoginForm"; import { RSSSmall } from "components/Icons/RSSSmall"; import { SpeedyLink } from "components/SpeedyLink"; type State = | { state: "email" } | { state: "code"; token: string } | { state: "success" }; export const SubscribeButton = (props: { compact?: boolean; publication: string; }) => { let { identity, mutate } = useIdentityData(); let [emailInputValue, setEmailInputValue] = useState(""); let [codeInputValue, setCodeInputValue] = useState(""); let [state, setState] = useState({ state: "email" }); if (state.state === "email") { return (
{ setEmailInputValue(e.currentTarget.value); }} /> { if (identity?.email) { await subscribeToPublicationWithEmail(props.publication); //optimistically could add! await mutate(); return; } let tokenID = await requestAuthEmailToken(emailInputValue); setState({ state: "code", token: tokenID }); }} > {props.compact ? ( ) : ( "Subscribe" )}
{/* */}
); } if (state.state === "code") { return (
Please enter the code we sent to
{emailInputValue}
); } if (state.state === "success") { return (
You're subscribed!
{/* */}
); } }; export const ShareButton = () => { return ( ); }; const ConfirmCodeInput = (props: { codeInputValue: string; token: string; setCodeInputValue: (value: string) => void; setState: (state: State) => void; publication: string; }) => { let { mutate } = useIdentityData(); return (
{ props.setCodeInputValue(e.currentTarget.value); }} /> { console.log( await confirmEmailAuthToken(props.token, props.codeInputValue), ); await subscribeToPublicationWithEmail(props.publication); //optimistically could add! await mutate(); props.setState({ state: "success" }); return; }} > Confirm
); }; export const SubscribeWithBluesky = (props: { isPost?: boolean; pubName: string; pub_uri: string; base_url: string; subscribers: { identity: string }[]; }) => { let { identity } = useIdentityData(); let searchParams = useSearchParams(); let [successModalOpen, setSuccessModalOpen] = useState( !!searchParams.has("showSubscribeSuccess"), ); let subscribed = identity?.atp_did && props.subscribers.find((s) => s.identity === identity.atp_did); if (successModalOpen) return ( ); if (subscribed) { return ; } return (
{props.isPost && (
Get updates from {props.pubName}!
)}
); }; const ManageSubscription = (props: { isPost?: boolean; pubName: string; pub_uri: string; subscribers: { identity: string }[]; base_url: string; }) => { let toaster = useToaster(); let [hasFeed] = useState(false); let [, unsubscribe, unsubscribePending] = useActionState(async () => { await unsubscribeToPublication(props.pub_uri); toaster({ content: "You unsubscribed.", type: "success", }); }, null); return (
You're Subscribed{props.isPost ? ` to ` : "!"} {props.isPost && ( {props.pubName} )}
Manage
} >

Update Options

{!hasFeed && ( View Bluesky Custom Feed )} Get RSS
{" "} ); }; let BlueskySubscribeButton = (props: { pub_uri: string; setSuccessModalOpen: (open: boolean) => void; }) => { let { identity } = useIdentityData(); let toaster = useToaster(); let [, subscribe, subscribePending] = useActionState(async () => { let result = await subscribeToPublication( props.pub_uri, window.location.href + "?refreshAuth", ); if (result.hasFeed === false) { props.setSuccessModalOpen(true); } toaster({ content:
You're Subscribed!
, type: "success" }); }, null); let [isClient, setIsClient] = useState(false); useEffect(() => { setIsClient(true); }, []); if (!identity?.atp_did) { return ( Subscribe with Bluesky } > {isClient && ( )} ); } return ( <>
{subscribePending ? ( ) : ( <> Subscribe with Bluesky )}
); }; const SubscribeSuccessModal = ({ open, setOpen, }: { open: boolean; setOpen: (open: boolean) => void; }) => { let searchParams = useSearchParams(); let [loading, setLoading] = useState(false); let toaster = useToaster(); return (

Subscribed!

You'll get updates about this publication via a Feed just for you. { if (loading) return; setLoading(true); let feedurl = "https://bsky.app/profile/leaflet.pub/feed/subscribedPublications"; await addFeed(); toaster({ content: "Feed added!", type: "success" }); setLoading(false); window.open(feedurl, "_blank"); }} > {loading ? : "Add Bluesky Feed"}
); };