|
| 1 | +import { useEffect, useState, type FormEvent } from 'react'; |
| 2 | +import { toast } from 'sonner'; |
| 3 | +import { Check, Mail, RefreshCw } from 'lucide-react'; |
| 4 | +import { api } from '@client/lib/api'; |
| 5 | +import { Button } from '@client/components/ui/button'; |
| 6 | +import { Input } from '@client/components/ui/input'; |
| 7 | +import type { UpdatesEmailResponse } from '@shared/api'; |
| 8 | + |
| 9 | +export function UpdatesEmailPrompt() { |
| 10 | + const [status, setStatus] = useState<UpdatesEmailResponse | null>(null); |
| 11 | + const [email, setEmail] = useState(''); |
| 12 | + const [submitting, setSubmitting] = useState(false); |
| 13 | + |
| 14 | + useEffect(() => { |
| 15 | + let cancelled = false; |
| 16 | + api.getUpdatesEmailStatus() |
| 17 | + .then((response) => { |
| 18 | + if (!cancelled) setStatus(response); |
| 19 | + }) |
| 20 | + .catch(() => { |
| 21 | + if (!cancelled) setStatus(null); |
| 22 | + }); |
| 23 | + |
| 24 | + return () => { |
| 25 | + cancelled = true; |
| 26 | + }; |
| 27 | + }, []); |
| 28 | + |
| 29 | + if (status?.status !== 'pending') return null; |
| 30 | + |
| 31 | + const subscribe = async (event: FormEvent<HTMLFormElement>) => { |
| 32 | + event.preventDefault(); |
| 33 | + setSubmitting(true); |
| 34 | + |
| 35 | + try { |
| 36 | + const response = await api.subscribeUpdates(email); |
| 37 | + setStatus(response); |
| 38 | + toast.success('Updates email saved', { |
| 39 | + description: 'You will only get important Codra release and security notes.', |
| 40 | + }); |
| 41 | + } catch (error) { |
| 42 | + toast.error('Could not save updates email', { |
| 43 | + description: error instanceof Error ? error.message : 'Please try again.', |
| 44 | + }); |
| 45 | + } finally { |
| 46 | + setSubmitting(false); |
| 47 | + } |
| 48 | + }; |
| 49 | + |
| 50 | + return ( |
| 51 | + <section className="surface overflow-hidden"> |
| 52 | + <div className="flex flex-col gap-4 px-4 py-4 sm:px-5 lg:flex-row lg:items-center lg:justify-between"> |
| 53 | + <div className="flex min-w-0 items-start gap-3"> |
| 54 | + <span className="mt-0.5 flex h-9 w-9 shrink-0 items-center justify-center rounded-md bg-primary/10 text-primary"> |
| 55 | + <Mail size={16} strokeWidth={2.1} /> |
| 56 | + </span> |
| 57 | + <div className="min-w-0"> |
| 58 | + <h2 className="text-sm font-semibold text-foreground">Get important Codra updates</h2> |
| 59 | + <p className="mt-1 max-w-2xl text-sm text-muted-foreground"> |
| 60 | + Add an email for release notes, security fixes, and upgrade heads-up. You can opt out from any update email later. No spam. |
| 61 | + </p> |
| 62 | + </div> |
| 63 | + </div> |
| 64 | + |
| 65 | + <form onSubmit={subscribe} className="flex min-w-0 flex-col gap-2 sm:flex-row sm:items-center lg:w-[31rem]"> |
| 66 | + <Input |
| 67 | + type="email" |
| 68 | + required |
| 69 | + value={email} |
| 70 | + onChange={(event) => setEmail(event.target.value)} |
| 71 | + placeholder="you@example.com" |
| 72 | + className="h-9 min-w-0 flex-1" |
| 73 | + aria-label="Email for Codra release updates" |
| 74 | + /> |
| 75 | + <div className="flex shrink-0 gap-2"> |
| 76 | + <Button type="submit" size="sm" disabled={submitting} className="flex-1 gap-2 sm:flex-none"> |
| 77 | + {submitting ? <RefreshCw size={13} className="animate-spin" /> : <Check size={13} />} |
| 78 | + Save email |
| 79 | + </Button> |
| 80 | + </div> |
| 81 | + </form> |
| 82 | + </div> |
| 83 | + </section> |
| 84 | + ); |
| 85 | +} |
0 commit comments