Files
logbuch/src/components/admin/MagicLinkForm.tsx
T
Matthias Giesselmann b90ff252d1 Add Logbuch: project update blog with admin, media and API
Public archive with sidebar navigation, project pages, month archive,
search and entry pages with image blocks. Admin area for brands,
projects, post types, users, api clients, media and the entry editor
with drag and drop images, preview per audience, scheduling and
publish checks. Read and write API with bearer tokens, audience
scoping, idempotent creation, OpenAPI document and editorial guide.
Magic link login with configurable allowed domains, whole app behind
the session gate. 456 tests including design rule checks.
2026-07-31 21:33:42 +02:00

81 lines
2.2 KiB
TypeScript

'use client'
import { useActionState } from 'react'
import Link from 'next/link'
import { useTranslations } from 'next-intl'
import { TbMailForward } from 'react-icons/tb'
import {
emptyMagicLinkState,
magicLinkMinutes,
type MagicLinkErrorKey,
type MagicLinkState,
} from '~/domain/magic-link'
import { requestMagicLink } from '~/lib/auth-actions'
import { loginPath } from '~/lib/auth-routes'
import { AdminNotice } from './AdminNotice'
import { inputClass, labelClass, primaryButtonClass, quietLinkClass } from './styles'
export type MagicLinkFormProps = {
next?: string
}
export function MagicLinkForm({ next }: MagicLinkFormProps) {
const t = useTranslations('login.magic')
const [state, formAction, pending] = useActionState<MagicLinkState, FormData>(
requestMagicLink,
emptyMagicLinkState,
)
const messages: Record<MagicLinkErrorKey, string> = {
email: t('errors.email'),
tooMany: t('errors.tooMany', { minutes: magicLinkMinutes }),
}
if (state.status === 'sent') {
return (
<AdminNotice
tone="done"
title={t('sentTitle')}
action={
<Link href={loginPath(next)} className={quietLinkClass}>
{t('again')}
</Link>
}
>
{`${t('sent', { email: state.email ?? '' })} ${t('sentHint', { minutes: magicLinkMinutes })}`}
</AdminNotice>
)
}
return (
<form action={formAction} className="flex flex-col gap-6">
{next ? <input type="hidden" name="next" value={next} /> : null}
{state.error ? <AdminNotice tone="alert">{messages[state.error]}</AdminNotice> : null}
<div className="flex flex-col gap-2">
<label htmlFor="magic-email" className={labelClass}>
{t('email')}
</label>
<input
id="magic-email"
name="email"
type="email"
autoComplete="username"
required
defaultValue={state.email ?? ''}
className={inputClass}
/>
<p className="m-0 text-small text-pretty text-ink-3">{t('intro')}</p>
</div>
<div className="flex flex-wrap items-center gap-4">
<button type="submit" disabled={pending} className={primaryButtonClass}>
<TbMailForward aria-hidden="true" className="size-4 shrink-0" />
{pending ? t('pending') : t('submit')}
</button>
</div>
</form>
)
}