|
1 | 1 | import React, { useState, useEffect } from 'react'; |
2 | 2 | import { Box, Text, useApp, useInput } from 'ink'; |
3 | 3 | import SelectList from './SelectList.js'; |
4 | | -import { validateConnection } from '../scans/connectionHandler.js'; |
5 | | -import { listRepos } from '../scans/listRepos.js'; |
6 | | -import { getScanHistory } from '../scans/getScanHistory.js'; |
7 | | -import { fetchScanResults } from '../scans/fetchScanResults.js'; |
8 | | -import { fetchAdvancedScanResults } from '../scans/fetchAdvancedScanResults.js'; |
9 | | -import { fetchDismissedAlerts } from '../scans/fetchDismissedAlerts.js'; |
| 4 | +import { validateConnectionOnMount } from '../scanCenter/validateConnectionOnMount.js'; |
| 5 | +import { handleSelectConnection as _handleSelectConnection } from '../scanCenter/handleSelectConnection.js'; |
| 6 | +import { handleSelectRepo as _handleSelectRepo } from '../scanCenter/handleSelectRepo.js'; |
| 7 | +import { handleSelectScan as _handleSelectScan } from '../scanCenter/handleSelectScan.js'; |
| 8 | +import { handleSelectResultType as _handleSelectResultType } from '../scanCenter/handleSelectResultType.js'; |
10 | 9 |
|
11 | 10 | const ce = React.createElement; |
12 | 11 |
|
@@ -182,88 +181,24 @@ export default function ScanCenter() { |
182 | 181 |
|
183 | 182 | // ── Step 1: validate connection on mount ── |
184 | 183 | useEffect(() => { |
185 | | - (async () => { |
186 | | - const res = await validateConnection(); |
187 | | - if (!res.success) { |
188 | | - setError(res.error || 'Failed to validate connection', null); |
189 | | - return; |
190 | | - } |
191 | | - if (!res.connections || res.connections.length === 0) { |
192 | | - setError('No connected organisations found. Please log in to CodeAnt first.', null); |
193 | | - return; |
194 | | - } |
195 | | - setConnections(res.connections); |
196 | | - setStep(STEPS.SELECT_CONNECTION); |
197 | | - })(); |
| 184 | + validateConnectionOnMount({ STEPS, setError, setConnections, setStep }); |
198 | 185 | }, []); |
199 | 186 |
|
200 | 187 | // ── Step 2: connection selected → fetch repos ── |
201 | | - const handleSelectConnection = async (item) => { |
202 | | - setSelectedConnection(item.value); |
203 | | - setStep(STEPS.LOADING); |
204 | | - setLoadingMsg(`Fetching repos for ${item.value.organizationName}…`); |
205 | | - const res = await listRepos(item.value.organizationName); |
206 | | - if (!res.success) { |
207 | | - setError(res.error || 'Failed to fetch repos', STEPS.SELECT_CONNECTION); |
208 | | - return; |
209 | | - } |
210 | | - setRepos(res.repos || []); |
211 | | - setStep(STEPS.SELECT_REPO); |
212 | | - }; |
| 188 | + const handleSelectConnection = (item) => |
| 189 | + _handleSelectConnection({ STEPS, item, setSelectedConnection, setStep, setLoadingMsg, setError, setRepos }); |
213 | 190 |
|
214 | 191 | // ── Step 3: repo selected → fetch scan history ── |
215 | | - const handleSelectRepo = async (item) => { |
216 | | - setSelectedRepo(item.value); |
217 | | - setStep(STEPS.LOADING); |
218 | | - setLoadingMsg(`Loading scan history for ${item.value.full_name}…`); |
219 | | - const res = await getScanHistory(item.value.full_name); |
220 | | - if (!res.success) { |
221 | | - setError(res.error || 'Failed to fetch scan history', STEPS.SELECT_REPO); |
222 | | - return; |
223 | | - } |
224 | | - const history = res.scanHistory || []; |
225 | | - process.stderr.write('SCAN_HISTORY_SAMPLE: ' + JSON.stringify(history.slice(0, 15), null, 2) + '\n'); |
226 | | - setScanHistory(history); |
227 | | - setStep(STEPS.SELECT_SCAN); |
228 | | - }; |
| 192 | + const handleSelectRepo = (item) => |
| 193 | + _handleSelectRepo({ STEPS, item, setSelectedRepo, setStep, setLoadingMsg, setError, setScanHistory }); |
229 | 194 |
|
230 | 195 | // ── Step 4: scan selected → show result type menu ── |
231 | | - const handleSelectScan = (item) => { |
232 | | - setSelectedScan(item.value); |
233 | | - setStep(STEPS.SELECT_RESULT_TYPE); |
234 | | - }; |
| 196 | + const handleSelectScan = (item) => |
| 197 | + _handleSelectScan({ STEPS, item, setSelectedScan, setStep }); |
235 | 198 |
|
236 | 199 | // ── Step 5: result type selected → fetch exactly one endpoint ── |
237 | | - const handleSelectResultType = async (item) => { |
238 | | - const rt = item.value; |
239 | | - setSelectedResultType(rt); |
240 | | - setStep(STEPS.LOADING); |
241 | | - setLoadingMsg(`Fetching ${item.label}…`); |
242 | | - |
243 | | - const repo = selectedRepo.full_name; |
244 | | - const commitId = selectedScan.commitId; |
245 | | - let res; |
246 | | - |
247 | | - if (rt.kind === 'basic') { |
248 | | - res = await fetchScanResults(repo, commitId, rt.value); |
249 | | - } else if (rt.kind === 'advanced') { |
250 | | - res = await fetchAdvancedScanResults(repo, commitId, rt.value); |
251 | | - } else if (rt.value === 'dismissed_alerts') { |
252 | | - const r = await fetchDismissedAlerts(repo, 'security'); |
253 | | - res = r.success ? { success: true, issues: r.dismissedAlerts } : r; |
254 | | - } else if (rt.value === 'dismissed_secrets') { |
255 | | - const r = await fetchDismissedAlerts(repo, 'secrets'); |
256 | | - res = r.success ? { success: true, issues: r.dismissedAlerts } : r; |
257 | | - } |
258 | | - |
259 | | - if (!res || !res.success) { |
260 | | - setError((res && res.error) || 'Failed to fetch results', STEPS.SELECT_RESULT_TYPE); |
261 | | - return; |
262 | | - } |
263 | | - |
264 | | - setResults(res); |
265 | | - setStep(STEPS.SHOWING_RESULTS); |
266 | | - }; |
| 200 | + const handleSelectResultType = (item) => |
| 201 | + _handleSelectResultType({ STEPS, item, selectedRepo, selectedScan, setSelectedResultType, setStep, setLoadingMsg, setError, setResults }); |
267 | 202 |
|
268 | 203 | // ── Back navigation ── |
269 | 204 | const goBack = { |
|
0 commit comments