-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathLogViewer.tsx
More file actions
330 lines (308 loc) · 11.6 KB
/
LogViewer.tsx
File metadata and controls
330 lines (308 loc) · 11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
import React, { useState, useEffect, memo } from 'react';
import { LogViewerContext, LogViewerToolbarContext } from './LogViewerContext';
import { css } from '@patternfly/react-styles';
import { LogViewerRow } from './LogViewerRow';
import { parseConsoleOutput, searchedKeyWordType, stripAnsi } from './utils/utils';
import { VariableSizeList as List, areEqual } from '../react-window';
import styles from '@patternfly/react-styles/css/components/LogViewer/log-viewer';
import AnsiUp from '../ansi_up/ansi_up';
interface LogViewerProps {
/** String or String Array data being sent by the consumer*/
data?: string | string[];
/** Consumer may turn off the visibility on the toolbar */
hasToolbar?: boolean;
/** Flag to enable or disable line numbers on the log viewer. */
hasLineNumbers?: boolean;
/** Width of the log viewer. */
width?: number | string;
/** Height of the log viewer. */
height?: number | string;
/** Rows rendered outside of view. The more rows are rendered, the higher impact on performance */
overScanCount?: number;
/** Toolbar rendered in the log viewer header */
toolbar?: React.ReactNode;
/** Content displayed while the log viewer is loading */
loadingContent?: React.ReactNode;
/** Flag indicating that log viewer is dark themed */
theme?: 'dark' | 'light';
/** Row index to scroll to */
scrollToRow?: number;
/** The width of index when the line numbers is shown, set by char numbers */
initialIndexWidth?: number;
/** Number of rows to display in the log viewer */
itemCount?: number;
/** Flag indicating that log viewer is wrapping text or not */
isTextWrapped?: boolean;
/** Component rendered in the log viewer console window header */
header?: React.ReactNode;
/** Component rendered in the log viewer console window footer */
footer?: React.ReactNode;
/** Callback function when scrolling the window.
* scrollDirection is the direction of scroll, could be 'forward'|'backward'.
* scrollOffset and scrollOffsetToBottom are the offset of the current position to the top or the bottom.
* scrollUpdateWasRequested is false when the scroll event is cause by the user interaction in the browser, else it's true.
* @example onScroll={({scrollDirection, scrollOffset, scrollOffsetToBottom, scrollUpdateWasRequested})=>{}}
*/
onScroll?: ({
scrollDirection,
scrollOffset,
scrollOffsetToBottom,
scrollUpdateWasRequested
}: {
scrollDirection: 'forward' | 'backward';
scrollOffset: number;
scrollOffsetToBottom: number;
scrollUpdateWasRequested: boolean;
}) => void;
/** Forwarded ref */
innerRef?: React.RefObject<any>;
}
let canvas: HTMLCanvasElement | undefined;
const getCharNums = (windowWidth: number, font: string) => {
// if given, use cached canvas for better performance
// else, create new canvas
canvas = canvas || document.createElement('canvas');
const context = canvas.getContext('2d');
context.font = font;
const oneChar = context.measureText('a');
return Math.floor(windowWidth / oneChar.width);
};
const LogViewerBase: React.FunctionComponent<LogViewerProps> = memo(
({
data = '',
hasLineNumbers = true,
height = 600,
overScanCount = 10,
loadingContent = '',
toolbar,
width,
theme = 'light',
scrollToRow = 0,
itemCount = undefined,
header,
footer,
onScroll,
innerRef,
isTextWrapped = true,
initialIndexWidth,
...props
}: LogViewerProps) => {
const [searchedInput, setSearchedInput] = useState<string | null>('');
const [rowInFocus, setRowInFocus] = useState<searchedKeyWordType | null>({ rowIndex: scrollToRow, matchIndex: 0 });
const [searchedWordIndexes, setSearchedWordIndexes] = useState<searchedKeyWordType[] | null>([]);
const [currentSearchedItemCount, setCurrentSearchedItemCount] = useState<number>(0);
const [lineHeight, setLineHeight] = useState<number>(0);
const [charNumsPerLine, setCharNumsPerLine] = useState<number>(0);
const [indexWidth, setIndexWidth] = useState<number>(0);
const [resizing, setResizing] = useState(false);
const [loading, setLoading] = useState(true);
const [listKey, setListKey] = useState(1);
const [, setEmptyState] = useState({});
/* Parse data every time it changes */
const parsedData = React.useMemo(() => parseConsoleOutput(data), [data]);
const ansiUp = new AnsiUp();
const ref = React.useRef<any>();
const logViewerRef = innerRef || ref;
const containerRef = React.useRef<any>();
let resizeTimer = null as any;
useEffect(() => {
let observer : ResizeObserver | undefined = undefined;
if (containerRef && containerRef.current) {
window.addEventListener('resize', callbackResize);
observer = new ResizeObserver((event) => {
setEmptyState({});
});
observer.observe(containerRef.current);
setLoading(false);
createDummyElements();
ansiUp.resetStyles();
}
return () => {
window.removeEventListener('resize', callbackResize);
observer?.disconnect();
}
}, [containerRef.current]);
const callbackResize = (event : UIEvent) => {
if (event.type === 'scroll') {
return;
}
if (!resizing) {
setResizing(true);
}
if (resizeTimer) {
clearTimeout(resizeTimer);
}
resizeTimer = setTimeout(() => {
setResizing(false);
createDummyElements();
}, 100);
};
useEffect(() => {
setLoading(resizing);
}, [resizing]);
const dataToRender = React.useMemo(
() => ({
parsedData,
logViewerRef,
rowInFocus,
searchedWordIndexes
}),
[parsedData, logViewerRef, rowInFocus, searchedWordIndexes]
);
useEffect(() => {
if (logViewerRef && logViewerRef.current) {
logViewerRef.current.resetAfterIndex(0);
}
}, [parsedData]);
useEffect(() => {
if (scrollToRow && parsedData.length) {
setRowInFocus({ rowIndex: scrollToRow, matchIndex: 0 });
// only in this way (setTimeout) the scrollToItem will work
setTimeout(() => {
if (logViewerRef && logViewerRef.current) {
logViewerRef.current.scrollToItem(scrollToRow, 'center');
}
}, 1);
}
}, [parsedData, scrollToRow]);
const createDummyElements = () => {
if (!(containerRef && containerRef.current)) {
return;
}
// create dummy elements
const dummyIndex = document.createElement('span');
dummyIndex.className = css(styles.logViewerIndex);
const dummyText = document.createElement('span');
dummyText.className = css(styles.logViewerText);
const dummyListItem = document.createElement('div');
dummyListItem.className = css(styles.logViewerListItem);
const dummyList = document.createElement('div');
dummyList.className = css(styles.logViewerList);
// append dummy elements
dummyListItem.appendChild(dummyIndex);
dummyListItem.appendChild(dummyText);
dummyList.appendChild(dummyListItem);
containerRef.current.appendChild(dummyList);
// compute styles
const dummyIndexStyles = getComputedStyle(dummyIndex);
const dummyTextStyles = getComputedStyle(dummyText);
setLineHeight(parseFloat(dummyTextStyles.lineHeight));
const lineWidth = hasLineNumbers
? (containerRef.current as HTMLDivElement).clientWidth -
(parseFloat(dummyTextStyles.paddingLeft) +
parseFloat(dummyTextStyles.paddingRight) +
parseFloat(dummyIndexStyles.width))
: (containerRef.current as HTMLDivElement).clientWidth -
(parseFloat(dummyTextStyles.paddingLeft) + parseFloat(dummyTextStyles.paddingRight));
const charNumsPerLine = getCharNums(
lineWidth,
`${dummyTextStyles.fontWeight} ${dummyTextStyles.fontSize} ${dummyTextStyles.fontFamily}`
);
setCharNumsPerLine(charNumsPerLine);
setIndexWidth(parseFloat(dummyIndexStyles.width));
// remove dummy elements from the DOM tree
containerRef.current.removeChild(dummyList);
setListKey(listKey => listKey + 1);
};
const scrollToRowInFocus = (searchedRowIndex: searchedKeyWordType) => {
setRowInFocus(searchedRowIndex);
logViewerRef.current.scrollToItem(searchedRowIndex.rowIndex, 'center');
// use this method to scroll to the right
// if the keyword is out of the window when wrapping text
if (!isTextWrapped) {
setTimeout(() => {
const element = containerRef.current.querySelector('.pf-v5-c-log-viewer__string.pf-m-current');
element && element.scrollIntoView({ block: 'nearest', inline: 'center' });
}, 1);
}
};
useEffect(() => {
setListKey(listKey => listKey + 1);
}, [isTextWrapped]);
const guessRowHeight = (rowIndex: number) => {
if (!isTextWrapped) {
return lineHeight;
}
// strip ansi escape code before estimate the row height
const rowText = stripAnsi(parsedData[rowIndex]);
// get the row numbers of the current text
const numRows = Math.ceil(rowText.length / charNumsPerLine);
// multiply by line height to get the total height
return lineHeight * (numRows || 1);
};
const createList = (parsedData: string[]) => (
<List
key={listKey}
outerClassName={css(styles.logViewerScrollContainer)}
innerClassName={css(styles.logViewerList)}
height={containerRef.current.clientHeight}
width={containerRef.current.clientWidth}
itemSize={guessRowHeight}
itemCount={typeof itemCount === 'undefined' ? parsedData.length : itemCount}
itemData={dataToRender}
ref={logViewerRef}
overscanCount={overScanCount}
onScroll={onScroll}
isTextWrapped={isTextWrapped}
hasLineNumbers={hasLineNumbers}
indexWidth={indexWidth}
ansiUp={ansiUp}
>
{LogViewerRow}
</List>
);
return (
<LogViewerContext.Provider
value={{
parsedData,
searchedInput
}}
>
<div
className={css(
styles.logViewer,
hasLineNumbers && styles.modifiers.lineNumbers,
!isTextWrapped && styles.modifiers.nowrap,
initialIndexWidth && styles.modifiers.lineNumberChars,
theme === 'dark' && styles.modifiers.dark
)}
{...(initialIndexWidth && {
style: {
'--pf-v5-c-log-viewer--line-number-chars': initialIndexWidth + 1
} as React.CSSProperties
})}
{...props}
>
{toolbar && (
<LogViewerToolbarContext.Provider
value={{
itemCount,
searchedInput,
rowInFocus,
searchedWordIndexes,
currentSearchedItemCount,
scrollToRow: scrollToRowInFocus,
setRowInFocus,
setSearchedInput,
setSearchedWordIndexes,
setCurrentSearchedItemCount
}}
>
<div className={css(styles.logViewerHeader)}>{toolbar}</div>
</LogViewerToolbarContext.Provider>
)}
{header}
<div className={css(styles.logViewerMain)} style={{ height, width }} ref={containerRef}>
{loading ? <div>{loadingContent}</div> : createList(parsedData)}
</div>
{footer}
</div>
</LogViewerContext.Provider>
);
},
areEqual
);
export const LogViewer = React.forwardRef((props: LogViewerProps, ref: React.Ref<any>) => (
<LogViewerBase innerRef={ref as React.MutableRefObject<any>} {...props} />
));
LogViewer.displayName = 'LogViewer';