forked from Talenttrust/Talenttrust-Frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpage.tsx
More file actions
183 lines (163 loc) · 6.98 KB
/
Copy pathpage.tsx
File metadata and controls
183 lines (163 loc) · 6.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
'use client';
import { useState, useEffect, useRef } from 'react';
import { ToastDemo } from '@/components/toast/toast-demo';
import { FormField } from '@/components/FormField';
import { ErrorSummary } from '@/components/ErrorSummary';
import { useToast } from '@/components/toast/toast-provider';
import {
MAX_EMAIL_LENGTH,
MAX_PASSWORD_LENGTH,
validateLogin,
} from '@/lib/validateLogin';
import {
getRemainingCooldownMs,
recordAttempt,
resetThrottle,
} from '@/lib/loginThrottle';
export default function Home() {
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const [errors, setErrors] = useState<{ fieldId: string; message: string }[]>([]);
const [cooldownRemainingMs, setCooldownRemainingMs] = useState(0);
const cooldownIntervalRef = useRef<ReturnType<typeof setInterval> | null>(null);
const { showSuccess } = useToast();
const clearCooldownInterval = () => {
if (cooldownIntervalRef.current !== null) {
clearInterval(cooldownIntervalRef.current);
cooldownIntervalRef.current = null;
}
};
const startCooldownCountdown = () => {
clearCooldownInterval();
const tick = () => {
const remaining = getRemainingCooldownMs();
if (remaining <= 0) {
setCooldownRemainingMs(0);
clearCooldownInterval();
return;
}
setCooldownRemainingMs(remaining);
};
tick();
cooldownIntervalRef.current = setInterval(tick, 250);
};
useEffect(() => {
const remaining = getRemainingCooldownMs();
if (remaining > 0) {
startCooldownCountdown();
}
return clearCooldownInterval;
}, []);
const handleSubmit = (e: React.FormEvent) => {
e.preventDefault();
if (cooldownRemainingMs > 0) return;
recordAttempt();
const remaining = getRemainingCooldownMs();
if (remaining > 0) {
startCooldownCountdown();
}
const newErrors = validateLogin(email, password);
setErrors(newErrors);
if (newErrors.length === 0) {
resetThrottle();
setCooldownRemainingMs(0);
clearCooldownInterval();
showSuccess({
title: 'Form submitted successfully!',
});
}
};
const getError = (fieldId: string) => errors.find((e) => e.fieldId === fieldId)?.message;
const cooldownSecs = Math.ceil(cooldownRemainingMs / 1000);
const isCooldown = cooldownRemainingMs > 0;
return (
/**
* ACCESSIBILITY LANDMARK STRUCTURE (WCAG 2.1 AA / issue #383)
*
* NOTE: No <main> landmark here — the root layout (src/app/layout.tsx) already
* provides the single <main id="main-content" tabIndex={-1}> landmark. Per WCAG 2.1 AA,
* a page should have exactly one main landmark to avoid confusing screen reader users
* with duplicate navigation targets. Additionally, no <h1> is rendered here; the layout
* header provides the page title, so this component uses <h2> to maintain a correct
* heading hierarchy (h1 → h2).
*
* This structure ensures that:
* 1. Screen readers see a single, unambiguous main content region
* 2. Heading navigation produces a logical outline (h1 first, then h2 for sections)
* 3. The ErrorSummary component's focus management works reliably (focus can move to
* the alert region and screen readers announce it without landmark confusion)
*/
<div className="min-h-screen bg-[radial-gradient(circle_at_top,_rgba(16,185,129,0.18),_transparent_28%),linear-gradient(180deg,_#f8fafc_0%,_#eff6ff_100%)] px-6 py-20">
<div className="mx-auto flex min-h-[calc(100vh-10rem)] max-w-3xl flex-col items-center justify-center rounded-[2rem] border border-white/70 bg-white/80 p-10 text-center shadow-[0_24px_80px_rgba(15,23,42,0.10)] backdrop-blur">
{/* Section heading (h2, not h1 — see accessibility note above) */}
<h2 className="mb-4 text-3xl font-bold text-center text-slate-900 sm:text-5xl">
TalentTrust
</h2>
<p className="max-w-xl text-center text-base text-slate-600 sm:text-lg">
Decentralized Freelancer Escrow Protocol on Stellar
</p>
<p className="mt-4 max-w-lg text-center text-sm text-slate-500 sm:text-base">
Accessible toast feedback now supports transient success and error states, including screen reader announcements for critical wallet and payout events.
</p>
<form onSubmit={handleSubmit} className="mt-8 w-full max-w-md text-left" noValidate aria-label="Sign in">
<ErrorSummary errors={errors} />
<div className="space-y-4">
<FormField
label="Email"
id="email"
error={getError('email')}
required
>
<input
type="email"
value={email}
onChange={(e) => setEmail(e.target.value)}
// Security: cap pasted/typed input at MAX_EMAIL_LENGTH so the
// browser and the validator enforce the same ceiling. See
// `MAX_EMAIL_LENGTH` in src/lib/validateLogin.ts.
maxLength={MAX_EMAIL_LENGTH}
className="w-full px-4 py-2.5 rounded-xl border border-slate-200 bg-white text-slate-900 placeholder-slate-400 focus:outline-none focus:ring-2 focus:ring-emerald-500/20 focus:border-emerald-500 transition-all shadow-sm"
placeholder="you@example.com"
/>
</FormField>
<FormField
label="Password"
id="password"
error={getError('password')}
required
>
<input
type="password"
value={password}
onChange={(e) => setPassword(e.target.value)}
// Security: cap pasted/typed input at MAX_PASSWORD_LENGTH. Mirrors
// the validator ceiling and prevents denial-of-service from
// arbitrarily long pasted secrets.
maxLength={MAX_PASSWORD_LENGTH}
className="w-full px-4 py-2.5 rounded-xl border border-slate-200 bg-white text-slate-900 placeholder-slate-400 focus:outline-none focus:ring-2 focus:ring-emerald-500/20 focus:border-emerald-500 transition-all shadow-sm"
placeholder="••••••••"
/>
</FormField>
</div>
<button
type="submit"
disabled={isCooldown}
className="mt-6 w-full rounded-xl bg-slate-900 px-5 py-3 text-sm font-semibold text-white transition hover:bg-slate-700 focus:outline-none focus:ring-2 focus:ring-slate-400 shadow-md disabled:cursor-not-allowed disabled:opacity-50"
>
{isCooldown ? `Wait ${cooldownSecs}s` : 'Sign In'}
</button>
{isCooldown && (
<div
aria-live="polite"
aria-atomic="true"
className="sr-only"
>
Please wait {cooldownSecs} seconds before trying to sign in again.
</div>
)}
</form>
<ToastDemo />
</div>
</div>
);
}