diff --git a/apps/dashboard/src/components/TerminalView.tsx b/apps/dashboard/src/components/TerminalView.tsx index d2706d8..064bdc7 100644 --- a/apps/dashboard/src/components/TerminalView.tsx +++ b/apps/dashboard/src/components/TerminalView.tsx @@ -59,6 +59,7 @@ export function TerminalView({ const controllerReadOnlyRef = useRef(false); const [searchOpen, setSearchOpen] = useState(false); const [historyOpen, setHistoryOpen] = useState(false); + const [historyOverlaySessionId, setHistoryOverlaySessionId] = useState(null); const [stickyCtrlMode, setStickyCtrlMode] = useState('inactive'); const [keyboardActive, setKeyboardActive] = useState(false); const [cursorArmed, setCursorArmed] = useState(false); @@ -268,10 +269,19 @@ export function TerminalView({ stickyCtrlModeRef.current = 'inactive'; setStickyCtrlMode('inactive'); resetTouchModes(); + setHistoryOverlaySessionId(null); }, [resetTouchModes, status]); - const handleNavigateScroll = useCallback((lines: number) => { - navigate({ type: 'navigate', op: 'scroll', lines }); - }, [navigate]); + const closeHistoryOverlay = useCallback(() => setHistoryOverlaySessionId(null), []); + const openHistoryOverlay = useCallback(() => { + if (!tmuxSessionKey || !historySessionId) return; + setHistoryOverlaySessionId(historySessionId); + }, [historySessionId, tmuxSessionKey]); + const historyOverlayOpen = historyOverlaySessionId === historySessionId; + useEffect(() => { + if (historyOverlaySessionId && historyOverlaySessionId !== historySessionId) { + setHistoryOverlaySessionId(null); + } + }, [historyOverlaySessionId, historySessionId]); const touchScrollRef = useTerminalTouchScroll({ enabled: touchInputModeEnabled, termRef, @@ -284,7 +294,8 @@ export function TerminalView({ writable: terminalWritable, onScrollInput: sendInput, tmuxSessionKey, - onNavigateScroll: handleNavigateScroll, + historySessionId, + onOpenHistory: openHistoryOverlay, onHorizontalSwipe: (direction) => { termRef.current?.dispatchEvent(new CustomEvent('terminal-window-swipe', { bubbles: true, @@ -419,6 +430,10 @@ export function TerminalView({ stickyCtrlMode={stickyCtrlMode} onStickyCtrlEvent={dispatchStickyCtrl} onOpenHistory={() => setHistoryOpen(true)} + historyOverlayOpen={historyOverlayOpen} + historySessionId={historySessionId} + historyFontSize={terminalFontSize} + onCloseHistoryOverlay={closeHistoryOverlay} tmuxPrefix={tmuxPrefix} onPreviousMark={previousMark} onNextMark={nextMark} diff --git a/apps/dashboard/src/components/terminal/ScrollbackPager.tsx b/apps/dashboard/src/components/terminal/ScrollbackPager.tsx index 9205418..e2ea619 100644 --- a/apps/dashboard/src/components/terminal/ScrollbackPager.tsx +++ b/apps/dashboard/src/components/terminal/ScrollbackPager.tsx @@ -10,6 +10,8 @@ import { useNotifications } from '@/stores/notifications'; import { SCROLLBACK_PAGE_LINES, advanceScrollbackSelection, + compensateScrollbackPrepend, + contentScrollbackLines, initialScrollbackRange, numberScrollbackLines, olderScrollbackRange, @@ -33,13 +35,6 @@ const LINE_HEIGHT = 32; const OVERSCAN = 12; const VIEWPORT_LINE_ESTIMATE = 30; -function contentLines(content: unknown): string[] { - if (typeof content !== 'string' || content.length === 0) return []; - const lines = content.split('\n'); - if (content.endsWith('\n')) lines.pop(); - return lines; -} - export function ScrollbackPager({ sessionId, open, onClose }: ScrollbackPagerProps) { const [pages, setPages] = useState([]); const [filter, setFilter] = useState(''); @@ -68,7 +63,7 @@ export function ScrollbackPager({ sessionId, open, onClose }: ScrollbackPagerPro } return { range, - lines: contentLines(response.result?.content), + lines: contentScrollbackLines(response.result?.content), }; }, [sessionId] @@ -126,7 +121,13 @@ export function ScrollbackPager({ sessionId, open, onClose }: ScrollbackPagerPro setPages((current) => [page, ...current]); setHasOlder(page.lines.length >= SCROLLBACK_PAGE_LINES); requestAnimationFrame(() => { - if (element) element.scrollTop += element.scrollHeight - previousHeight; + if (element) { + element.scrollTop = compensateScrollbackPrepend( + element.scrollTop, + previousHeight, + element.scrollHeight + ); + } }); } catch (loadError) { setError( diff --git a/apps/dashboard/src/components/terminal/TerminalHistoryOverlay.tsx b/apps/dashboard/src/components/terminal/TerminalHistoryOverlay.tsx new file mode 100644 index 0000000..bfba1a4 --- /dev/null +++ b/apps/dashboard/src/components/terminal/TerminalHistoryOverlay.tsx @@ -0,0 +1,292 @@ +'use client'; + +import { useCallback, useEffect, useMemo, useRef, useState } from 'react'; +import { ArrowDown, Loader2, RotateCcw } from 'lucide-react'; +import { Button } from '@/components/ui/button'; +import { getSessionScrollback } from '@/lib/api'; +import { + SCROLLBACK_PAGE_LINES, + compensateScrollbackPrepend, + contentScrollbackLines, + initialScrollbackRange, + isNearScrollbackTop, + numberScrollbackLines, + olderScrollbackRange, + resolveScrollbackVirtualWindow, + shouldDismissHistoryOverscroll, + type ScrollbackRange, +} from './scrollbackPaging'; + +interface TerminalHistoryOverlayProps { + sessionId: string; + open: boolean; + fontSize: number; + onClose: () => void; +} + +interface HistoryPage { + range: ScrollbackRange; + lines: string[]; +} + +const OVERSCAN_LINES = 12; + +export function TerminalHistoryOverlay({ + sessionId, + open, + fontSize, + onClose, +}: TerminalHistoryOverlayProps) { + const [pages, setPages] = useState([]); + const [loadingInitial, setLoadingInitial] = useState(false); + const [loadingOlder, setLoadingOlder] = useState(false); + const [hasOlder, setHasOlder] = useState(true); + const [error, setError] = useState(null); + const [scrollTop, setScrollTop] = useState(0); + const [viewportHeight, setViewportHeight] = useState(0); + const scrollRef = useRef(null); + const generationRef = useRef(0); + const loadingOlderRef = useRef(false); + const overscrollStartRef = useRef<{ y: number; atBottom: boolean } | null>(null); + const lineHeight = Math.max(16, Math.round(fontSize * 1.2)); + + const fetchPage = useCallback(async (range: ScrollbackRange): Promise => { + const response = await getSessionScrollback(sessionId, { + mode: 'range', + start_line: range.startLine, + end_line: range.endLine, + strip_ansi: true, + }); + if (!response.ok) { + throw new Error(response.error?.message || 'Scrollback capture failed.'); + } + return { + range, + lines: contentScrollbackLines(response.result?.content), + }; + }, [sessionId]); + + const loadInitial = useCallback(async () => { + const generation = ++generationRef.current; + setLoadingInitial(true); + setError(null); + setPages([]); + setHasOlder(true); + setScrollTop(0); + try { + const page = await fetchPage(initialScrollbackRange()); + if (generationRef.current !== generation) return; + setPages([page]); + setHasOlder(page.lines.length >= SCROLLBACK_PAGE_LINES); + requestAnimationFrame(() => { + const element = scrollRef.current; + if (!element || generationRef.current !== generation) return; + element.scrollTop = element.scrollHeight; + setScrollTop(element.scrollTop); + setViewportHeight(element.clientHeight); + }); + } catch (loadError) { + if (generationRef.current !== generation) return; + setError( + loadError instanceof Error ? loadError.message : 'Terminal history could not be loaded.' + ); + } finally { + if (generationRef.current === generation) setLoadingInitial(false); + } + }, [fetchPage]); + + useEffect(() => { + if (!open) { + generationRef.current += 1; + loadingOlderRef.current = false; + overscrollStartRef.current = null; + setLoadingOlder(false); + return; + } + void loadInitial(); + }, [loadInitial, open]); + + useEffect(() => { + if (!open) return; + const element = scrollRef.current; + if (!element) return; + const updateHeight = () => setViewportHeight(element.clientHeight); + updateHeight(); + const observer = new ResizeObserver(updateHeight); + observer.observe(element); + return () => observer.disconnect(); + }, [open]); + + const loadOlder = useCallback(async () => { + if (loadingOlderRef.current || !hasOlder) return; + const generation = generationRef.current; + const currentOldest = pages[0]?.range ?? initialScrollbackRange(); + const range = olderScrollbackRange(currentOldest); + const element = scrollRef.current; + const previousHeight = element?.scrollHeight ?? 0; + loadingOlderRef.current = true; + setLoadingOlder(true); + setError(null); + try { + const page = await fetchPage(range); + if (generationRef.current !== generation) return; + setPages((current) => [page, ...current]); + setHasOlder(page.lines.length >= SCROLLBACK_PAGE_LINES); + requestAnimationFrame(() => { + if (!element || generationRef.current !== generation) return; + const nextTop = compensateScrollbackPrepend( + element.scrollTop, + previousHeight, + element.scrollHeight + ); + element.scrollTop = nextTop; + setScrollTop(nextTop); + }); + } catch (loadError) { + if (generationRef.current !== generation) return; + setError( + loadError instanceof Error ? loadError.message : 'Older history could not be loaded.' + ); + } finally { + if (generationRef.current === generation) { + loadingOlderRef.current = false; + setLoadingOlder(false); + } + } + }, [fetchPage, hasOlder, pages]); + + const allLines = useMemo( + () => pages.flatMap((page) => numberScrollbackLines(page.range, page.lines)), + [pages] + ); + const virtualWindow = resolveScrollbackVirtualWindow({ + lineCount: allLines.length, + scrollTop, + viewportHeight, + lineHeight, + overscan: OVERSCAN_LINES, + }); + const visibleLines = allLines.slice(virtualWindow.startIndex, virtualWindow.endIndex); + + if (!open) return null; + + return ( +
+
+
+ {loadingOlder ? 'Loading older…' : hasOlder ? 'History' : 'Start of history'} +
+ +
+ + {error && ( +
+ {error} + +
+ )} + +
{ + const element = event.currentTarget; + setScrollTop(element.scrollTop); + setViewportHeight(element.clientHeight); + if (isNearScrollbackTop(element.scrollTop, lineHeight)) void loadOlder(); + }} + onTouchStart={(event) => { + const touch = event.touches[0]; + const element = event.currentTarget; + if (!touch) return; + overscrollStartRef.current = { + y: touch.clientY, + atBottom: element.scrollHeight - element.clientHeight - element.scrollTop <= 2, + }; + }} + onTouchMove={(event) => { + const start = overscrollStartRef.current; + const touch = event.touches[0]; + if (!start || !touch) return; + if (shouldDismissHistoryOverscroll(start.atBottom, touch.clientY - start.y)) { + overscrollStartRef.current = null; + onClose(); + } + }} + onTouchEnd={() => { overscrollStartRef.current = null; }} + onTouchCancel={() => { overscrollStartRef.current = null; }} + > + {loadingInitial ? ( +
+
+ ) : allLines.length === 0 ? ( +
+ No terminal history was captured. +
+ ) : ( +
+ {visibleLines.map((line, offset) => { + const virtualIndex = virtualWindow.startIndex + offset; + return ( +
+ {line.text || ' '} +
+ ); + })} +
+ )} +
+
+ ); +} diff --git a/apps/dashboard/src/components/terminal/TerminalSurface.tsx b/apps/dashboard/src/components/terminal/TerminalSurface.tsx index 643047f..56e6600 100644 --- a/apps/dashboard/src/components/terminal/TerminalSurface.tsx +++ b/apps/dashboard/src/components/terminal/TerminalSurface.tsx @@ -9,6 +9,7 @@ import type { StickyCtrlEvent, StickyCtrlMode } from '@/components/mobile/sticky import { Button } from '@/components/ui/button'; import { cn } from '@/lib/utils'; import type { ConnectionStatus, XTerminal } from './types'; +import { TerminalHistoryOverlay } from './TerminalHistoryOverlay'; import { TerminalSearchSheet, type TerminalSearchControlsProps } from './TerminalSearch'; import { TerminalTouchSelection } from './TerminalTouchSelection'; import type { TerminalCommandMarkView } from './commandMarks'; @@ -32,6 +33,10 @@ interface TerminalSurfaceProps { stickyCtrlMode: StickyCtrlMode; onStickyCtrlEvent: (event: StickyCtrlEvent) => void; onOpenHistory: () => void; + historyOverlayOpen: boolean; + historySessionId: string; + historyFontSize: number; + onCloseHistoryOverlay: () => void; tmuxPrefix?: string; onPreviousMark: () => void; onNextMark: () => void; @@ -68,6 +73,10 @@ export function TerminalSurface({ stickyCtrlMode, onStickyCtrlEvent, onOpenHistory, + historyOverlayOpen, + historySessionId, + historyFontSize, + onCloseHistoryOverlay, tmuxPrefix, onPreviousMark, onNextMark, @@ -161,6 +170,13 @@ export function TerminalSurface({