/** * @license * Copyright 2025 Qwen Code * SPDX-License-Identifier: Apache-2.0 */ import { useState, useEffect, useRef } from 'react'; import { Box, Text } from 'ink'; import { SessionService, getGitBranch } from '@qwen-code/qwen-code-core'; import { theme } from '../semantic-colors.js'; import { useDialogSessionPicker } from '../hooks/useDialogSessionPicker.js'; import { SessionListItemView } from './SessionListItem.js'; import { t } from '../../i18n/index.js'; export interface ResumeSessionDialogProps { cwd: string; onSelect: (sessionId: string) => void; onCancel: () => void; availableTerminalHeight?: number; } export function ResumeSessionDialog({ cwd, onSelect, onCancel, availableTerminalHeight, }: ResumeSessionDialogProps): React.JSX.Element { const sessionServiceRef = useRef(null); const [currentBranch, setCurrentBranch] = useState(); const [isReady, setIsReady] = useState(false); // Initialize session service useEffect(() => { sessionServiceRef.current = new SessionService(cwd); setCurrentBranch(getGitBranch(cwd)); setIsReady(true); }, [cwd]); // Calculate visible items based on terminal height const maxVisibleItems = availableTerminalHeight ? Math.max(3, Math.floor((availableTerminalHeight - 6) / 3)) : 5; const picker = useDialogSessionPicker({ sessionService: sessionServiceRef.current, currentBranch, onSelect, onCancel, maxVisibleItems, centerSelection: false, isActive: isReady, }); if (!isReady || picker.isLoading) { return ( {t('Resume Session')} {t('Loading sessions...')} ); } return ( {/* Header */} {t('Resume Session')} {picker.filterByBranch && currentBranch && ( {' '} {t('(branch: {{branch}})', { branch: currentBranch })} )} {/* Session List */} {picker.filteredSessions.length === 0 ? ( {picker.filterByBranch ? t('No sessions found for branch "{{branch}}"', { branch: currentBranch ?? '', }) : t('No sessions found')} ) : ( picker.visibleSessions.map((session, visibleIndex) => { const actualIndex = picker.scrollOffset + visibleIndex; return ( ); }) )} {/* Footer */} {currentBranch && ( <> B {t(' to toggle branch') + ' · '} )} {t('to navigate · Enter to select · Esc to cancel')} ); }