@@ -17,6 +17,15 @@ type NametagData = {
1717 profilePhoto : string
1818}
1919
20+ /** Same rules as the availability effect: empty, format, or not explicitly available. */
21+ function getHandleHasError ( handle : string , handleAvailable : boolean | null ) : boolean {
22+ const trimmed = handle . trim ( )
23+ const handleRegex = / ^ (?: [ a - z 0 - 9 _ ] | - ) + $ /
24+ const formatInvalid =
25+ ! trimmed || ! handleRegex . test ( handle ) || handle . length < 3 || handle . length > 30
26+ return formatInvalid || handleAvailable !== true
27+ }
28+
2029export default function Setup ( ) {
2130 const [ loading , setLoading ] = useState ( true )
2231 const [ saving , setSaving ] = useState ( false )
@@ -207,19 +216,27 @@ export default function Setup() {
207216
208217 const handleHandleChange = ( e : React . ChangeEvent < HTMLInputElement > ) => {
209218 setHandle ( e . target . value . toLowerCase ( ) )
210- // Clear error when user starts typing
211- if ( hasAttemptedSubmit && validationErrors . handle ) {
212- setValidationErrors ( ( prev ) => ( { ...prev , handle : false } ) )
213- }
214219 }
215220
221+ // After first submit, recompute handle error from current value + availability (not unconditional clear on keystroke)
222+ useEffect ( ( ) => {
223+ if ( ! hasAttemptedSubmit ) return
224+
225+ const handleError = getHandleHasError ( handle , handleAvailable )
226+
227+ setValidationErrors ( ( prev ) => {
228+ if ( prev . handle === handleError ) return prev
229+ return { ...prev , handle : handleError }
230+ } )
231+ } , [ handle , handleAvailable , hasAttemptedSubmit ] )
232+
216233 const handleFinishSetup = async ( e : React . FormEvent ) => {
217234 e . preventDefault ( )
218235 setHasAttemptedSubmit ( true )
219236
220237 // Validate form and set error states
221238 const errors = {
222- handle : ! handle . trim ( ) || ! handleAvailable ,
239+ handle : getHandleHasError ( handle , handleAvailable ) ,
223240 profilePhoto : ! nametagData . profilePhoto ,
224241 fullName : ! nametagData . fullName . trim ( )
225242 }
0 commit comments