fix: address code review findings for TelegramLoginButton

- Replace useCallback with ref pattern to prevent stale closures and
  unnecessary Telegram.Login.init() re-calls
- Make isOIDC a proper boolean with Boolean() wrapper
- Clear previous error on successful login attempt
- Remove hardcoded fallback string from t() call
This commit is contained in:
Fringg
2026-03-07 02:51:18 +03:00
parent c221c6e8bf
commit 5c11f1251a

View File

@@ -1,4 +1,4 @@
import { useEffect, useRef, useCallback, useState } from 'react'; import { useEffect, useRef, useState } from 'react';
import { useTranslation } from 'react-i18next'; import { useTranslation } from 'react-i18next';
import { useQuery } from '@tanstack/react-query'; import { useQuery } from '@tanstack/react-query';
import { brandingApi, type TelegramWidgetConfig } from '../api/branding'; import { brandingApi, type TelegramWidgetConfig } from '../api/branding';
@@ -25,11 +25,13 @@ export default function TelegramLoginButton({ referralCode }: TelegramLoginButto
const botUsername = const botUsername =
widgetConfig?.bot_username || import.meta.env.VITE_TELEGRAM_BOT_USERNAME || ''; widgetConfig?.bot_username || import.meta.env.VITE_TELEGRAM_BOT_USERNAME || '';
const isOIDC = widgetConfig?.oidc_enabled && widgetConfig?.oidc_client_id; const isOIDC = Boolean(widgetConfig?.oidc_enabled && widgetConfig?.oidc_client_id);
// OIDC callback handler // OIDC callback handler (ref pattern to avoid stale closures and unnecessary re-inits)
const handleOIDCCallback = useCallback( const handleOIDCCallbackRef =
async (data: { id_token?: string; error?: string }) => { useRef<(data: { id_token?: string; error?: string }) => void>(undefined);
handleOIDCCallbackRef.current = async (data: { id_token?: string; error?: string }) => {
if (data.error || !data.id_token) { if (data.error || !data.id_token) {
setOidcError(data.error || t('auth.loginFailed')); setOidcError(data.error || t('auth.loginFailed'));
setOidcLoading(false); setOidcLoading(false);
@@ -37,6 +39,7 @@ export default function TelegramLoginButton({ referralCode }: TelegramLoginButto
} }
try { try {
setOidcLoading(true); setOidcLoading(true);
setOidcError('');
await loginWithTelegramOIDC(data.id_token); await loginWithTelegramOIDC(data.id_token);
navigate('/'); navigate('/');
} catch (err: unknown) { } catch (err: unknown) {
@@ -45,9 +48,7 @@ export default function TelegramLoginButton({ referralCode }: TelegramLoginButto
} finally { } finally {
setOidcLoading(false); setOidcLoading(false);
} }
}, };
[loginWithTelegramOIDC, navigate, t],
);
// Load OIDC script and init // Load OIDC script and init
useEffect(() => { useEffect(() => {
@@ -64,7 +65,7 @@ export default function TelegramLoginButton({ referralCode }: TelegramLoginButto
request_access: widgetConfig.request_access ? ['write'] : undefined, request_access: widgetConfig.request_access ? ['write'] : undefined,
lang: document.documentElement.lang || 'en', lang: document.documentElement.lang || 'en',
}, },
handleOIDCCallback, (data) => handleOIDCCallbackRef.current?.(data),
); );
} }
}; };
@@ -80,7 +81,7 @@ export default function TelegramLoginButton({ referralCode }: TelegramLoginButto
// Script already loaded, just re-init // Script already loaded, just re-init
initTelegramLogin(); initTelegramLogin();
} }
}, [isOIDC, widgetConfig?.oidc_client_id, widgetConfig?.request_access, handleOIDCCallback]); }, [isOIDC, widgetConfig?.oidc_client_id, widgetConfig?.request_access]);
// Legacy widget effect (only when NOT OIDC) // Legacy widget effect (only when NOT OIDC)
useEffect(() => { useEffect(() => {
@@ -141,9 +142,7 @@ export default function TelegramLoginButton({ referralCode }: TelegramLoginButto
<svg className="h-5 w-5" viewBox="0 0 24 24" fill="currentColor"> <svg className="h-5 w-5" viewBox="0 0 24 24" fill="currentColor">
<path d="M11.944 0A12 12 0 0 0 0 12a12 12 0 0 0 12 12 12 12 0 0 0 12-12A12 12 0 0 0 12 0a12 12 0 0 0-.056 0zm4.962 7.224c.1-.002.321.023.465.14a.506.506 0 0 1 .171.325c.016.093.036.306.02.472-.18 1.898-.962 6.502-1.36 8.627-.168.9-.499 1.201-.82 1.23-.696.065-1.225-.46-1.9-.902-1.056-.693-1.653-1.124-2.678-1.8-1.185-.78-.417-1.21.258-1.91.177-.184 3.247-2.977 3.307-3.23.007-.032.014-.15-.056-.212s-.174-.041-.249-.024c-.106.024-1.793 1.14-5.061 3.345-.48.33-.913.49-1.302.48-.428-.008-1.252-.241-1.865-.44-.752-.245-1.349-.374-1.297-.789.027-.216.325-.437.893-.663 3.498-1.524 5.83-2.529 6.998-3.014 3.332-1.386 4.025-1.627 4.476-1.635z" /> <path d="M11.944 0A12 12 0 0 0 0 12a12 12 0 0 0 12 12 12 12 0 0 0 12-12A12 12 0 0 0 12 0a12 12 0 0 0-.056 0zm4.962 7.224c.1-.002.321.023.465.14a.506.506 0 0 1 .171.325c.016.093.036.306.02.472-.18 1.898-.962 6.502-1.36 8.627-.168.9-.499 1.201-.82 1.23-.696.065-1.225-.46-1.9-.902-1.056-.693-1.653-1.124-2.678-1.8-1.185-.78-.417-1.21.258-1.91.177-.184 3.247-2.977 3.307-3.23.007-.032.014-.15-.056-.212s-.174-.041-.249-.024c-.106.024-1.793 1.14-5.061 3.345-.48.33-.913.49-1.302.48-.428-.008-1.252-.241-1.865-.44-.752-.245-1.349-.374-1.297-.789.027-.216.325-.437.893-.663 3.498-1.524 5.83-2.529 6.998-3.014 3.332-1.386 4.025-1.627 4.476-1.635z" />
</svg> </svg>
{oidcLoading {oidcLoading ? t('common.loading') : t('auth.loginWithTelegram')}
? t('common.loading')
: t('auth.loginWithTelegram', 'Sign in with Telegram')}
</button> </button>
{oidcError && <p className="text-xs text-red-500">{oidcError}</p>} {oidcError && <p className="text-xs text-red-500">{oidcError}</p>}
</div> </div>