import { useEffect, useState } from "react"; import { FormattedDate, FormattedMessage, FormattedNumber, useIntl } from "react-intl"; import { Link } from "react-router-dom"; import PageSpinner from "Element/PageSpinner"; import useEventPublisher from "Feed/EventPublisher"; import SnortApi, { Subscription, SubscriptionError } from "SnortApi"; import { mapPlanName, mapSubscriptionErrorCode } from "."; import Icon from "Icons/Icon"; import AsyncButton from "Element/AsyncButton"; import SendSats from "Element/SendSats"; export default function ManageSubscriptionPage() { const publisher = useEventPublisher(); const { formatMessage } = useIntl(); const api = new SnortApi(undefined, publisher); const [subs, setSubs] = useState>(); const [error, setError] = useState(); const [invoice, setInvoice] = useState(""); useEffect(() => { (async () => { try { const s = await api.listSubscriptions(); setSubs(s); } catch (e) { if (e instanceof SubscriptionError) { setError(e); } } })(); }, []); async function renew(id: string) { try { const rsp = await api.renewSubscription(id); setInvoice(rsp.pr); } catch (e) { if (e instanceof SubscriptionError) { setError(e); } } } if (subs === undefined) { return ; } return ( <>

{subs.map(a => { const created = new Date(a.created); const expires = new Date(a.expires); const now = new Date(); const daysToExpire = Math.floor((expires.getTime() - now.getTime()) / 8.64e7); const hoursToExpire = Math.floor((expires.getTime() - now.getTime()) / 3.6e6); const isExpired = a.state === "expired"; const isNew = a.state === "new"; return (
{mapPlanName(a.type)}

{daysToExpire >= 1 && (

)} {daysToExpire >= 0 && daysToExpire < 1 && (

)} {isExpired && (

)} {isNew && (

)}
{(isExpired || isNew) && (
renew(a.id)}> {isExpired ? ( ) : ( )}
)}
); })} {subs.length === 0 && (

), }} />

)} {error && {mapSubscriptionErrorCode(error)}} setInvoice("")} title={formatMessage({ defaultMessage: "Pay for subscription", })} /> ); }