import React, { useCallback, useState } from 'react' import TextInput from '../../components/TextInput.js' import { useTerminalSize } from '../../hooks/useTerminalSize.js' import { Box, Text } from '@anthropic/ink' import { useKeybindings } from '../../keybindings/useKeybinding.js' interface ChooseRepoStepProps { currentRepo: string | null useCurrentRepo: boolean repoUrl: string onRepoUrlChange: (value: string) => void onToggleUseCurrentRepo: (useCurrentRepo: boolean) => void onSubmit: () => void } export function ChooseRepoStep({ currentRepo, useCurrentRepo, repoUrl, onRepoUrlChange, onSubmit, onToggleUseCurrentRepo, }: ChooseRepoStepProps) { const [cursorOffset, setCursorOffset] = useState(0) const [showEmptyError, setShowEmptyError] = useState(false) const terminalSize = useTerminalSize() const textInputColumns = terminalSize.columns const handleSubmit = useCallback(() => { const repoName = useCurrentRepo ? currentRepo : repoUrl if (!repoName?.trim()) { setShowEmptyError(true) return } onSubmit() }, [useCurrentRepo, currentRepo, repoUrl, onSubmit]) // When the text input is visible, omit confirm:yes so bare 'y' passes // through to the input instead of submitting. TextInput's onSubmit handles // Enter. Keep the Confirmation context (not Settings) to avoid j/k bindings. const isTextInputVisible = !useCurrentRepo || !currentRepo const handlePrevious = useCallback(() => { onToggleUseCurrentRepo(true) setShowEmptyError(false) }, [onToggleUseCurrentRepo]) const handleNext = useCallback(() => { onToggleUseCurrentRepo(false) setShowEmptyError(false) }, [onToggleUseCurrentRepo]) useKeybindings( { 'confirm:previous': handlePrevious, 'confirm:next': handleNext, 'confirm:yes': handleSubmit, }, { context: 'Confirmation', isActive: !isTextInputVisible }, ) useKeybindings( { 'confirm:previous': handlePrevious, 'confirm:next': handleNext, }, { context: 'Confirmation', isActive: isTextInputVisible }, ) return ( <> Install GitHub App Select GitHub repository {currentRepo && ( {useCurrentRepo ? '> ' : ' '} Use current repository: {currentRepo} )} {!useCurrentRepo || !currentRepo ? '> ' : ' '} {currentRepo ? 'Enter a different repository' : 'Enter repository'} {(!useCurrentRepo || !currentRepo) && ( { onRepoUrlChange(value) setShowEmptyError(false) }} onSubmit={handleSubmit} focus={true} placeholder="Enter a repo as owner/repo or https://github.com/owner/repo…" columns={textInputColumns} cursorOffset={cursorOffset} onChangeCursorOffset={setCursorOffset} showCursor={true} /> )} {showEmptyError && ( Please enter a repository name to continue )} {currentRepo ? '↑/↓ to select · ' : ''}Enter to continue ) }