import { cx } from '@emotion/css'; import memoizeOne from 'memoize-one'; import React, { PureComponent } from 'react'; import Highlighter from 'react-highlight-words'; import { CoreApp, findHighlightChunksInText, LogRowModel } from '@grafana/data'; import { ClipboardButton, IconButton } from '@grafana/ui'; import { LogMessageAnsi } from './LogMessageAnsi'; import { LogRowStyles } from './getLogRowStyles'; export const MAX_CHARACTERS = 100000; interface Props { row: LogRowModel; wrapLogMessage: boolean; prettifyLogMessage: boolean; app?: CoreApp; showContextToggle?: (row?: LogRowModel) => boolean; onOpenContext: (row: LogRowModel) => void; onPermalinkClick?: (row: LogRowModel) => Promise; styles: LogRowStyles; } function renderLogMessage( hasAnsi: boolean, entry: string, highlights: string[] | undefined, highlightClassName: string ) { const needsHighlighter = highlights && highlights.length > 0 && highlights[0] && highlights[0].length > 0 && entry.length < MAX_CHARACTERS; const searchWords = highlights ?? []; if (hasAnsi) { const highlight = needsHighlighter ? { searchWords, highlightClassName } : undefined; return ; } else if (needsHighlighter) { return ( ); } else { return entry; } } const restructureLog = memoizeOne((line: string, prettifyLogMessage: boolean): string => { if (prettifyLogMessage) { try { return JSON.stringify(JSON.parse(line), undefined, 2); } catch (error) { return line; } } return line; }); export class LogRowMessage extends PureComponent { onShowContextClick = (e: React.SyntheticEvent) => { const { onOpenContext } = this.props; e.stopPropagation(); onOpenContext(this.props.row); }; onLogRowClick = (e: React.SyntheticEvent) => { e.stopPropagation(); }; getLogText = () => { const { row, prettifyLogMessage } = this.props; const { raw } = row; return restructureLog(raw, prettifyLogMessage); }; render() { const { row, wrapLogMessage, prettifyLogMessage, showContextToggle, styles, onPermalinkClick } = this.props; const { hasAnsi, raw } = row; const restructuredEntry = restructureLog(raw, prettifyLogMessage); const shouldShowContextToggle = showContextToggle ? showContextToggle(row) : false; return ( <> { // When context is open, the position has to be NOT relative. // Setting the postion as inline-style to // overwrite the more sepecific style definition from `styles.logsRowMessage`. }
{shouldShowContextToggle && ( )} {onPermalinkClick && row.uid && ( onPermalinkClick(row)} /> )} ); } }