2025-12-03 21:56:50 +00:00
|
|
|
import { useState, useEffect } from 'react';
|
2026-01-15 19:40:46 +00:00
|
|
|
import { authApi } from '@/services/api/auth';
|
2025-12-03 21:56:50 +00:00
|
|
|
|
|
|
|
|
export function useUsernameAvailability(username: string) {
|
|
|
|
|
const [available, setAvailable] = useState<boolean | null>(null);
|
|
|
|
|
const [checking, setChecking] = useState(false);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (!username || username.length < 3) {
|
|
|
|
|
setAvailable(null);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const timer = setTimeout(async () => {
|
|
|
|
|
setChecking(true);
|
|
|
|
|
try {
|
2026-01-15 19:40:46 +00:00
|
|
|
const response = await authApi.checkUsername({ username });
|
|
|
|
|
setAvailable(response.available);
|
2025-12-03 21:56:50 +00:00
|
|
|
} catch (error) {
|
|
|
|
|
setAvailable(null);
|
|
|
|
|
} finally {
|
|
|
|
|
setChecking(false);
|
|
|
|
|
}
|
|
|
|
|
}, 500);
|
|
|
|
|
|
|
|
|
|
return () => clearTimeout(timer);
|
|
|
|
|
}, [username]);
|
|
|
|
|
|
|
|
|
|
return { available, checking };
|
|
|
|
|
}
|