|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useState } from "react"; |
| 4 | +import { ChevronRight } from "lucide-react"; |
| 5 | +import { toast } from "sonner"; |
| 6 | +import useSWR from "swr"; |
| 7 | +import { z } from "zod"; |
| 8 | + |
| 9 | +import { Button } from "@/components/ui/button"; |
| 10 | +import { Input } from "@/components/ui/input"; |
| 11 | +import { AnimatePresence, motion } from "framer-motion"; |
| 12 | + |
| 13 | +const formSchema = z.object({ |
| 14 | + email: z.string().email(), |
| 15 | +}); |
| 16 | + |
| 17 | +const fetcher = (url: string) => fetch(url).then((res) => res.json()); |
| 18 | + |
| 19 | +type FormSchema = z.infer<typeof formSchema>; |
| 20 | + |
| 21 | +function useWaitlistCount() { |
| 22 | + const { data, mutate } = useSWR<{ count: number }>( |
| 23 | + "/api/waitlist", |
| 24 | + fetcher, |
| 25 | + { |
| 26 | + revalidateOnFocus: false, |
| 27 | + }, |
| 28 | + ); |
| 29 | + |
| 30 | + return { count: data?.count ?? 0, mutate } as const; |
| 31 | +} |
| 32 | + |
| 33 | +export default function SDKPage() { |
| 34 | + const [email, setEmail] = useState(""); |
| 35 | + const [isSubmitting, setIsSubmitting] = useState(false); |
| 36 | + const [success, setSuccess] = useState(false); |
| 37 | + const waitlist = useWaitlistCount(); |
| 38 | + |
| 39 | + async function joinWaitlist(e: React.FormEvent<HTMLFormElement>) { |
| 40 | + e.preventDefault(); |
| 41 | + |
| 42 | + const validation = formSchema.safeParse({ email }); |
| 43 | + if (!validation.success) { |
| 44 | + toast.error("Please enter a valid email address"); |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + setIsSubmitting(true); |
| 49 | + |
| 50 | + try { |
| 51 | + const response = await fetch("/api/waitlist", { |
| 52 | + method: "POST", |
| 53 | + headers: { |
| 54 | + "Content-Type": "application/json", |
| 55 | + }, |
| 56 | + body: JSON.stringify({ email }), |
| 57 | + }); |
| 58 | + |
| 59 | + const data = await response.json(); |
| 60 | + if (!response.ok) throw new Error(data.error || "Something went wrong"); |
| 61 | + |
| 62 | + setSuccess(true); |
| 63 | + setEmail(""); |
| 64 | + waitlist.mutate({ count: waitlist.count + 1 }, false); |
| 65 | + } catch (error) { |
| 66 | + const msg = |
| 67 | + error instanceof Error ? error.message : "Something went wrong. Please try again."; |
| 68 | + toast.error(msg); |
| 69 | + } finally { |
| 70 | + setIsSubmitting(false); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + return ( |
| 75 | + <main className="flex min-h-screen flex-col items-center justify-center px-6 py-16 md:px-8 lg:px-12"> |
| 76 | + <div className="flex w-full max-w-3xl flex-col items-center gap-6 text-center"> |
| 77 | + <header className="space-y-2"> |
| 78 | + <p className="text-lg text-muted-foreground"> |
| 79 | + Join the waitlist to get early access to the SDK for <i>AI-Native writing interfaces</i>. |
| 80 | + </p> |
| 81 | + </header> |
| 82 | + |
| 83 | + <AnimatePresence mode="wait" initial={false}> |
| 84 | + {success ? ( |
| 85 | + <motion.div |
| 86 | + key="success" |
| 87 | + animate={{ opacity: 1, y: 0 }} |
| 88 | + exit={{ opacity: 0, y: -12 }} |
| 89 | + transition={{ duration: 0.25, ease: "easeOut" }} |
| 90 | + className="flex h-11 w-full items-center justify-center" |
| 91 | + > |
| 92 | + <p className="text-sm font-medium text-muted-foreground"> |
| 93 | + You're in! We'll keep you posted. |
| 94 | + </p> |
| 95 | + </motion.div> |
| 96 | + ) : ( |
| 97 | + <motion.form |
| 98 | + key="form" |
| 99 | + onSubmit={joinWaitlist} |
| 100 | + animate={{ opacity: 1, y: 0 }} |
| 101 | + exit={{ opacity: 0, y: -12 }} |
| 102 | + transition={{ duration: 0.25, ease: "easeOut" }} |
| 103 | + className="flex w-full max-w-lg flex-col gap-3 sm:flex-row" |
| 104 | + > |
| 105 | + <Input |
| 106 | + type="email" |
| 107 | + placeholder="example@email.com" |
| 108 | + value={email} |
| 109 | + onChange={(e) => setEmail(e.target.value)} |
| 110 | + className="h-11 w-full rounded-md px-4 text-base font-medium placeholder:font-medium placeholder:text-muted-foreground md:text-base" |
| 111 | + disabled={isSubmitting} |
| 112 | + required |
| 113 | + /> |
| 114 | + <Button |
| 115 | + variant="outline" |
| 116 | + type="submit" |
| 117 | + className="h-11 w-full pl-4 pr-3 text-base sm:w-fit" |
| 118 | + disabled={isSubmitting} |
| 119 | + > |
| 120 | + Join Waitlist <ChevronRight className="ml-1 h-5 w-5" /> |
| 121 | + </Button> |
| 122 | + </motion.form> |
| 123 | + )} |
| 124 | + </AnimatePresence> |
| 125 | + |
| 126 | + <div className="relative flex flex-row items-center justify-center gap-2"> |
| 127 | + <span className="size-2 rounded-full bg-green-600 dark:bg-green-400" /> |
| 128 | + <span className="absolute left-0 size-2 rounded-full bg-green-600 blur-xs dark:bg-green-400" /> |
| 129 | + <span className="text-sm text-green-600 dark:text-green-400 sm:text-base"> |
| 130 | + {waitlist.count.toLocaleString()} people already joined |
| 131 | + </span> |
| 132 | + </div> |
| 133 | + </div> |
| 134 | + </main> |
| 135 | + ); |
| 136 | +} |
0 commit comments