import { useState } from 'react'; import type { AllowedProfileVisibility } from '../../api/user'; import { httpGet, httpPost } from '../../lib/http'; import { useToast } from '../../hooks/use-toast'; import { CheckIcon, Loader2, X, XCircle } from 'lucide-react'; type ProfileUsernameProps = { username: string; setUsername: (username: string) => void; profileVisibility: AllowedProfileVisibility; currentUsername?: string; }; export function ProfileUsername(props: ProfileUsernameProps) { const { username, setUsername, profileVisibility, currentUsername } = props; const toast = useToast(); const [isLoading, setIsLoading] = useState(false); const [isUnique, setIsUnique] = useState(null); const checkIsUnique = async (username: string) => { if (isLoading || username.length < 3) { return; } if (currentUsername && username === currentUsername && isUnique !== false) { setIsUnique(true); return; } setIsLoading(true); const { response, error } = await httpPost<{ isUnique: boolean; }>(`${import.meta.env.PUBLIC_API_URL}/v1-check-is-unique-username`, { username, }); if (error || !response) { setIsUnique(null); setIsLoading(false); toast.error(error?.message || 'Something went wrong. Please try again.'); return; } setIsUnique(response.isUnique); setIsLoading(false); }; return (
roadmap.sh/u/
setUsername((e.target as HTMLInputElement).value)} onBlur={(e) => checkIsUnique((e.target as HTMLInputElement).value)} required={profileVisibility === 'public'} /> {isLoading ? ( ) : isUnique === false ? ( ) : isUnique === true ? ( ) : null}
); }