import { useState, useMemo, useEffect, useCallback } from 'react' import { Button } from '@/components/ui/button' import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle } from '@/components/ui/dialog' import { Badge } from '@/components/ui/badge' import { ExternalLink } from 'lucide-react' import { oauthApi, type FormAnswer, type FormValue, type IntegrationMethod, type IntegrationOAuthMethod, type OAuthAuthorizeResponse, } from '@/api/oauth' import { buildAnswer, hasFields, hasMissingAnswers, resolveAnswers, setAnswerValue, visibleFields } from '@/lib/formFields' import { mapOAuthError } from '@/lib/oauthErrors' import { ProviderAuthField } from './ProviderAuthField' interface OAuthAuthorizeDialogProps { providerId: string providerName: string methods: IntegrationMethod[] open: boolean onOpenChange: (open: boolean) => void onSuccess: (response: OAuthAuthorizeResponse, methodID: string) => void } function isBrowserLocalMethod(method: IntegrationOAuthMethod): boolean { return method.label.toLowerCase().includes('browser') } export function OAuthAuthorizeDialog({ providerId, providerName, methods, open, onOpenChange, onSuccess, }: OAuthAuthorizeDialogProps) { const [isLoading, setIsLoading] = useState(false) const [error, setError] = useState(null) const [selectedMethodID, setSelectedMethodID] = useState(null) const [answers, setAnswers] = useState>({}) const [autoStarted, setAutoStarted] = useState(false) const oauthMethods = useMemo(() => { return methods .filter((method): method is IntegrationOAuthMethod => method.type === 'oauth') .filter((method) => !(providerId === 'openai' && isBrowserLocalMethod(method))) }, [methods, providerId]) const getAnswer = useCallback( (method: IntegrationOAuthMethod) => resolveAnswers(method.form, answers[method.id]), [answers], ) const handleAnswerChange = useCallback((methodID: string, key: string, value: FormValue | undefined) => { setAnswers((prev) => setAnswerValue(prev, methodID, key, value)) }, []) const handleAuthorize = useCallback(async (method: IntegrationOAuthMethod) => { const answer = getAnswer(method) const fields = visibleFields(method.form ?? [], answer) if (hasMissingAnswers(method.form ?? [], answer)) { setError('Please complete all required authentication fields') setSelectedMethodID(method.id) return } setIsLoading(true) setError(null) setSelectedMethodID(method.id) try { const response = await oauthApi.authorize( providerId, method.id, fields.length > 0 ? buildAnswer(method.form ?? [], answer) : undefined, ) onSuccess(response, method.id) } catch (err) { setError(mapOAuthError(err, 'authorize')) } finally { setIsLoading(false) } }, [getAnswer, providerId, onSuccess]) useEffect(() => { if (open && oauthMethods.length === 1 && !autoStarted && !isLoading) { const method = oauthMethods[0] setAutoStarted(true) setSelectedMethodID(method.id) if (!hasFields(method.form)) { void handleAuthorize(method) } } }, [open, oauthMethods, autoStarted, isLoading, handleAuthorize]) const handleMethodSelection = (method: IntegrationOAuthMethod) => { setError(null) setSelectedMethodID(method.id) if (!hasFields(method.form)) { void handleAuthorize(method) } } const handleClose = () => { setError(null) setAnswers({}) setSelectedMethodID(null) onOpenChange(false) } return ( Connect to {providerName} Select an authentication method to connect your {providerName} account. {error && (

{error}

)}
{oauthMethods.map((method) => { const isBrowserLocal = isBrowserLocalMethod(method) const answer = getAnswer(method) const fields = visibleFields(method.form ?? [], answer) const canSubmit = !hasMissingAnswers(method.form ?? [], answer) return (
{isBrowserLocal && ( Localhost only )}
{selectedMethodID === method.id && hasFields(method.form) && (
{fields.map((field) => ( handleAnswerChange(method.id, field.key, value)} /> ))}
)} {isBrowserLocal && (

This method relies on a callback server started by OpenCode and may not work when OCM is remote.

)}
) })}

• Some methods may require completing authorization in your browser

) }