'use client' import { type ReactNode, useState } from 'react' import { Chip, cn } from '@sim/emcn' import { createLogger } from '@sim/logger' import { getErrorMessage } from '@sim/utils/errors' import { GithubIcon, GoogleIcon, MicrosoftIcon } from '@/components/icons' import { client } from '@/lib/auth/auth-client' import { AUTH_BUTTON_CLASS } from '@/app/(auth)/components/constants' const logger = createLogger('SocialLoginButtons') interface SocialLoginButtonsProps { githubAvailable: boolean googleAvailable: boolean microsoftAvailable: boolean callbackURL?: string isProduction: boolean children?: ReactNode } export function SocialLoginButtons({ githubAvailable, googleAvailable, microsoftAvailable, callbackURL = '/workspace', isProduction, children, }: SocialLoginButtonsProps) { const [isGithubLoading, setIsGithubLoading] = useState(false) const [isGoogleLoading, setIsGoogleLoading] = useState(false) const [isMicrosoftLoading, setIsMicrosoftLoading] = useState(false) async function signInWithGithub() { if (!githubAvailable) return setIsGithubLoading(true) try { await client.signIn.social({ provider: 'github', callbackURL }) } catch (err) { logger.error('GitHub sign-in failed', { error: getErrorMessage(err) }) } finally { setIsGithubLoading(false) } } async function signInWithGoogle() { if (!googleAvailable) return setIsGoogleLoading(true) try { await client.signIn.social({ provider: 'google', callbackURL }) } catch (err) { logger.error('Google sign-in failed', { error: getErrorMessage(err) }) } finally { setIsGoogleLoading(false) } } async function signInWithMicrosoft() { if (!microsoftAvailable) return setIsMicrosoftLoading(true) try { await client.signIn.social({ provider: 'microsoft', callbackURL }) } catch (err) { logger.error('Microsoft sign-in failed', { error: getErrorMessage(err) }) } finally { setIsMicrosoftLoading(false) } } const githubButton = ( {isGithubLoading ? 'Connecting…' : 'GitHub'} ) const googleButton = ( {isGoogleLoading ? 'Connecting…' : 'Google'} ) const microsoftButton = ( {isMicrosoftLoading ? 'Connecting…' : 'Microsoft'} ) const hasAnyOAuthProvider = githubAvailable || googleAvailable || microsoftAvailable if (!hasAnyOAuthProvider && !children) { return null } return (
{googleAvailable && googleButton} {microsoftAvailable && microsoftButton} {githubAvailable && githubButton} {children}
) }