forked from aws/agentcore-cli
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCredentialSourcePrompt.tsx
More file actions
161 lines (148 loc) · 5.2 KB
/
Copy pathCredentialSourcePrompt.tsx
File metadata and controls
161 lines (148 loc) · 5.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import { Panel } from './Panel';
import { ScreenLayout } from './ScreenLayout';
import { SecretInput } from './SecretInput';
import { SelectList, type SelectableItem } from './SelectList';
import { Box, Text, useInput } from 'ink';
import { useEffect, useRef, useState } from 'react';
export type CredentialSource = 'env-local' | 'manual' | 'skip';
interface IdentityCredential {
providerName: string;
envVarName: string;
}
interface CredentialSourcePromptProps {
/** List of identity providers that need API keys */
missingCredentials: IdentityCredential[];
/** Called when user selects to use .env.local credentials */
onUseEnvLocal: () => void;
/** Called when user enters credentials manually */
onManualEntry: (credentials: Record<string, string>) => void;
/** Called when user chooses to skip */
onSkip: () => void;
}
const SOURCE_OPTIONS: SelectableItem[] = [
{
id: 'env-local',
title: 'Use credentials from .env.local',
},
{
id: 'manual',
title: 'Enter credentials manually',
description: 'Not saved to disk',
},
{
id: 'skip',
title: 'Skip for now',
},
];
/**
* Credential source selection prompt for deploy flow.
* Allows user to choose how to provide API keys for identity providers.
*/
export function CredentialSourcePrompt({
missingCredentials,
onUseEnvLocal,
onManualEntry,
onSkip,
}: CredentialSourcePromptProps) {
const [selectedIndex, setSelectedIndex] = useState(0);
const [phase, setPhase] = useState<'select' | 'manual-entry'>('select');
const [manualCredentials, setManualCredentials] = useState<Record<string, string>>({});
const [currentCredentialIndex, setCurrentCredentialIndex] = useState(0);
const submittedRef = useRef(false);
// Submit manual credentials when all collected (avoids setState during render)
useEffect(() => {
if (phase === 'manual-entry' && currentCredentialIndex >= missingCredentials.length && !submittedRef.current) {
submittedRef.current = true;
onManualEntry(manualCredentials);
}
}, [phase, currentCredentialIndex, missingCredentials.length, manualCredentials, onManualEntry]);
useInput((input, key) => {
if (phase !== 'select') return;
if (key.upArrow) {
setSelectedIndex(prev => (prev > 0 ? prev - 1 : SOURCE_OPTIONS.length - 1));
} else if (key.downArrow) {
setSelectedIndex(prev => (prev < SOURCE_OPTIONS.length - 1 ? prev + 1 : 0));
} else if (key.return) {
const selectedOption = SOURCE_OPTIONS[selectedIndex];
if (selectedOption?.id === 'env-local') {
onUseEnvLocal();
} else if (selectedOption?.id === 'manual') {
setPhase('manual-entry');
} else if (selectedOption?.id === 'skip') {
onSkip();
}
}
});
// Manual entry phase - collect each credential one by one
if (phase === 'manual-entry') {
const currentCredential = missingCredentials[currentCredentialIndex];
if (!currentCredential || currentCredentialIndex >= missingCredentials.length) {
// All credentials collected - use effect to submit to avoid setState during render
return null;
}
const handleSubmit = (value: string) => {
setManualCredentials(prev => ({
...prev,
[currentCredential.envVarName]: value,
}));
setCurrentCredentialIndex(prev => prev + 1);
};
const handleCancel = () => {
// Go back to selection
setPhase('select');
setManualCredentials({});
setCurrentCredentialIndex(0);
submittedRef.current = false;
};
return (
<ScreenLayout>
<Panel>
<Box flexDirection="column" gap={1}>
<Text bold>
Enter API Key ({currentCredentialIndex + 1}/{missingCredentials.length})
</Text>
<Text>
Provider: <Text color="cyan">{currentCredential.providerName}</Text>
</Text>
<SecretInput
key={currentCredential.envVarName}
prompt="API Key"
onSubmit={handleSubmit}
onCancel={handleCancel}
customValidation={value => value.trim().length > 0 || 'API key is required'}
revealChars={4}
/>
</Box>
</Panel>
</ScreenLayout>
);
}
// Selection phase
return (
<ScreenLayout>
<Panel>
<Box flexDirection="column" gap={1}>
<Text bold>Identity Provider Setup</Text>
<Text dimColor>
{new Set(missingCredentials.map(c => c.providerName)).size} identity provider
{new Set(missingCredentials.map(c => c.providerName)).size > 1 ? 's' : ''} configured:
</Text>
<Box flexDirection="column" marginLeft={2}>
{[...new Set(missingCredentials.map(c => c.providerName))].map(name => (
<Text key={name} dimColor>
• {name}
</Text>
))}
</Box>
<Box marginTop={1}>
<Text dimColor>How would you like to provide the credentials?</Text>
</Box>
<Box marginTop={1}>
<SelectList items={SOURCE_OPTIONS} selectedIndex={selectedIndex} />
</Box>
<Text dimColor>↑↓ navigate · Enter select</Text>
</Box>
</Panel>
</ScreenLayout>
);
}