|
| 1 | +import React, { useState, useEffect } from 'react' |
| 2 | + |
| 3 | +import { BottomBanner } from './bottom-banner' |
| 4 | +import { Button } from './button' |
| 5 | +import { useChatStore } from '../state/chat-store' |
| 6 | +import { |
| 7 | + openOAuthInBrowser, |
| 8 | + exchangeCodeForTokens, |
| 9 | + disconnectClaudeOAuth, |
| 10 | + getClaudeOAuthStatus, |
| 11 | +} from '../utils/claude-oauth' |
| 12 | +import { useTheme } from '../hooks/use-theme' |
| 13 | + |
| 14 | +type FlowState = 'checking' | 'not-connected' | 'waiting-for-code' | 'connected' | 'error' |
| 15 | + |
| 16 | +export const ClaudeConnectBanner = () => { |
| 17 | + const setInputMode = useChatStore((state) => state.setInputMode) |
| 18 | + const theme = useTheme() |
| 19 | + const [flowState, setFlowState] = useState<FlowState>('checking') |
| 20 | + const [error, setError] = useState<string | null>(null) |
| 21 | + const [isDisconnectHovered, setIsDisconnectHovered] = useState(false) |
| 22 | + |
| 23 | + // Check initial connection status |
| 24 | + useEffect(() => { |
| 25 | + const status = getClaudeOAuthStatus() |
| 26 | + if (status.connected) { |
| 27 | + setFlowState('connected') |
| 28 | + } else { |
| 29 | + setFlowState('not-connected') |
| 30 | + } |
| 31 | + }, []) |
| 32 | + |
| 33 | + const handleConnect = async () => { |
| 34 | + try { |
| 35 | + setFlowState('waiting-for-code') |
| 36 | + await openOAuthInBrowser() |
| 37 | + } catch (err) { |
| 38 | + setError(err instanceof Error ? err.message : 'Failed to open browser') |
| 39 | + setFlowState('error') |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + const handleDisconnect = () => { |
| 44 | + disconnectClaudeOAuth() |
| 45 | + setFlowState('not-connected') |
| 46 | + } |
| 47 | + |
| 48 | + const handleClose = () => { |
| 49 | + setInputMode('default') |
| 50 | + } |
| 51 | + |
| 52 | + // Connected state |
| 53 | + if (flowState === 'connected') { |
| 54 | + const status = getClaudeOAuthStatus() |
| 55 | + const connectedDate = status.connectedAt |
| 56 | + ? new Date(status.connectedAt).toLocaleDateString() |
| 57 | + : 'Unknown' |
| 58 | + |
| 59 | + return ( |
| 60 | + <BottomBanner borderColorKey="success" onClose={handleClose}> |
| 61 | + <box style={{ flexDirection: 'row', justifyContent: 'space-between', width: '100%' }}> |
| 62 | + <text style={{ fg: theme.success }}> |
| 63 | + ✓ Connected to Claude (since {connectedDate}) |
| 64 | + </text> |
| 65 | + <Button |
| 66 | + onClick={handleDisconnect} |
| 67 | + onMouseOver={() => setIsDisconnectHovered(true)} |
| 68 | + onMouseOut={() => setIsDisconnectHovered(false)} |
| 69 | + > |
| 70 | + <text style={{ fg: isDisconnectHovered ? theme.error : theme.muted }}> |
| 71 | + disconnect |
| 72 | + </text> |
| 73 | + </Button> |
| 74 | + </box> |
| 75 | + </BottomBanner> |
| 76 | + ) |
| 77 | + } |
| 78 | + |
| 79 | + // Error state |
| 80 | + if (flowState === 'error') { |
| 81 | + return ( |
| 82 | + <BottomBanner |
| 83 | + borderColorKey="error" |
| 84 | + text={`Error: ${error}. Press Escape to close.`} |
| 85 | + onClose={handleClose} |
| 86 | + /> |
| 87 | + ) |
| 88 | + } |
| 89 | + |
| 90 | + // Waiting for code state |
| 91 | + if (flowState === 'waiting-for-code') { |
| 92 | + return ( |
| 93 | + <BottomBanner borderColorKey="info" onClose={handleClose}> |
| 94 | + <box style={{ flexDirection: 'column', gap: 0 }}> |
| 95 | + <text style={{ fg: theme.info }}> |
| 96 | + Browser opened. Sign in with your Claude account, then paste the authorization code below. |
| 97 | + </text> |
| 98 | + <text style={{ fg: theme.muted, marginTop: 1 }}> |
| 99 | + Type the code in the input box above and press Enter. |
| 100 | + </text> |
| 101 | + </box> |
| 102 | + </BottomBanner> |
| 103 | + ) |
| 104 | + } |
| 105 | + |
| 106 | + // Not connected / checking state - show connect button |
| 107 | + return ( |
| 108 | + <BottomBanner borderColorKey="info" onClose={handleClose}> |
| 109 | + <box style={{ flexDirection: 'row', justifyContent: 'space-between', width: '100%' }}> |
| 110 | + <text style={{ fg: theme.info }}> |
| 111 | + Connect your Claude Pro/Max subscription to use Claude models directly. |
| 112 | + </text> |
| 113 | + <Button onClick={handleConnect}> |
| 114 | + <text style={{ fg: theme.link }}>Connect →</text> |
| 115 | + </Button> |
| 116 | + </box> |
| 117 | + </BottomBanner> |
| 118 | + ) |
| 119 | +} |
| 120 | + |
| 121 | +/** |
| 122 | + * Handle the authorization code input from the user. |
| 123 | + * This is called when the user pastes their code in connect:claude mode. |
| 124 | + */ |
| 125 | +export async function handleClaudeAuthCode(code: string): Promise<{ |
| 126 | + success: boolean |
| 127 | + message: string |
| 128 | +}> { |
| 129 | + try { |
| 130 | + await exchangeCodeForTokens(code) |
| 131 | + return { |
| 132 | + success: true, |
| 133 | + message: 'Successfully connected to Claude! Your Claude models will now use your subscription.', |
| 134 | + } |
| 135 | + } catch (err) { |
| 136 | + return { |
| 137 | + success: false, |
| 138 | + message: err instanceof Error ? err.message : 'Failed to exchange authorization code', |
| 139 | + } |
| 140 | + } |
| 141 | +} |
0 commit comments