55 */
66
77import type React from 'react' ;
8+ import { useRef , useEffect } from 'react' ;
89import { Box , Text } from 'ink' ;
910import { theme } from '../semantic-colors.js' ;
1011import { TextInput } from '../components/shared/TextInput.js' ;
1112import { useTextBuffer } from '../components/shared/text-buffer.js' ;
1213import { useUIState } from '../contexts/UIStateContext.js' ;
14+ import { clearApiKey , debugLogger } from '@google/gemini-cli-core' ;
15+ import { useKeypress } from '../hooks/useKeypress.js' ;
16+ import { keyMatchers , Command } from '../keyMatchers.js' ;
1317
1418interface ApiAuthDialogProps {
1519 onSubmit : ( apiKey : string ) => void ;
@@ -27,9 +31,20 @@ export function ApiAuthDialog({
2731 const { mainAreaWidth } = useUIState ( ) ;
2832 const viewportWidth = mainAreaWidth - 8 ;
2933
34+ const pendingPromise = useRef < { cancel : ( ) => void } | null > ( null ) ;
35+
36+ useEffect (
37+ ( ) => ( ) => {
38+ pendingPromise . current ?. cancel ( ) ;
39+ } ,
40+ [ ] ,
41+ ) ;
42+
43+ const initialApiKey = defaultValue ;
44+
3045 const buffer = useTextBuffer ( {
31- initialText : defaultValue || '' ,
32- initialCursorOffset : defaultValue ?. length || 0 ,
46+ initialText : initialApiKey || '' ,
47+ initialCursorOffset : initialApiKey ?. length || 0 ,
3348 viewport : {
3449 width : viewportWidth ,
3550 height : 4 ,
@@ -44,6 +59,41 @@ export function ApiAuthDialog({
4459 onSubmit ( value ) ;
4560 } ;
4661
62+ const handleClear = ( ) => {
63+ pendingPromise . current ?. cancel ( ) ;
64+
65+ let isCancelled = false ;
66+ const wrappedPromise = new Promise < void > ( ( resolve , reject ) => {
67+ clearApiKey ( ) . then (
68+ ( ) => ! isCancelled && resolve ( ) ,
69+ ( error ) => ! isCancelled && reject ( error ) ,
70+ ) ;
71+ } ) ;
72+
73+ pendingPromise . current = {
74+ cancel : ( ) => {
75+ isCancelled = true ;
76+ } ,
77+ } ;
78+
79+ return wrappedPromise
80+ . then ( ( ) => {
81+ buffer . setText ( '' ) ;
82+ } )
83+ . catch ( ( err ) => {
84+ debugLogger . debug ( 'Failed to clear API key:' , err ) ;
85+ } ) ;
86+ } ;
87+
88+ useKeypress (
89+ async ( key ) => {
90+ if ( keyMatchers [ Command . CLEAR_INPUT ] ( key ) ) {
91+ await handleClear ( ) ;
92+ }
93+ } ,
94+ { isActive : true } ,
95+ ) ;
96+
4797 return (
4898 < Box
4999 borderStyle = "round"
@@ -89,7 +139,7 @@ export function ApiAuthDialog({
89139 ) }
90140 < Box marginTop = { 1 } >
91141 < Text color = { theme . text . secondary } >
92- (Press Enter to submit, Esc to cancel)
142+ (Press Enter to submit, Esc to cancel, Ctrl+C to clear stored key )
93143 </ Text >
94144 </ Box >
95145 </ Box >
0 commit comments