// desktop/src/components/settings/ChatGPTOfficialLogin.tsx import { useEffect } from 'react' import { open as shellOpen } from '@tauri-apps/plugin-shell' import { LogIn, LogOut } from 'lucide-react' import { useHahaOpenAIOAuthStore } from '../../stores/hahaOpenAIOAuthStore' import { useTranslation } from '../../i18n' export function ChatGPTOfficialLogin() { const t = useTranslation() const { status, isLoading, error, fetchStatus, login, logout, startPolling, stopPolling, } = useHahaOpenAIOAuthStore() useEffect(() => { void fetchStatus() return () => stopPolling() }, [fetchStatus, stopPolling]) const handleLogin = async () => { try { const { authorizeUrl } = await login() try { await shellOpen(authorizeUrl) startPolling() } catch (err) { console.error('[ChatGPTOfficialLogin] shellOpen failed:', err) useHahaOpenAIOAuthStore.setState({ error: t('settings.chatgptOfficialLogin.openBrowserFailed'), }) } } catch { // store.login() errors are already captured into store.error } } if (status === null) { if (error) { return (
{t('settings.chatgptOfficialLogin.errorPrefix')}{error}
) } return (
{t('common.loading')}
) } if (status.loggedIn) { const accountLabel = status.email || status.accountId || t('settings.chatgptOfficialLogin.accountUnknown') return (
{t('settings.chatgptOfficialLogin.loggedInPrefix')} {accountLabel}
) } return (
{t('settings.chatgptOfficialLogin.intro')}
{error && (
{t('settings.chatgptOfficialLogin.errorPrefix')}{error}
)}
) }