All files / src/features/auth/components GoogleSignInButton.tsx

91.66% Statements 11/12
93.1% Branches 27/29
80% Functions 4/5
100% Lines 10/10

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39                3x     49x 49x 49x 49x 49x 49x 49x   2x               1x                      
import { LoaderCircle } from 'lucide-react'
import { useMutation } from '@tanstack/react-query'
import { useTranslation } from 'react-i18next'
import { ApiError } from '@/lib/http-client'
import { GoogleIcon } from '@/shared/icons/google-icon'
import { startGoogleSignIn } from '../api/google-auth.api'
import { useGoogleStatus } from '../api/google-auth.hooks'
 
const redirect = (url: string) => window.location.assign(url)
 
export function GoogleSignInButton({ onRedirect = redirect }: { onRedirect?: (url: string) => void }) {
  const { t } = useTranslation()
  const status = useGoogleStatus()
  const start = useMutation({ mutationFn: startGoogleSignIn, onSuccess: (url) => onRedirect(url) })
  const checking = status.isFetching && !status.data
  const pending = start.isPending || start.isSuccess
  const label = t(checking ? 'auth.social.googleChecking' : pending ? 'auth.social.googleRedirecting' : 'auth.social.google')
  return (
    <div className="space-y-2">
      <button type="button" aria-label={label} aria-busy={checking || pending} disabled={!status.data?.enabled || pending} onClick={() => start.mutate()}
        className="flex h-12 w-full items-center justify-center gap-2.5 rounded-[10px] border border-border bg-card text-sm font-medium text-ink transition-colors hover:bg-muted focus-visible:ring-3 focus-visible:ring-emerald-600/40 focus-visible:outline-none disabled:cursor-not-allowed disabled:opacity-60">
        {checking ? <LoaderCircle aria-hidden="true" className="size-[18px] animate-spin motion-reduce:animate-none" /> : <GoogleIcon className="h-[18px] w-[18px]" />}
        <span role={checking ? 'status' : undefined}>{label}</span>
      </button>
      {status.isError ? (
        <p className="text-center text-xs text-ink-muted" role="status">
          {t('auth.social.googleUnavailable')}{' '}
          <button type="button" onClick={() => void status.refetch()} className="rounded underline focus-visible:outline-2">{t('auth.social.googleRetry')}</button>
        </p>
      ) : status.data && !status.data.enabled ? (
        <p className="text-center text-xs text-ink-muted">{t('auth.social.googleUnavailable')}</p>
      ) : null}
      {start.isError && <p role="alert" className="text-center text-sm text-destructive">
        {t(start.error instanceof ApiError && start.error.status === 429 ? 'auth.assisted.rateLimited' : 'auth.social.googleError')}
      </p>}
    </div>
  )
}