1
1
import { ExpandCircleDown } from '@mui/icons-material' ;
2
2
import { Button , CircularProgress } from '@mui/material' ;
3
- import { useEffect , useRef , useState , useCallback , useLayoutEffect } from 'react' ;
3
+ import { useEffect , useRef , useState , useCallback , useLayoutEffect , memo } from 'react' ;
4
4
import { ErrorBoundary } from 'react-error-boundary' ;
5
5
6
6
import { FlexBox } from '@/components/FlexBox' ;
@@ -16,7 +16,7 @@ import { parseLogLine } from '@/utils/logFormatter';
16
16
17
17
import { MotionBox } from '../../components/MotionComponents' ;
18
18
19
- export const SystemLogs = ( { serviceName } : { serviceName ?: ServiceNames } ) => {
19
+ const SystemLogs = memo ( ( { serviceName } : { serviceName ?: ServiceNames } ) => {
20
20
const { services, loading, logs } = useSystemLogs ( { serviceName } ) ;
21
21
const containerRef = useRef < HTMLDivElement > ( null ) ;
22
22
const showScrollDownButton = useBoolState ( false ) ;
@@ -25,7 +25,7 @@ export const SystemLogs = ({ serviceName }: { serviceName?: ServiceNames }) => {
25
25
const [ totalMatches , setTotalMatches ] = useState ( 0 ) ;
26
26
const [ highlightedElements , setHighlightedElements ] = useState < HTMLElement [ ] > ( [ ] ) ;
27
27
const currentHighlightRef = useRef < HTMLElement | null > ( null ) ;
28
-
28
+ const isInitialLoad = useRef ( true ) ;
29
29
30
30
const scrollToBottom = useCallback ( ( ) => {
31
31
if ( containerRef . current ) {
@@ -103,17 +103,26 @@ export const SystemLogs = ({ serviceName }: { serviceName?: ServiceNames }) => {
103
103
} , [ currentMatch , totalMatches , highlightedElements , updateHighlight ] ) ;
104
104
105
105
useEffect ( ( ) => {
106
- if ( ! loading && logs . length && containerRef . current ) {
107
- // Defer scrolling until after the DOM paints
106
+ if ( ! loading && logs . length && containerRef . current && isInitialLoad . current ) {
107
+ isInitialLoad . current = false ;
108
108
requestAnimationFrame ( ( ) => {
109
109
containerRef . current ?. scrollTo ( {
110
110
top : containerRef . current . scrollHeight ,
111
- behavior : 'auto' ,
111
+ behavior : 'auto'
112
112
} ) ;
113
113
} ) ;
114
114
}
115
115
} , [ loading , logs ] ) ;
116
-
116
+
117
+ const renderLogs = useCallback ( ( ) => {
118
+ return logs . map ( ( log , index ) => {
119
+ const parsedLog = parseLogLine ( log ) ;
120
+ if ( ! parsedLog ) {
121
+ return < PlainLog log = { log } index = { index } key = { index } searchQuery = { searchQuery } /> ;
122
+ }
123
+ return < FormattedLog log = { parsedLog } index = { index } key = { index } searchQuery = { searchQuery } /> ;
124
+ } ) ;
125
+ } , [ logs , searchQuery ] ) ;
117
126
118
127
return (
119
128
< ErrorBoundary
@@ -135,14 +144,7 @@ export const SystemLogs = ({ serviceName }: { serviceName?: ServiceNames }) => {
135
144
</ FlexBox >
136
145
) : (
137
146
< FlexBox ref = { containerRef } col sx = { { overflowY : 'auto' , maxHeight : '100%' } } >
138
- { services &&
139
- logs . map ( ( log , index ) => {
140
- const parsedLog = parseLogLine ( log ) ;
141
- if ( ! parsedLog ) {
142
- return < PlainLog log = { log } index = { index } key = { index } searchQuery = { searchQuery } /> ;
143
- }
144
- return < FormattedLog log = { parsedLog } index = { index } key = { index } searchQuery = { searchQuery } /> ;
145
- } ) }
147
+ { services && renderLogs ( ) }
146
148
</ FlexBox >
147
149
) }
148
150
@@ -170,4 +172,7 @@ export const SystemLogs = ({ serviceName }: { serviceName?: ServiceNames }) => {
170
172
</ FlexBox >
171
173
</ ErrorBoundary >
172
174
) ;
173
- } ;
175
+ } ) ;
176
+
177
+
178
+ export { SystemLogs } ;
0 commit comments