import { useState } from 'react' import { useNavigate } from 'react-router-dom' import { useAuthStore } from '../../store/authStore' import { useTranslation } from '../../i18n' /** * Register data hook — owns the form state, the client-side validation and the * register → redirect flow. RegisterPage is a pure wiring container. Behaviour * is identical to the previous in-component logic. */ export function useRegister() { const { t } = useTranslation() const { register } = useAuthStore() const navigate = useNavigate() const [username, setUsername] = useState('') const [email, setEmail] = useState('') const [password, setPassword] = useState('') const [confirmPassword, setConfirmPassword] = useState('') const [showPassword, setShowPassword] = useState(false) const [isLoading, setIsLoading] = useState(false) const [error, setError] = useState('') const handleSubmit = async (e: React.FormEvent): Promise => { e.preventDefault() setError('') if (password !== confirmPassword) { setError(t('register.passwordMismatch')) return } if (password.length < 8) { setError(t('register.passwordTooShort')) return } setIsLoading(true) try { await register(username, email, password) navigate('/dashboard') } catch (err: unknown) { setError(err instanceof Error ? err.message : t('register.failed')) } finally { setIsLoading(false) } } return { username, setUsername, email, setEmail, password, setPassword, confirmPassword, setConfirmPassword, showPassword, setShowPassword, isLoading, error, handleSubmit, } }