forked from aws/agentcore-cli
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathuseAddPolicyWizard.ts
More file actions
181 lines (161 loc) · 5.09 KB
/
Copy pathuseAddPolicyWizard.ts
File metadata and controls
181 lines (161 loc) · 5.09 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import type { AddPolicyConfig, AddPolicyStep, PolicySourceMethod } from './types';
import { useCallback, useState } from 'react';
// Steps vary based on source method, but the wizard tracks the current step directly
const COMMON_PREFIX: AddPolicyStep[] = ['engine', 'name', 'source-method'];
const COMMON_SUFFIX: AddPolicyStep[] = ['validation-mode', 'confirm'];
const SOURCE_STEPS: Record<PolicySourceMethod, AddPolicyStep[]> = {
file: ['source-file'],
inline: ['source-inline'],
generate: [
'source-generate-gateway',
'source-generate-description',
'source-generate-loading',
'source-generate-review',
],
};
function getSteps(sourceMethod: PolicySourceMethod | null, skipEngine: boolean): AddPolicyStep[] {
const prefix = skipEngine ? COMMON_PREFIX.filter(s => s !== 'engine') : COMMON_PREFIX;
const sourceSteps = sourceMethod ? SOURCE_STEPS[sourceMethod] : [];
return [...prefix, ...sourceSteps, ...COMMON_SUFFIX];
}
function getDefaultConfig(preSelectedEngine?: string): AddPolicyConfig {
return {
name: '',
engine: preSelectedEngine ?? '',
sourceMethod: 'file',
statement: '',
sourceFile: '',
gatewayArn: '',
naturalLanguageDescription: '',
validationMode: 'FAIL_ON_ANY_FINDINGS',
};
}
export function useAddPolicyWizard(preSelectedEngine?: string) {
const skipEngine = !!preSelectedEngine;
const [config, setConfig] = useState<AddPolicyConfig>(() => getDefaultConfig(preSelectedEngine));
const initialStep: AddPolicyStep = skipEngine ? 'name' : 'engine';
const [step, setStep] = useState<AddPolicyStep>(initialStep);
const [sourceMethod, setSourceMethodState] = useState<PolicySourceMethod | null>(null);
const steps = getSteps(sourceMethod, skipEngine);
const currentIndex = steps.indexOf(step);
const goBack = useCallback(() => {
const allSteps = getSteps(sourceMethod, skipEngine);
const idx = allSteps.indexOf(step);
if (idx > 0) {
const prevStep = allSteps[idx - 1]!;
// If going back from a source sub-step to source-method, clear the source method
if (prevStep === 'source-method') {
setSourceMethodState(null);
}
setStep(prevStep);
}
}, [sourceMethod, step, skipEngine]);
const advance = useCallback(
(fromStep: AddPolicyStep) => {
const allSteps = getSteps(sourceMethod, skipEngine);
const idx = allSteps.indexOf(fromStep);
const next = allSteps[idx + 1];
if (next) setStep(next);
},
[sourceMethod, skipEngine]
);
const setEngine = useCallback(
(engine: string) => {
setConfig(c => ({ ...c, engine }));
advance('engine');
},
[advance]
);
const setName = useCallback(
(name: string) => {
setConfig(c => ({ ...c, name }));
advance('name');
},
[advance]
);
const setSourceMethod = useCallback(
(method: PolicySourceMethod) => {
setSourceMethodState(method);
setConfig(c => ({ ...c, sourceMethod: method }));
// Compute next step with the new source method
const allSteps = getSteps(method, skipEngine);
const idx = allSteps.indexOf('source-method');
const next = allSteps[idx + 1];
if (next) setStep(next);
},
[skipEngine]
);
const setSourceFile = useCallback(
(sourceFile: string) => {
setConfig(c => ({ ...c, sourceFile, statement: '' }));
advance('source-file');
},
[advance]
);
const setInlineStatement = useCallback(
(statement: string) => {
setConfig(c => ({ ...c, statement, sourceFile: '' }));
advance('source-inline');
},
[advance]
);
const setGateway = useCallback(
(gatewayArn: string) => {
setConfig(c => ({ ...c, gatewayArn }));
advance('source-generate-gateway');
},
[advance]
);
const setNaturalLanguageDescription = useCallback(
(naturalLanguageDescription: string) => {
setConfig(c => ({ ...c, naturalLanguageDescription }));
advance('source-generate-description');
},
[advance]
);
const setGeneratedStatement = useCallback(
(statement: string) => {
setConfig(c => ({ ...c, statement, sourceFile: '' }));
advance('source-generate-review');
},
[advance]
);
// Called when generation completes to move past the loading step
const onGenerationComplete = useCallback(
(statement: string) => {
setConfig(c => ({ ...c, statement, sourceFile: '' }));
advance('source-generate-loading');
},
[advance]
);
const setValidationMode = useCallback(
(validationMode: AddPolicyConfig['validationMode']) => {
setConfig(c => ({ ...c, validationMode }));
advance('validation-mode');
},
[advance]
);
const reset = useCallback(() => {
setConfig(getDefaultConfig(preSelectedEngine));
setStep(initialStep);
setSourceMethodState(null);
}, [preSelectedEngine, initialStep]);
return {
config,
step,
steps,
currentIndex,
goBack,
setEngine,
setName,
setSourceMethod,
setSourceFile,
setInlineStatement,
setGateway,
setNaturalLanguageDescription,
setGeneratedStatement,
onGenerationComplete,
setValidationMode,
reset,
};
}