|
| 1 | +import type { ImportResult, ImportResourceResult } from '../../../commands/import/types'; |
| 2 | +import { ErrorPrompt } from '../../components/PromptScreen'; |
| 3 | +import { NextSteps, type NextStep } from '../../components/NextSteps'; |
| 4 | +import { Screen } from '../../components/Screen'; |
| 5 | +import { Panel } from '../../components/Panel'; |
| 6 | +import { HELP_TEXT } from '../../constants'; |
| 7 | +import { ArnInputScreen } from './ArnInputScreen'; |
| 8 | +import { CodePathScreen } from './CodePathScreen'; |
| 9 | +import { ImportProgressScreen } from './ImportProgressScreen'; |
| 10 | +import { ImportSelectScreen, type ImportType } from './ImportSelectScreen'; |
| 11 | +import { YamlPathScreen } from './YamlPathScreen'; |
| 12 | +import { Box, Text } from 'ink'; |
| 13 | +import React, { useState } from 'react'; |
| 14 | + |
| 15 | +type ImportFlowState = |
| 16 | + | { name: 'select-type' } |
| 17 | + | { name: 'arn-input'; resourceType: 'runtime' | 'memory' } |
| 18 | + | { name: 'code-path'; resourceType: 'runtime'; arn: string } |
| 19 | + | { name: 'yaml-path' } |
| 20 | + | { |
| 21 | + name: 'importing'; |
| 22 | + importType: ImportType; |
| 23 | + arn?: string; |
| 24 | + code?: string; |
| 25 | + yamlPath?: string; |
| 26 | + } |
| 27 | + | { |
| 28 | + name: 'success'; |
| 29 | + importType: ImportType; |
| 30 | + result: ImportResourceResult | ImportResult; |
| 31 | + } |
| 32 | + | { name: 'error'; message: string }; |
| 33 | + |
| 34 | +const IMPORT_NEXT_STEPS: NextStep[] = [ |
| 35 | + { command: 'deploy', label: 'Deploy the imported stack' }, |
| 36 | + { command: 'status', label: 'Verify resource status' }, |
| 37 | +]; |
| 38 | + |
| 39 | +interface ImportFlowProps { |
| 40 | + onBack: () => void; |
| 41 | +} |
| 42 | + |
| 43 | +export function ImportFlow({ onBack }: ImportFlowProps) { |
| 44 | + const [flow, setFlow] = useState<ImportFlowState>({ name: 'select-type' }); |
| 45 | + |
| 46 | + if (flow.name === 'select-type') { |
| 47 | + return ( |
| 48 | + <ImportSelectScreen |
| 49 | + onSelect={(type) => { |
| 50 | + if (type === 'runtime' || type === 'memory') { |
| 51 | + setFlow({ name: 'arn-input', resourceType: type }); |
| 52 | + } else { |
| 53 | + setFlow({ name: 'yaml-path' }); |
| 54 | + } |
| 55 | + }} |
| 56 | + onExit={onBack} |
| 57 | + /> |
| 58 | + ); |
| 59 | + } |
| 60 | + |
| 61 | + if (flow.name === 'arn-input') { |
| 62 | + return ( |
| 63 | + <ArnInputScreen |
| 64 | + resourceType={flow.resourceType} |
| 65 | + onSubmit={(arn) => { |
| 66 | + if (flow.resourceType === 'runtime') { |
| 67 | + setFlow({ name: 'code-path', resourceType: 'runtime', arn }); |
| 68 | + } else { |
| 69 | + setFlow({ |
| 70 | + name: 'importing', |
| 71 | + importType: 'memory', |
| 72 | + arn, |
| 73 | + }); |
| 74 | + } |
| 75 | + }} |
| 76 | + onExit={() => setFlow({ name: 'select-type' })} |
| 77 | + /> |
| 78 | + ); |
| 79 | + } |
| 80 | + |
| 81 | + if (flow.name === 'code-path') { |
| 82 | + return ( |
| 83 | + <CodePathScreen |
| 84 | + onSubmit={(codePath) => { |
| 85 | + setFlow({ |
| 86 | + name: 'importing', |
| 87 | + importType: 'runtime', |
| 88 | + arn: flow.arn, |
| 89 | + code: codePath, |
| 90 | + }); |
| 91 | + }} |
| 92 | + onExit={() => setFlow({ name: 'arn-input', resourceType: 'runtime' })} |
| 93 | + /> |
| 94 | + ); |
| 95 | + } |
| 96 | + |
| 97 | + if (flow.name === 'yaml-path') { |
| 98 | + return ( |
| 99 | + <YamlPathScreen |
| 100 | + onSubmit={(yamlPath) => { |
| 101 | + setFlow({ |
| 102 | + name: 'importing', |
| 103 | + importType: 'starter-toolkit', |
| 104 | + yamlPath, |
| 105 | + }); |
| 106 | + }} |
| 107 | + onExit={() => setFlow({ name: 'select-type' })} |
| 108 | + /> |
| 109 | + ); |
| 110 | + } |
| 111 | + |
| 112 | + if (flow.name === 'importing') { |
| 113 | + return ( |
| 114 | + <ImportProgressScreen |
| 115 | + importType={flow.importType} |
| 116 | + arn={flow.arn} |
| 117 | + code={flow.code} |
| 118 | + yamlPath={flow.yamlPath} |
| 119 | + onSuccess={(result) => { |
| 120 | + setFlow({ name: 'success', importType: flow.importType, result }); |
| 121 | + }} |
| 122 | + onError={(message) => { |
| 123 | + setFlow({ name: 'error', message }); |
| 124 | + }} |
| 125 | + onExit={onBack} |
| 126 | + /> |
| 127 | + ); |
| 128 | + } |
| 129 | + |
| 130 | + if (flow.name === 'success') { |
| 131 | + const result = flow.result; |
| 132 | + const isResource = 'resourceType' in result; |
| 133 | + |
| 134 | + return ( |
| 135 | + <Screen title="Import Complete" onExit={onBack} helpText={HELP_TEXT.BACK}> |
| 136 | + <Panel> |
| 137 | + <Box flexDirection="column"> |
| 138 | + <Text color="green">Import successful!</Text> |
| 139 | + {isResource && ( |
| 140 | + <Box flexDirection="column" marginTop={1}> |
| 141 | + <Text> |
| 142 | + <Text dimColor>Type: </Text> |
| 143 | + <Text>{(result as ImportResourceResult).resourceType}</Text> |
| 144 | + </Text> |
| 145 | + <Text> |
| 146 | + <Text dimColor>Name: </Text> |
| 147 | + <Text>{(result as ImportResourceResult).resourceName}</Text> |
| 148 | + </Text> |
| 149 | + {(result as ImportResourceResult).resourceId && ( |
| 150 | + <Text> |
| 151 | + <Text dimColor>ID: </Text> |
| 152 | + <Text>{(result as ImportResourceResult).resourceId}</Text> |
| 153 | + </Text> |
| 154 | + )} |
| 155 | + </Box> |
| 156 | + )} |
| 157 | + {!isResource && ( |
| 158 | + <Box flexDirection="column" marginTop={1}> |
| 159 | + {(result as ImportResult).importedAgents?.map((agent) => ( |
| 160 | + <Text key={agent}> |
| 161 | + <Text dimColor>Agent: </Text> |
| 162 | + <Text>{agent}</Text> |
| 163 | + </Text> |
| 164 | + ))} |
| 165 | + {(result as ImportResult).importedMemories?.map((mem) => ( |
| 166 | + <Text key={mem}> |
| 167 | + <Text dimColor>Memory: </Text> |
| 168 | + <Text>{mem}</Text> |
| 169 | + </Text> |
| 170 | + ))} |
| 171 | + </Box> |
| 172 | + )} |
| 173 | + </Box> |
| 174 | + </Panel> |
| 175 | + <NextSteps steps={IMPORT_NEXT_STEPS} isInteractive={true} onSelect={() => onBack()} onBack={onBack} /> |
| 176 | + </Screen> |
| 177 | + ); |
| 178 | + } |
| 179 | + |
| 180 | + if (flow.name === 'error') { |
| 181 | + return ( |
| 182 | + <ErrorPrompt |
| 183 | + message="Import failed" |
| 184 | + detail={flow.message} |
| 185 | + onBack={() => setFlow({ name: 'select-type' })} |
| 186 | + onExit={onBack} |
| 187 | + /> |
| 188 | + ); |
| 189 | + } |
| 190 | + |
| 191 | + return null; |
| 192 | +} |
0 commit comments