|
| 1 | +import { EnvelopeIcon } from "@heroicons/react/20/solid"; |
| 2 | +import type { LoaderFunctionArgs, MetaFunction } from "@remix-run/node"; |
| 3 | +import { Form } from "@remix-run/react"; |
| 4 | +import { GitHubLightIcon } from "@trigger.dev/companyicons"; |
| 5 | +import { typedjson, useTypedLoaderData } from "remix-typedjson"; |
| 6 | +import { GoogleLogo } from "~/assets/logos/GoogleLogo"; |
| 7 | +import { LoginPageLayout } from "~/components/LoginPageLayout"; |
| 8 | +import { Button, LinkButton } from "~/components/primitives/Buttons"; |
| 9 | +import { Callout } from "~/components/primitives/Callout"; |
| 10 | +import { Fieldset } from "~/components/primitives/Fieldset"; |
| 11 | +import { Header1, Header2, Header3 } from "~/components/primitives/Headers"; |
| 12 | +import { Paragraph } from "~/components/primitives/Paragraph"; |
| 13 | +import { TextLink } from "~/components/primitives/TextLink"; |
| 14 | +import { isGithubAuthSupported, isGoogleAuthSupported } from "~/services/auth.server"; |
| 15 | +import { validatePromoCode } from "~/services/platform.v3.server"; |
| 16 | +import { setPromoCodeCookie } from "~/services/promoCode.server"; |
| 17 | +import { getUserId } from "~/services/session.server"; |
| 18 | +import { requestUrl } from "~/utils/requestUrl.server"; |
| 19 | + |
| 20 | +export const meta: MetaFunction = () => [{ title: "Claim your Trigger.dev credits" }]; |
| 21 | + |
| 22 | +export async function loader({ request }: LoaderFunctionArgs) { |
| 23 | + const userId = await getUserId(request); |
| 24 | + const url = requestUrl(request); |
| 25 | + const code = url.searchParams.get("code")?.trim() || null; |
| 26 | + |
| 27 | + const authMethods = { |
| 28 | + showGithubAuth: isGithubAuthSupported, |
| 29 | + showGoogleAuth: isGoogleAuthSupported, |
| 30 | + }; |
| 31 | + |
| 32 | + // Credits are only granted to brand-new accounts, so an already-signed-in |
| 33 | + // user can't redeem a code. |
| 34 | + if (userId) { |
| 35 | + return typedjson({ view: "signed_in" as const, ...authMethods }); |
| 36 | + } |
| 37 | + |
| 38 | + if (!code) { |
| 39 | + return typedjson({ view: "invalid" as const, ...authMethods }); |
| 40 | + } |
| 41 | + |
| 42 | + const validated = await validatePromoCode(code); |
| 43 | + if (!validated || !validated.valid) { |
| 44 | + return typedjson({ view: "invalid" as const, ...authMethods }); |
| 45 | + } |
| 46 | + |
| 47 | + // Stash the code so it survives the OAuth round-trip and can be applied when |
| 48 | + // the new org is created. |
| 49 | + return typedjson( |
| 50 | + { |
| 51 | + view: "valid" as const, |
| 52 | + amountInCents: validated.amountInCents ?? 0, |
| 53 | + expiresAt: validated.expiresAt ?? null, |
| 54 | + ...authMethods, |
| 55 | + }, |
| 56 | + { headers: { "Set-Cookie": await setPromoCodeCookie(code) } } |
| 57 | + ); |
| 58 | +} |
| 59 | + |
| 60 | +function formatDollars(cents: number) { |
| 61 | + const dollars = cents / 100; |
| 62 | + return Number.isInteger(dollars) ? `$${dollars}` : `$${dollars.toFixed(2)}`; |
| 63 | +} |
| 64 | + |
| 65 | +function formatExpiry(iso: string | null) { |
| 66 | + if (!iso) return null; |
| 67 | + const date = new Date(iso); |
| 68 | + if (Number.isNaN(date.getTime())) return null; |
| 69 | + return date.toLocaleDateString("en-US", { month: "long", day: "numeric", year: "numeric" }); |
| 70 | +} |
| 71 | + |
| 72 | +function SignInForm({ |
| 73 | + showGithubAuth, |
| 74 | + showGoogleAuth, |
| 75 | +}: { |
| 76 | + showGithubAuth: boolean; |
| 77 | + showGoogleAuth: boolean; |
| 78 | +}) { |
| 79 | + return ( |
| 80 | + <Fieldset className="w-full"> |
| 81 | + <div className="flex flex-col items-center gap-y-3"> |
| 82 | + {showGithubAuth && ( |
| 83 | + <Form action="/auth/github" method="post" className="w-full"> |
| 84 | + <Button |
| 85 | + type="submit" |
| 86 | + variant="secondary/extra-large" |
| 87 | + fullWidth |
| 88 | + data-action="continue with github" |
| 89 | + > |
| 90 | + <GitHubLightIcon className="mr-2 size-5" /> |
| 91 | + <span className="text-text-bright">Continue with GitHub</span> |
| 92 | + </Button> |
| 93 | + </Form> |
| 94 | + )} |
| 95 | + {showGoogleAuth && ( |
| 96 | + <Form action="/auth/google" method="post" className="w-full"> |
| 97 | + <Button |
| 98 | + type="submit" |
| 99 | + variant="secondary/extra-large" |
| 100 | + fullWidth |
| 101 | + data-action="continue with google" |
| 102 | + > |
| 103 | + <GoogleLogo className="mr-2 size-5" /> |
| 104 | + <span className="text-text-bright">Continue with Google</span> |
| 105 | + </Button> |
| 106 | + </Form> |
| 107 | + )} |
| 108 | + <LinkButton |
| 109 | + to="/login/magic" |
| 110 | + variant="secondary/extra-large" |
| 111 | + fullWidth |
| 112 | + data-action="continue with email" |
| 113 | + className="text-text-bright" |
| 114 | + > |
| 115 | + <EnvelopeIcon className="mr-2 size-5 text-text-bright" /> |
| 116 | + Continue with Email |
| 117 | + </LinkButton> |
| 118 | + </div> |
| 119 | + <Paragraph variant="extra-small" className="mt-2 text-center"> |
| 120 | + By signing up you agree to our{" "} |
| 121 | + <TextLink href="https://trigger.dev/legal" target="_blank"> |
| 122 | + terms |
| 123 | + </TextLink>{" "} |
| 124 | + and{" "} |
| 125 | + <TextLink href="https://trigger.dev/legal/privacy" target="_blank"> |
| 126 | + privacy |
| 127 | + </TextLink>{" "} |
| 128 | + policy. |
| 129 | + </Paragraph> |
| 130 | + </Fieldset> |
| 131 | + ); |
| 132 | +} |
| 133 | + |
| 134 | +function PromoPanel({ |
| 135 | + amountInCents, |
| 136 | + expiresAt, |
| 137 | +}: { |
| 138 | + amountInCents: number; |
| 139 | + expiresAt: string | null; |
| 140 | +}) { |
| 141 | + const expiry = formatExpiry(expiresAt); |
| 142 | + return ( |
| 143 | + <div className="flex h-full flex-col items-center justify-center px-16"> |
| 144 | + <Paragraph variant="small" className="uppercase tracking-wide text-text-dimmed"> |
| 145 | + Promo applied |
| 146 | + </Paragraph> |
| 147 | + <Header3 className="mt-3 text-center text-5xl font-semibold text-text-bright"> |
| 148 | + {formatDollars(amountInCents)} in free credits |
| 149 | + </Header3> |
| 150 | + <Paragraph className="mt-4 text-center text-text-dimmed"> |
| 151 | + Added to your new Trigger.dev account when you sign up |
| 152 | + {expiry ? `, valid until ${expiry}` : ""}. |
| 153 | + </Paragraph> |
| 154 | + </div> |
| 155 | + ); |
| 156 | +} |
| 157 | + |
| 158 | +export default function PromoPage() { |
| 159 | + const data = useTypedLoaderData<typeof loader>(); |
| 160 | + |
| 161 | + const rightContent = |
| 162 | + data.view === "valid" ? ( |
| 163 | + <PromoPanel amountInCents={data.amountInCents} expiresAt={data.expiresAt} /> |
| 164 | + ) : undefined; |
| 165 | + |
| 166 | + return ( |
| 167 | + <LoginPageLayout rightContent={rightContent}> |
| 168 | + <div className="flex w-full flex-col"> |
| 169 | + {data.view === "signed_in" ? ( |
| 170 | + <> |
| 171 | + <Header2 className="sm:text-2xl md:text-3xl lg:text-4xl" spacing> |
| 172 | + Promo codes are for new accounts |
| 173 | + </Header2> |
| 174 | + <Paragraph variant="base" spacing> |
| 175 | + You're already signed in. Promo credits can only be added to a brand-new account. |
| 176 | + </Paragraph> |
| 177 | + <LinkButton to="/" variant="secondary/medium"> |
| 178 | + Go to dashboard |
| 179 | + </LinkButton> |
| 180 | + </> |
| 181 | + ) : ( |
| 182 | + <> |
| 183 | + <Header2 className="sm:text-2xl md:text-3xl lg:text-4xl" spacing> |
| 184 | + {data.view === "valid" ? "Claim your credits" : "Create your account"} |
| 185 | + </Header2> |
| 186 | + <Paragraph variant="base" spacing> |
| 187 | + Create an account or login |
| 188 | + </Paragraph> |
| 189 | + {data.view === "invalid" && ( |
| 190 | + <Callout variant="warning" className="mb-6 w-full"> |
| 191 | + That promo code isn't valid. You can still sign up below — credits just won't be |
| 192 | + added. |
| 193 | + </Callout> |
| 194 | + )} |
| 195 | + <SignInForm showGithubAuth={data.showGithubAuth} showGoogleAuth={data.showGoogleAuth} /> |
| 196 | + </> |
| 197 | + )} |
| 198 | + </div> |
| 199 | + </LoginPageLayout> |
| 200 | + ); |
| 201 | +} |
0 commit comments