import { useState, useEffect } from 'react' import { Loader2 } from 'lucide-react' import { toast } from 'sonner' import { trpc } from '@/lib/trpc' import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogFooter, } from '@/components/ui/dialog' import { Button } from '@/components/ui/button' import { Input } from '@/components/ui/input' import { Label } from '@/components/ui/label' import { Textarea } from '@/components/ui/textarea' import { Badge } from '@/components/ui/badge' import { cn } from '@/lib/utils' interface UpdateCredentialsDialogProps { open: boolean onOpenChange: (open: boolean) => void account: { id: string; email: string; provider: string } } export function UpdateCredentialsDialog({ open, onOpenChange, account, }: UpdateCredentialsDialogProps) { const [tab, setTab] = useState<'token' | 'credentials'>('token') // Tab A — token const [token, setToken] = useState('') const [tokenError, setTokenError] = useState('') // Tab B — credentials JSON const [configJsonText, setConfigJsonText] = useState('') const [credentialsText, setCredentialsText] = useState('') const [configJsonError, setConfigJsonError] = useState('') const [credentialsError, setCredentialsError] = useState('') // Shared const [serverError, setServerError] = useState('') const utils = trpc.useUtils() const mutation = trpc.updateAccountAuth.useMutation({ onSuccess: () => { toast.success(`Credentials updated for ${account.email}.`) void utils.systemHealthCheck.invalidate() onOpenChange(false) }, onError: (err) => { setServerError(err.message) }, }) useEffect(() => { if (open) { setTab('token') setToken('') setTokenError('') setConfigJsonText('') setCredentialsText('') setConfigJsonError('') setCredentialsError('') setServerError('') } }, [open]) function handleSubmit() { if (tab === 'token') { if (!token.trim()) { setTokenError('Required') return } setTokenError('') mutation.mutate({ id: account.id, configJson: JSON.stringify({ hasCompletedOnboarding: true }), credentials: JSON.stringify({ claudeAiOauth: { accessToken: token } }), }) } else { let hasError = false if (configJsonText.trim()) { try { JSON.parse(configJsonText) setConfigJsonError('') } catch { setConfigJsonError('Invalid JSON') hasError = true } } else { setConfigJsonError('') } if (credentialsText.trim()) { try { JSON.parse(credentialsText) setCredentialsError('') } catch { setCredentialsError('Invalid JSON') hasError = true } } else { setCredentialsError('') } if (hasError) return mutation.mutate({ id: account.id, configJson: configJsonText.trim() || '{}', credentials: credentialsText.trim() || '{}', }) } } return ( Update Credentials
{/* Read-only account identity */}

{account.email}

{account.provider}
{/* Tab navigation */}
{/* Tab A — By token */} {tab === 'token' && (
setToken(e.target.value)} /> {tokenError && (

{tokenError}

)}
)} {/* Tab B — By credentials JSON */} {tab === 'credentials' && (