({
sessions,
onSelect,
onExit,
}: SessionSelectorProps)
| 37 | } |
| 38 | |
| 39 | export function SessionSelector({ |
| 40 | sessions, |
| 41 | onSelect, |
| 42 | onExit, |
| 43 | }: SessionSelectorProps) { |
| 44 | const [selectedIndex, setSelectedIndex] = useState(0); |
| 45 | const [previewSession, setPreviewSession] = useState<Session | null>(null); |
| 46 | const { rows: terminalHeight, columns: terminalWidth } = useTerminalSize(); |
| 47 | |
| 48 | // Load the selected session for preview |
| 49 | useEffect(() => { |
| 50 | const selectedSession = sessions[selectedIndex]; |
| 51 | if (selectedSession && !selectedSession.isRemote) { |
| 52 | const session = loadSessionById(selectedSession.sessionId); |
| 53 | setPreviewSession(session); |
| 54 | } else { |
| 55 | setPreviewSession(null); |
| 56 | } |
| 57 | }, [selectedIndex, sessions]); |
| 58 | |
| 59 | // Calculate how many sessions we can display based on terminal height and scrolling |
| 60 | const { displaySessions, scrollOffset } = useMemo(() => { |
| 61 | // Account for: |
| 62 | // - Box border (top + bottom): 2 lines |
| 63 | // - Box padding (top + bottom): 2 lines |
| 64 | // - Header "Recent Sessions": 1 line |
| 65 | // - Instructions line: 1 line |
| 66 | // - Empty line after instructions: 1 line |
| 67 | // - Potential scroll indicators: up to 2 lines (1 above, 1 below) |
| 68 | // Total overhead: ~9 lines |
| 69 | const MAGIC_NUMBER = 9; |
| 70 | const availableHeight = Math.max(1, terminalHeight - MAGIC_NUMBER); |
| 71 | const maxDisplayableSessions = Math.floor(availableHeight / 3); |
| 72 | |
| 73 | // If we can display all sessions, no need to scroll |
| 74 | if (sessions.length <= maxDisplayableSessions) { |
| 75 | return { displaySessions: sessions, scrollOffset: 0 }; |
| 76 | } |
| 77 | |
| 78 | // Calculate scroll offset to keep selected item visible |
| 79 | let scrollOffset = 0; |
| 80 | if (selectedIndex >= maxDisplayableSessions) { |
| 81 | scrollOffset = Math.min( |
| 82 | selectedIndex - maxDisplayableSessions + 1, |
| 83 | sessions.length - maxDisplayableSessions, |
| 84 | ); |
| 85 | } |
| 86 | |
| 87 | const displaySessions = sessions.slice( |
| 88 | scrollOffset, |
| 89 | scrollOffset + maxDisplayableSessions, |
| 90 | ); |
| 91 | return { displaySessions, scrollOffset }; |
| 92 | }, [sessions, terminalHeight, selectedIndex]); |
| 93 | |
| 94 | useInput((input, key) => { |
| 95 | if (key.upArrow || input === "k") { |
| 96 | setSelectedIndex((prev) => (prev > 0 ? prev - 1 : sessions.length - 1)); |
nothing calls this directly
no test coverage detected