|
1 | 1 | import React, { useState, useEffect } from 'react'; |
2 | | -import { Box, Text } from 'ink'; |
| 2 | +import { Box, Text, useInput } from 'ink'; |
3 | 3 |
|
4 | 4 | // ── Constants ──────────────────────────────────────────────────────────────── |
5 | 5 |
|
@@ -326,3 +326,89 @@ export function renderDone(secrets, startTime, fileCount, meta) { |
326 | 326 | Box, { flexDirection: 'column', paddingX: 1, paddingY: 1 }, ...els, |
327 | 327 | ); |
328 | 328 | } |
| 329 | + |
| 330 | + |
| 331 | +// ── Bypass Prompt ─────────────────────────────────────────────────────────── |
| 332 | + |
| 333 | +function BypassPrompt({ secrets, onSelect }) { |
| 334 | + const [selected, setSelected] = React.useState(0); |
| 335 | + const [mode, setMode] = React.useState('menu'); // 'menu' | 'typing' |
| 336 | + const [customReason, setCustomReason] = React.useState(''); |
| 337 | + const allSecrets = secrets.flatMap(file => |
| 338 | + file.secrets.map(s => ({ ...s, file_path: file.file_path })) |
| 339 | + ); |
| 340 | + |
| 341 | + const options = [ |
| 342 | + { label: "It's a false positive", value: 'false_positive' }, |
| 343 | + { label: "It's used in tests", value: 'used_in_tests' }, |
| 344 | + { label: "I'll fix it later", value: 'fix_later' }, |
| 345 | + { label: 'Other (type your reason)', value: 'other' }, |
| 346 | + { label: 'Cancel \u2014 block this push', value: 'cancel' }, |
| 347 | + ]; |
| 348 | + |
| 349 | + useInput((input, key) => { |
| 350 | + if (mode === 'menu') { |
| 351 | + if (key.upArrow) { |
| 352 | + setSelected(s => (s - 1 + options.length) % options.length); |
| 353 | + } else if (key.downArrow) { |
| 354 | + setSelected(s => (s + 1) % options.length); |
| 355 | + } else if (key.return) { |
| 356 | + if (options[selected].value === 'other') { |
| 357 | + setMode('typing'); |
| 358 | + } else { |
| 359 | + onSelect(options[selected].value); |
| 360 | + } |
| 361 | + } |
| 362 | + } else { |
| 363 | + // typing mode |
| 364 | + if (key.return) { |
| 365 | + const reason = customReason.trim(); |
| 366 | + if (reason) { |
| 367 | + onSelect('other:' + reason); |
| 368 | + } |
| 369 | + } else if (key.escape) { |
| 370 | + setMode('menu'); |
| 371 | + setCustomReason(''); |
| 372 | + } else if (key.backspace || key.delete) { |
| 373 | + setCustomReason(s => s.slice(0, -1)); |
| 374 | + } else if (input && !key.ctrl && !key.meta) { |
| 375 | + setCustomReason(s => s + input); |
| 376 | + } |
| 377 | + } |
| 378 | + }); |
| 379 | + |
| 380 | + const els = []; |
| 381 | + els.push(React.createElement(Text, { key: 'div-top', color: 'gray', dimColor: true }, DIVIDER)); |
| 382 | + els.push(React.createElement( |
| 383 | + Text, { key: 'title', color: 'yellow', bold: true }, |
| 384 | + `${allSecrets.length} secret(s) detected. Select an action:`, |
| 385 | + )); |
| 386 | + els.push(React.createElement(Text, { key: 'sp' }, '')); |
| 387 | + |
| 388 | + if (mode === 'menu') { |
| 389 | + options.forEach((opt, i) => { |
| 390 | + const prefix = i === selected ? '> ' : ' '; |
| 391 | + const color = i === selected ? 'cyan' : 'white'; |
| 392 | + els.push(React.createElement( |
| 393 | + Text, { key: `opt-${i}`, color, bold: i === selected }, |
| 394 | + `${prefix}${opt.label}`, |
| 395 | + )); |
| 396 | + }); |
| 397 | + } else { |
| 398 | + els.push(React.createElement(Text, { key: 'typing-label', color: 'cyan' }, ' Type your reason (Enter to submit, Esc to go back):')); |
| 399 | + els.push(React.createElement(Text, { key: 'typing-sp' }, '')); |
| 400 | + els.push(React.createElement( |
| 401 | + Text, { key: 'typing-input', color: 'white' }, |
| 402 | + ` > ${customReason}\u2588`, |
| 403 | + )); |
| 404 | + } |
| 405 | + |
| 406 | + els.push(React.createElement(Text, { key: 'sp2' }, '')); |
| 407 | + els.push(React.createElement(Text, { key: 'div-bot', color: 'gray', dimColor: true }, DIVIDER)); |
| 408 | + |
| 409 | + return React.createElement(Box, { flexDirection: 'column', paddingX: 1, paddingY: 1 }, ...els); |
| 410 | +} |
| 411 | + |
| 412 | +export function renderBypassPrompt(secrets, onSelect) { |
| 413 | + return React.createElement(BypassPrompt, { secrets, onSelect }); |
| 414 | +} |
0 commit comments