import { useEffect, useRef } from 'react' import { useTranslation } from 'react-i18next' interface TelegramLoginButtonProps { botUsername: string } export default function TelegramLoginButton({ botUsername, }: TelegramLoginButtonProps) { const { t } = useTranslation() const containerRef = useRef(null) // Load widget script useEffect(() => { if (!containerRef.current || !botUsername) return // Clear previous widget using safe DOM API while (containerRef.current.firstChild) { containerRef.current.removeChild(containerRef.current.firstChild) } // Get current URL for redirect const redirectUrl = `${window.location.origin}/auth/telegram/callback` // Create script element for Telegram Login Widget const script = document.createElement('script') script.src = 'https://telegram.org/js/telegram-widget.js?22' script.setAttribute('data-telegram-login', botUsername) script.setAttribute('data-size', 'large') script.setAttribute('data-radius', '8') script.setAttribute('data-auth-url', redirectUrl) script.setAttribute('data-request-access', 'write') script.async = true containerRef.current.appendChild(script) }, [botUsername]) if (!botUsername || botUsername === 'your_bot') { return (
{t('auth.telegramNotConfigured')}
) } return (
{/* Telegram Widget will be inserted here */}
{/* Fallback link for mobile */}

{t('auth.orOpenInApp')}

@{botUsername}
) }