Welcome.tsx147 lines, 4.6 KB
| 1 | import { useMutation } from '@tanstack/react-query' |
| 2 | import { useState, type FormEvent } from 'react' |
| 3 | import { useNavigate } from 'react-router' |
| 4 | import { request, setSession, type Me } from '../../lib/api' |
| 5 | |
| 6 | type Account = Me & { token: string } |
| 7 | |
| 8 | // field hands a form's one field to run. This is onSubmit, not a form |
| 9 | // action: an action resets the form, and a taken handle should stay. |
| 10 | const field = |
| 11 | (name: string, run: (value: string) => void) => |
| 12 | (e: FormEvent<HTMLFormElement>) => { |
| 13 | e.preventDefault() |
| 14 | run(String(new FormData(e.currentTarget).get(name)).trim()) |
| 15 | } |
| 16 | |
| 17 | export function Welcome() { |
| 18 | const navigate = useNavigate() |
| 19 | const [copied, setCopied] = useState(false) |
| 20 | const signUp = useMutation({ |
| 21 | mutationFn: (handle: string) => |
| 22 | request<Account>('/users', { method: 'POST', json: { handle } }), |
| 23 | onSuccess: ({ token, handle }) => setSession({ token, handle }), |
| 24 | }) |
| 25 | const signIn = useMutation({ |
| 26 | mutationFn: (token: string) => |
| 27 | request<Me>('/me', { |
| 28 | headers: { Authorization: `Bearer ${token}` }, |
| 29 | }), |
| 30 | onSuccess: ({ handle }, token) => { |
| 31 | setSession({ token, handle }) |
| 32 | navigate('/') |
| 33 | }, |
| 34 | }) |
| 35 | const account = signUp.data |
| 36 | |
| 37 | return ( |
| 38 | <main |
| 39 | className="mx-auto grid min-h-dvh max-w-4xl content-center |
| 40 | items-center gap-12 px-6 py-16 lg:grid-cols-2 lg:gap-20" |
| 41 | > |
| 42 | <header> |
| 43 | <p className="text-5xl font-semibold tracking-tight lg:text-6xl"> |
| 44 | murmur<span className="text-accent-ink">.</span> |
| 45 | </p> |
| 46 | <p className="mt-4 max-w-xs text-lg text-mute"> |
| 47 | Short posts from a few people you follow, and a quiet word |
| 48 | when someone likes yours. |
| 49 | </p> |
| 50 | </header> |
| 51 | |
| 52 | {account ? ( |
| 53 | <section className="space-y-4"> |
| 54 | <h1 className="text-lg font-semibold"> |
| 55 | Welcome, @{account.handle} |
| 56 | </h1> |
| 57 | <p className="text-sm text-mute"> |
| 58 | This is your key. There is no password: keep the key |
| 59 | somewhere safe, because it is the only way back in, and it |
| 60 | is shown only once. |
| 61 | </p> |
| 62 | <code |
| 63 | className="block rounded-lg border border-line p-3 text-sm |
| 64 | break-all" |
| 65 | > |
| 66 | {account.token} |
| 67 | </code> |
| 68 | <div className="flex gap-3"> |
| 69 | <button |
| 70 | className="btn-quiet" |
| 71 | onClick={async () => { |
| 72 | await navigator.clipboard.writeText(account.token) |
| 73 | setCopied(true) |
| 74 | }} |
| 75 | > |
| 76 | {copied ? 'Copied' : 'Copy key'} |
| 77 | </button> |
| 78 | <button className="btn" onClick={() => navigate('/')}> |
| 79 | Continue |
| 80 | </button> |
| 81 | </div> |
| 82 | </section> |
| 83 | ) : ( |
| 84 | <div className="space-y-10"> |
| 85 | <form |
| 86 | onSubmit={field('handle', signUp.mutate)} |
| 87 | className="space-y-3" |
| 88 | > |
| 89 | <h1 className="text-lg font-semibold">Join Murmur</h1> |
| 90 | <label className="block text-sm"> |
| 91 | <span className="text-mute">Pick a handle</span> |
| 92 | <input |
| 93 | name="handle" |
| 94 | required |
| 95 | autoCapitalize="none" |
| 96 | spellCheck={false} |
| 97 | placeholder="your_name" |
| 98 | className="field mt-1" |
| 99 | /> |
| 100 | </label> |
| 101 | <Problem error={signUp.error} /> |
| 102 | <button className="btn" disabled={signUp.isPending}> |
| 103 | Create account |
| 104 | </button> |
| 105 | </form> |
| 106 | |
| 107 | <form |
| 108 | onSubmit={field('token', signIn.mutate)} |
| 109 | className="space-y-3 border-t border-line pt-8 text-sm" |
| 110 | > |
| 111 | <h2 className="font-medium">Have a key?</h2> |
| 112 | <div className="flex gap-2"> |
| 113 | <label className="flex-1"> |
| 114 | <span className="sr-only">Your key</span> |
| 115 | <input |
| 116 | name="token" |
| 117 | required |
| 118 | type="password" |
| 119 | autoComplete="off" |
| 120 | placeholder="Paste it here" |
| 121 | className="field" |
| 122 | /> |
| 123 | </label> |
| 124 | <button className="btn-quiet" disabled={signIn.isPending}> |
| 125 | Sign in |
| 126 | </button> |
| 127 | </div> |
| 128 | <Problem error={signIn.error} /> |
| 129 | </form> |
| 130 | </div> |
| 131 | )} |
| 132 | </main> |
| 133 | ) |
| 134 | } |
| 135 | |
| 136 | // Problem says what the API said, the way it said it. |
| 137 | function Problem({ error }: { error: Error | null }) { |
| 138 | if (!error) return null |
| 139 | return ( |
| 140 | <p |
| 141 | role="alert" |
| 142 | className="text-sm text-accent-ink first-letter:uppercase" |
| 143 | > |
| 144 | {error.message} |
| 145 | </p> |
| 146 | ) |
| 147 | } |