import { useState, useEffect, useRef } from 'react' import { Button } from '@/components/ui/button' import { Input } from '@/components/ui/input' import { Label } from '@/components/ui/label' import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle } from '@/components/ui/dialog' import { Loader2, ExternalLink, CheckCircle } from 'lucide-react' import { CopyButton } from '@/components/ui/copy-button' import { showToast } from '@/lib/toast' import { oauthApi, type OAuthAuthorizeResponse } from '@/api/oauth' import { mapOAuthError } from '@/lib/oauthErrors' interface OAuthCallbackDialogProps { providerId: string providerName: string authResponse: OAuthAuthorizeResponse open: boolean onOpenChange: (open: boolean) => void onSuccess: () => void } const POLL_INTERVAL_MS = 2000 export function OAuthCallbackDialog({ providerId, providerName, authResponse, open, onOpenChange, onSuccess, }: OAuthCallbackDialogProps) { const [isLoading, setIsLoading] = useState(false) const [authCode, setAuthCode] = useState('') const [error, setError] = useState(null) const isAutoMethod = authResponse.mode === 'auto' const completedRef = useRef(false) const onSuccessRef = useRef(onSuccess) onSuccessRef.current = onSuccess useEffect(() => { completedRef.current = false setIsLoading(false) setAuthCode('') setError(null) }, [authResponse.attemptID]) useEffect(() => { if (!open || !isAutoMethod) return let cancelled = false setIsLoading(true) setError(null) const poll = async () => { while (!cancelled) { try { const status = await oauthApi.getStatus(providerId, authResponse.attemptID) if (cancelled) return if (status.status === 'complete') { completedRef.current = true setIsLoading(false) onSuccessRef.current() return } if (status.status === 'failed' || status.status === 'expired') { setIsLoading(false) setError(status.message ?? 'Authentication failed') return } } catch (err) { if (!cancelled) { setIsLoading(false) setError(mapOAuthError(err, 'callback')) } return } await new Promise((resolve) => setTimeout(resolve, POLL_INTERVAL_MS)) } } void poll() return () => { cancelled = true } }, [open, isAutoMethod, providerId, authResponse.attemptID]) const handleCallback = async () => { setIsLoading(true) setError(null) try { await oauthApi.callback(providerId, authResponse.attemptID, authCode.trim() || undefined) completedRef.current = true onSuccessRef.current() } catch (err) { setError(mapOAuthError(err, 'callback')) } finally { setIsLoading(false) } } const handleOpenAuthUrl = () => { window.open(authResponse.url, '_blank') } const handleClose = () => { setError(null) setAuthCode('') if (!completedRef.current) { void oauthApi.cancel(providerId, authResponse.attemptID).catch(() => undefined) } onOpenChange(false) } // Extract device/user code from instructions (e.g., "Enter code: 596A-E304") const codeMatch = authResponse.instructions.match(/(?:Enter code|User code|Device code)[:\s]+([A-Z0-9-]+)/i) const deviceCode = codeMatch ? codeMatch[1] : '' return ( Complete {providerName} Authentication {isAutoMethod ? 'Follow the instructions below to complete authentication.' : 'Enter the authorization code from the provider.' } {error && (

{error}

)}

{authResponse.instructions}

{deviceCode && (
{deviceCode} showToast.success('Code copied to clipboard')} />
)}
showToast.success('URL copied to clipboard')} />
{!isAutoMethod && (
showToast.success('Code copied to clipboard')} />
setAuthCode(e.target.value)} placeholder="Enter the authorization code..." className="bg-background border-border" disabled={isLoading} />
)} {isAutoMethod && (
Waiting for authorization to complete...
)}
) }