import { styled, Box } from '@mui/material' import type { ReactNode } from 'react' import type { SearchState } from '@/components/base' import { buildRegex } from '@/utils/search-matcher' const Item = styled(Box)(({ theme: { palette, typography } }) => ({ padding: '8px 0', margin: '0 12px', lineHeight: 1.35, borderBottom: `1px solid ${palette.divider}`, fontSize: '0.875rem', fontFamily: typography.fontFamily, userSelect: 'text', '& .time': { color: palette.text.secondary, }, '& .type': { display: 'inline-block', marginLeft: 8, textAlign: 'center', borderRadius: 2, textTransform: 'uppercase', fontWeight: '600', }, '& .type[data-type="error"], & .type[data-type="err"]': { color: palette.error.main, }, '& .type[data-type="warning"], & .type[data-type="warn"]': { color: palette.warning.main, }, '& .type[data-type="info"], & .type[data-type="inf"]': { color: palette.info.main, }, '& .data': { color: palette.text.primary, overflowWrap: 'anywhere', }, '& .highlight': { backgroundColor: palette.mode === 'dark' ? '#ffeb3b40' : '#ffeb3b90', borderRadius: 2, padding: '0 2px', }, })) interface Props { value: ILogItem searchState?: SearchState } // Compiling per row multiplied by high-frequency log appends; searchState is // React state, so identity reuse is safe until the search inputs change. let highlightRegexCache: { state: SearchState; regex: RegExp | null } | null = null const getHighlightRegex = (searchState: SearchState): RegExp | null => { if (highlightRegexCache?.state === searchState) return highlightRegexCache.regex const searchText = searchState.text let pattern: string if (searchState.useRegularExpression) { try { new RegExp(searchText) pattern = searchText } catch { pattern = searchText.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') } } else { const escaped = searchText.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') pattern = searchState.matchWholeWord ? `\\b${escaped}\\b` : escaped } const flags = searchState.matchCase ? 'g' : 'gi' const regex = buildRegex(pattern, flags) highlightRegexCache = { state: searchState, regex } return regex } const LogItem = ({ value, searchState }: Props) => { const renderHighlightText = (text: string) => { if (!searchState?.text.trim()) return text const regex = getHighlightRegex(searchState) if (!regex) return text const elements: ReactNode[] = [] let lastIndex = 0 let match: RegExpExecArray | null while ((match = regex.exec(text)) !== null) { const start = match.index const matchText = match[0] if (matchText === '') { regex.lastIndex += 1 continue } if (start > lastIndex) { elements.push(text.slice(lastIndex, start)) } elements.push( {matchText} , ) lastIndex = start + matchText.length } if (lastIndex < text.length) { elements.push(text.slice(lastIndex)) } return elements.length ? elements : text } return (
{renderHighlightText(value.time || '')} {renderHighlightText(value.type)}
{renderHighlightText(value.payload)}
) } export default LogItem