import { useState, useEffect } from 'react'; import { toast } from 'sonner'; import { Button } from '@/components/ui/button'; import { useClient } from 'hooks/useClient'; import { useTranslation } from '@/lib/i18n'; type GoogleStatus = { connected: boolean; email: string | null; picture: string | null; configured: boolean; }; export const GoogleAccount = () => { const { t } = useTranslation(); const client = useClient(); const [isLoading, setIsLoading] = useState(true); const [status, setStatus] = useState({ connected: false, email: null, picture: null, configured: false, }); useEffect(() => { client .get('/integrations/google/status') .then(setStatus) .catch(() => {}) .finally(() => setIsLoading(false)); const params = new URLSearchParams(window.location.search); const result = params.get('google'); if (result === 'success') { toast.success(t('settings.googleAccount.connectedToast')); } else if (result === 'error') { toast.error(t('settings.googleAccount.connectFailed')); } if (result) { window.history.replaceState({}, '', window.location.pathname); } }, []); const handleConnect = () => { const params = new URLSearchParams({ token: client.token ?? '', origin: window.location.origin, }); window.location.href = `/api/integrations/google/authorize?${params.toString()}`; }; const handleDisconnect = async () => { try { await client.delete('/integrations/google/connection'); setStatus({ ...status, connected: false, email: null }); toast.success(t('settings.googleAccount.disconnectedToast')); } catch { toast.error(t('settings.googleAccount.disconnectFailed')); } }; if (isLoading) return null; if (!status.configured) return null; return (

{t('settings.googleAccount.title')}

{t('settings.googleAccount.hint')}

{status.connected ? ( <>

{t('settings.googleAccount.connected')}

{status.email}

{status.picture && ( )}
) : ( )}
); };