import figures from 'figures' import React, { useEffect, useState } from 'react' import { Box, Text, Dialog } from '@anthropic/ink' import { logForDebugging } from '../utils/debug.js' import type { GitFileStatus } from '../utils/git.js' import { getFileStatus, stashToCleanState } from '../utils/git.js' import { Select } from './CustomSelect/index.js' import { Spinner } from './Spinner.js' type TeleportStashProps = { onStashAndContinue: () => void onCancel: () => void } export function TeleportStash({ onStashAndContinue, onCancel, }: TeleportStashProps): React.ReactNode { const [gitFileStatus, setGitFileStatus] = useState(null) const changedFiles = gitFileStatus !== null ? [...gitFileStatus.tracked, ...gitFileStatus.untracked] : [] const [loading, setLoading] = useState(true) const [stashing, setStashing] = useState(false) const [error, setError] = useState(null) // Load changed files on mount useEffect(() => { const loadChangedFiles = async () => { try { const fileStatus = await getFileStatus() setGitFileStatus(fileStatus) } catch (err) { const errorMessage = err instanceof Error ? err.message : String(err) logForDebugging(`Error getting changed files: ${errorMessage}`, { level: 'error', }) setError('Failed to get changed files') } finally { setLoading(false) } } void loadChangedFiles() }, []) const handleStash = async () => { setStashing(true) try { logForDebugging('Stashing changes before teleport...') const success = await stashToCleanState('Teleport auto-stash') if (success) { logForDebugging('Successfully stashed changes') onStashAndContinue() } else { setError('Failed to stash changes') } } catch (err) { const errorMessage = err instanceof Error ? err.message : String(err) logForDebugging(`Error stashing changes: ${errorMessage}`, { level: 'error', }) setError('Failed to stash changes') } finally { setStashing(false) } } const handleSelectChange = (value: string) => { if (value === 'stash') { void handleStash() } else { onCancel() } } if (loading) { return ( Checking git status{figures.ellipsis} ) } if (error) { return ( Error: {error} Press Escape to cancel ) } const showFileCount = changedFiles.length > 8 return ( Teleport will switch git branches. The following changes were found: {changedFiles.length > 0 ? ( showFileCount ? ( {changedFiles.length} files changed ) : ( changedFiles.map((file: string, index: number) => ( {file} )) ) ) : ( No changes detected )} Would you like to stash these changes and continue with teleport? {stashing ? ( Stashing changes... ) : (