import { useQuery } from '@tanstack/react-query';
import { useEffect, useState } from 'react';
import { Button } from '#/components/atoms/button.jsx';
import { Notice } from '#/components/atoms/notice.jsx';
import { Panel } from '#/components/atoms/panel.jsx';
import { ErrorNotice } from '#/components/molecules/error-notice.jsx';
import { FormShell } from '#/components/molecules/form-shell.jsx';
import { Section } from '#/components/molecules/section.jsx';
import { TextField } from '#/components/molecules/text-field.jsx';
import { TextLink } from '#/components/molecules/text-link.jsx';
import { get, post } from '#/lib/api.js';
import { queryKeys, useApiMutation, useInvalidate } from '#/lib/query.js';
export function EmailCard() {
const { data, error } = useQuery({
queryKey: queryKeys.email,
queryFn: () => get('/email'),
});
const action = useApiMutation([queryKeys.email]);
const invalidate = useInvalidate();
const [notice, setNotice] = useState('');
const [now, setNow] = useState(() => Date.now());
const pending = data?.pending || null;
const resendAt = pending?.resendAt || 0;
const secondsLeft = Math.max(0, Math.ceil((resendAt - now) / 1000));
// Tick once a second while the resend is still cooling down.
useEffect(() => {
if (!pending || Date.now() >= resendAt) return;
const id = setInterval(() => {
setNow(Date.now());
if (Date.now() >= resendAt) clearInterval(id);
}, 1000);
return () => clearInterval(id);
}, [pending, resendAt]);
if (data && !data.supported) return null;
const run = (body, message) =>
action.run(async () => {
await post('/email/action', body);
setNotice(message);
});
// The pending card is the feedback that a code went out, so sending one
// clears any notice rather than repeating the address a third time.
const resend = () => run({ action: 'resend' }, '');
const cancel = () => run({ action: 'cancel' }, '');
const sendToStored = () => run({ address: data.address }, '');
return (
An address to get back into your account. This server sends nothing
else to it.
{data.address} stays your address until you confirm this one.