import React from 'react'; import { Alert, Banner, Button, Checkbox, Dropdown, DropdownItem, DropdownList, Flex, FlexItem, MenuToggle, Popover, SearchInput, Spinner, Toolbar, ToolbarContent, ToolbarGroup, ToolbarItem, Truncate, } from '@patternfly/react-core'; import { AngleDownIcon } from '@patternfly/react-icons/dist/esm/icons/angle-down-icon'; import { AngleUpIcon } from '@patternfly/react-icons/dist/esm/icons/angle-up-icon'; import { CompressIcon } from '@patternfly/react-icons/dist/esm/icons/compress-icon'; import { DownloadIcon } from '@patternfly/react-icons/dist/esm/icons/download-icon'; import { ExpandIcon } from '@patternfly/react-icons/dist/esm/icons/expand-icon'; import { OutlinedKeyboardIcon } from '@patternfly/react-icons/dist/esm/icons/outlined-keyboard-icon'; import { OutlinedPlayCircleIcon } from '@patternfly/react-icons/dist/esm/icons/outlined-play-circle-icon'; import classNames from 'classnames'; import { saveAs } from 'file-saver'; import { v4 as uuidv4 } from 'uuid'; import { FeatureFlagIndicator } from '~/feature-flags/FeatureFlagIndicator'; import { logger } from '~/monitoring/logger'; import { KeyboardShortcutHint, type ShortcutEntry, } from '~/shared/components/keyboard-shortcut-hint'; import { useAutoScrollWithResume } from '~/shared/components/pipeline-run-logs/logs/useAutoScrollWithResume'; import { LoadingInline } from '~/shared/components/status-box/StatusBox'; import { type LogSection, type NormalizedLogSection, type VirtualizedLogContentImperativeHandleMethods, normalizeSection, useLineNumberNavigation, VirtualizedLogContent, } from '~/shared/components/virtualized-log-viewer'; import { useContainerHeight } from '~/shared/hooks'; import { useFullscreen } from '~/shared/hooks/fullscreen'; import { TaskRunKind } from '~/types'; import { useLogSearch } from '../useLogSearch'; import LogsTaskDuration from './LogsTaskDuration'; import { useLineWrap } from './useLineWrap'; import { useLogViewerTheme } from './useLogViewerTheme'; import '@patternfly/react-log-viewer/dist/css/log-viewer.css'; import './LogViewer.scss'; const LOG_VIEWER_SHORTCUTS: ShortcutEntry[] = [ { keys: 'Arrow Up', macKeys: 'Arrow Up', description: 'Scroll up one line' }, { keys: 'Arrow Down', macKeys: 'Arrow Down', description: 'Scroll down one line' }, { keys: 'PageUp', macKeys: 'Fn + Arrow Up', description: 'Scroll up one page' }, { keys: 'PageDown', macKeys: 'Fn + Arrow Down', description: 'Scroll down one page' }, { keys: 'Home', macKeys: 'Fn + Arrow Left', description: 'Scroll to top' }, { keys: 'End', macKeys: 'Fn + Arrow Right', description: 'Scroll to bottom' }, ]; export type Props = { showSearch?: boolean; sections?: LogSection[]; normalizedSections?: NormalizedLogSection[]; allowAutoScroll?: boolean; downloadAllLabel?: string; onDownloadAll?: () => Promise; onDownloadFullLogs?: (sectionIndex: number) => Promise; onViewFullLogs?: (sectionIndex: number) => void; taskRun: TaskRunKind | null; isLoading: boolean; errorMessage: string | null; onScroll?: (props: { scrollDirection: 'forward' | 'backward'; scrollOffset: number; scrollUpdateWasRequested: boolean; }) => void; enableLineNavigation?: boolean; allowExpandAllSections?: boolean; }; const LogViewer: React.FC = ({ showSearch = true, allowAutoScroll, sections, normalizedSections: normalizedSectionsProp, downloadAllLabel = 'Download all task logs', onDownloadAll, onDownloadFullLogs, onViewFullLogs, taskRun, isLoading, errorMessage, onScroll: onScrollProp, enableLineNavigation = true, allowExpandAllSections = false, }) => { const taskName = taskRun?.spec.taskRef?.name ?? taskRun?.metadata.name; const [logTheme, setLogTheme] = useLogViewerTheme(); const themeCheckboxId = React.useId(); const wrapLineCheckboxId = React.useId(); const normalizedSections = React.useMemo( () => normalizedSectionsProp ?? sections?.map(normalizeSection) ?? [], [normalizedSectionsProp, sections], ); const lineNumberNavigationProps = useLineNumberNavigation(); const { autoScroll, showResumeStreamButton, handleScroll, handleResumeClick } = useAutoScrollWithResume({ allowAutoScroll, activeLineTarget: enableLineNavigation ? lineNumberNavigationProps.highlightedLines : null, onScroll: onScrollProp, }); const [isFullscreen, fullscreenRef, fullscreenToggle, isFullscreenSupported] = useFullscreen(); const downloadData = React.useMemo(() => { return normalizedSections .map((s) => s.containerName ? `${s.containerName}\n${s.lines.join('\n')}` : s.lines.join('\n'), ) .join('\n\n'); }, [normalizedSections]); const [downloadAllStatus, setDownloadAllStatus] = React.useState(false); const [isDownloadOpen, setIsDownloadOpen] = React.useState(false); const [wrapLines, setWrapLines] = useLineWrap(); const downloadLogs = () => { if (!downloadData) return; const blob = new Blob([downloadData], { type: 'text/plain;charset=utf-8', }); saveAs(blob, `${taskName || `task-run-${uuidv4()}`}.log`); }; const startDownloadAll = () => { setDownloadAllStatus(true); onDownloadAll?.() .then(() => { setDownloadAllStatus(false); }) .catch((err: Error) => { setDownloadAllStatus(false); logger.warn(err.message || 'Error downloading logs.'); }); }; // Use containerRef to measure actual height for VirtualizedLogViewer const { containerRef, containerHeight } = useContainerHeight(); const allLines = React.useMemo( () => normalizedSections.flatMap((s) => s.lines), [normalizedSections], ); const { scrollToRow: searchScrollToRow, searchText, setSearchText, currentMatch, matchCount, currentMatchIndex, nextMatch, prevMatch, } = useLogSearch(allLines); const scrollToRow = searchScrollToRow || (autoScroll ? allLines.length : 0); const childRef = React.useRef(null); const [sectionsExpanded, setSectionsExpanded] = React.useState(null); const handleOnToggleAllSections = () => { childRef?.current?.toggleAllSections(); }; return (
{/* Toolbar */}
{showSearch && ( 0 ? 'logs-ready' : 'logs-empty'} value={searchText} onChange={(_event, value) => setSearchText(value)} resultsCount={ matchCount > 0 ? `${currentMatchIndex + 1}/${matchCount}` : undefined } onNextClick={nextMatch} onPreviousClick={prevMatch} onClear={() => setSearchText('')} placeholder="Search" name="logViewerSearchInput" aria-label="Search logs" /> )} {!!allowExpandAllSections && ( )} setLogTheme(logTheme === 'dark' ? 'light' : 'dark')} /> setWrapLines(!wrapLines)} /> setIsDownloadOpen(false)} onOpenChange={setIsDownloadOpen} toggle={(toggleRef) => ( setIsDownloadOpen(!isDownloadOpen)} isExpanded={isDownloadOpen} aria-label="Download logs" data-test="download-logs-toggle" > )} > Download {onDownloadAll && ( {downloadAllLabel} {downloadAllStatus && } )} {fullscreenToggle && isFullscreenSupported && ( <>
{/* Header */} {(taskName || isLoading) && ( {taskName && ( )} {isLoading && ( )} )} {errorMessage && } {/* Log Viewer */}
{containerHeight && (
)}
{/* Footer */} {showResumeStreamButton && (
)}
); }; export default LogViewer;