import { signOut, useSession } from "next-auth/react"; import { useEffect, useState } from "react"; import { toast } from "react-hot-toast"; import { useRouter } from "next/router"; import CenteredForm from "@/components/CenteredForm"; import { Plan } from "@linkwarden/types"; import { Button } from "@/components/ui/button"; import getServerSideProps from "@/lib/client/getServerSideProps"; import { Trans, useTranslation } from "next-i18next"; import { useUser } from "@linkwarden/router/user"; import { Separator } from "@/components/ui/separator"; import { cn } from "@/lib/utils"; const TRIAL_PERIOD_DAYS = Number(process.env.NEXT_PUBLIC_TRIAL_PERIOD_DAYS) || 14; const REQUIRE_CC = process.env.NEXT_PUBLIC_REQUIRE_CC === "true"; export default function Subscribe() { const { t } = useTranslation(); const [submitLoader, setSubmitLoader] = useState(false); const session = useSession(); const [plan, setPlan] = useState(1); const router = useRouter(); const { data: user } = useUser(); const [daysLeft, setDaysLeft] = useState(0); useEffect(() => { if (user?.createdAt) { const trialEndTime = new Date(user.createdAt).getTime() + (1 + Number(TRIAL_PERIOD_DAYS)) * 86400000; // Add 1 to account for the current day setDaysLeft(Math.floor((trialEndTime - Date.now()) / 86400000)); } }, [user]); useEffect(() => { if ( session.status === "authenticated" && user?.id && (user?.subscription?.active || user?.parentSubscription?.active) ) router.push("/dashboard"); }, [session.status, user]); async function submit() { setSubmitLoader(true); const redirectionToast = toast.loading(t("redirecting_to_stripe")); const res = await fetch("/api/v1/payment?plan=" + plan); const data = await res.json(); router.push(data.response); toast.dismiss(redirectionToast); } return (

{t("subscribe_title")}

, ]} />

{t("discount_percent", { percent: 25, })}

${plan === Plan.monthly ? "4" : "3"} /mo

{plan === Plan.monthly ? t("billed_monthly") : t("billed_yearly")}

{REQUIRE_CC || daysLeft > 0 ? (
{t("total")}

{plan === Plan.monthly ? t("total_monthly_desc", { count: REQUIRE_CC ? 14 : daysLeft, monthlyPrice: "4", }) : t("total_annual_desc", { count: REQUIRE_CC ? 14 : daysLeft, annualPrice: "36", })}

{t("plus_tax")}

) : (

{t("plus_tax")}

)}
{REQUIRE_CC || daysLeft <= 0 ? (
signOut()} className="w-fit mx-auto cursor-pointer text-neutral font-semibold " > {t("sign_out")}
) : ( )}
); } export { getServerSideProps };