MCPcopy Create free account
hub / github.com/Prejudice-Studio/Twilight / EmailCodeInput

Function EmailCodeInput

webui/src/components/email-code-input.tsx:26–86  ·  view source on GitHub ↗
({ purpose, email, code, onCodeChange, onSent, disabled }: EmailCodeInputProps)

Source from the content-addressed store, hash-verified

24
25// EmailCodeInput 封装「获取验证码 + 输入验证码」的复用 UI(含重发冷却)。
26// 绑定邮箱与强制改密两条链路共用:父组件保存 verification_id + code,提交时使用。
27export function EmailCodeInput({ purpose, email, code, onCodeChange, onSent, disabled }: EmailCodeInputProps) {
28 const { t } = useI18n();
29 const { toast } = useToast();
30 const [sending, setSending] = useState(false);
31 const [cooldown, setCooldown] = useCountdownState(0);
32
33 const send = async () => {
34 if (purpose === "bind" && !email?.trim()) {
35 toast({ title: t("email.enterEmailFirst"), variant: "destructive" });
36 return;
37 }
38 setSending(true);
39 try {
40 const res = await api.sendEmailCode({ purpose, email: email?.trim() });
41 if (res.success && res.data) {
42 onSent(res.data.verification_id);
43 setCooldown(res.data.resend_after || 60);
44 toast({ title: t("email.codeSentTo", { email: res.data.email }), variant: "success" });
45 } else if (isThrottleErrorCode(res.error_code)) {
46 // 被限流或发送失败:本地起冷却让按钮自禁,避免继续无效点击。
47 // EMAIL_SEND_FAILED 通常是 SMTP 服务商限流(如每小时发件上限),
48 // 给予较长冷却(120s)减少无效重试。
49 const cd = throttleCooldownSeconds(res.error_code);
50 setCooldown((c) => (c > 0 ? c : cd));
51 toast({ title: friendlyError(res.error_code, res.message), variant: "destructive" });
52 } else {
53 toast({ title: friendlyError(res.error_code, res.message), variant: "destructive" });
54 }
55 } catch (error: any) {
56 toast({ title: error?.message || t("common.networkError"), variant: "destructive" });
57 } finally {
58 setSending(false);
59 }
60 };
61
62 return (
63 <div className="space-y-2">
64 <Label>{t("email.codeLabel")}</Label>
65 <div className="flex items-center gap-2">
66 <Input
67 value={code}
68 onChange={(e) => onCodeChange(e.target.value)}
69 placeholder={t("email.codePlaceholder")}
70 inputMode="numeric"
71 autoComplete="one-time-code"
72 disabled={disabled}
73 />
74 <Button type="button" variant="outline" className="shrink-0" onClick={send} disabled={disabled || sending || cooldown > 0}>
75 {sending && <Loader2 className="mr-2 h-4 w-4 animate-spin" />}
76 {cooldown > 0 ? t("email.resendIn", { seconds: cooldown }) : t("email.sendCode")}
77 </Button>
78 </div>
79 </div>
80 );
81}

Callers

nothing calls this directly

Calls 2

useI18nFunction · 0.90
useToastFunction · 0.90

Tested by

no test coverage detected