1
1
import { ExpandCircleDown } from '@mui/icons-material' ;
2
2
import { Button , CircularProgress } from '@mui/material' ;
3
- import { useEffect , useRef , useCallback , useReducer , memo , useState } from 'react' ;
3
+ import {
4
+ useEffect ,
5
+ useRef ,
6
+ useCallback ,
7
+ useReducer ,
8
+ memo ,
9
+ useState
10
+ } from 'react' ;
4
11
import { ErrorBoundary } from 'react-error-boundary' ;
5
12
6
13
import { FlexBox } from '@/components/FlexBox' ;
@@ -34,14 +41,14 @@ const initialSearchState: SearchState = {
34
41
query : '' ,
35
42
elements : [ ] ,
36
43
currentIndex : 0 ,
37
- isSearching : false ,
44
+ isSearching : false
38
45
} ;
39
46
40
47
function searchReducer ( state : SearchState , action : SearchAction ) : SearchState {
41
48
switch ( action . type ) {
42
49
case 'SET_QUERY' :
43
- return {
44
- ...state ,
50
+ return {
51
+ ...state ,
45
52
query : action . payload ,
46
53
isSearching : ! ! action . payload
47
54
} ;
@@ -71,7 +78,9 @@ const SystemLogs = memo(({ serviceName }: { serviceName?: ServiceNames }) => {
71
78
const showScrollDownButton = useBoolState ( false ) ;
72
79
const [ searchState , dispatch ] = useReducer ( searchReducer , initialSearchState ) ;
73
80
const isInitialLoad = useRef ( true ) ;
74
- const [ currentMatchLineIndex , setCurrentMatchLineIndex ] = useState < number | null > ( null ) ;
81
+ const [ currentMatchLineIndex , setCurrentMatchLineIndex ] = useState <
82
+ number | null
83
+ > ( null ) ;
75
84
76
85
useEffect ( ( ) => {
77
86
if ( ! searchState . query || searchState . query . length < 3 ) {
@@ -85,7 +94,9 @@ const SystemLogs = memo(({ serviceName }: { serviceName?: ServiceNames }) => {
85
94
const timer = setTimeout ( ( ) => {
86
95
requestAnimationFrame ( ( ) => {
87
96
const elements = Array . from (
88
- containerRef . current ?. querySelectorAll ( 'span[data-highlighted="true"]' ) ?? [ ]
97
+ containerRef . current ?. querySelectorAll (
98
+ 'span[data-highlighted="true"]'
99
+ ) ?? [ ]
89
100
) as HTMLElement [ ] ;
90
101
91
102
dispatch ( { type : 'SET_MATCHES' , payload : elements } ) ;
@@ -108,7 +119,8 @@ const SystemLogs = memo(({ serviceName }: { serviceName?: ServiceNames }) => {
108
119
dispatch ( { type : 'SET_QUERY' , payload : query } ) ;
109
120
} , [ ] ) ;
110
121
111
- const handleNavigate = useCallback ( ( direction : 'prev' | 'next' ) => {
122
+ const handleNavigate = useCallback (
123
+ ( direction : 'prev' | 'next' ) => {
112
124
const { elements, currentIndex, isSearching } = searchState ;
113
125
const total = elements . length ;
114
126
@@ -119,7 +131,9 @@ const SystemLogs = memo(({ serviceName }: { serviceName?: ServiceNames }) => {
119
131
: ( currentIndex > 1 ? currentIndex - 1 : total ) ;
120
132
121
133
dispatch ( { type : 'SET_CURRENT_INDEX' , payload : newIndex } ) ;
122
- } , [ searchState ] ) ;
134
+ } ,
135
+ [ searchState ]
136
+ ) ;
123
137
124
138
useEffect ( ( ) => {
125
139
const { currentIndex, elements } = searchState ;
@@ -137,7 +151,10 @@ const SystemLogs = memo(({ serviceName }: { serviceName?: ServiceNames }) => {
137
151
}
138
152
139
153
if ( parentElement ) {
140
- const lineIndex = parseInt ( parentElement . getAttribute ( 'data-log-index' ) || '-1' , 10 ) ;
154
+ const lineIndex = parseInt (
155
+ parentElement . getAttribute ( 'data-log-index' ) || '-1' ,
156
+ 10
157
+ ) ;
141
158
setCurrentMatchLineIndex ( lineIndex ) ;
142
159
143
160
requestAnimationFrame ( ( ) => {
@@ -150,7 +167,12 @@ const SystemLogs = memo(({ serviceName }: { serviceName?: ServiceNames }) => {
150
167
} , [ searchState . currentIndex , searchState . elements ] ) ;
151
168
152
169
useEffect ( ( ) => {
153
- if ( ! loading && logs . length && containerRef . current && isInitialLoad . current ) {
170
+ if (
171
+ ! loading &&
172
+ logs . length &&
173
+ containerRef . current &&
174
+ isInitialLoad . current
175
+ ) {
154
176
isInitialLoad . current = false ;
155
177
requestAnimationFrame ( ( ) => {
156
178
if ( containerRef . current ) {
@@ -168,7 +190,9 @@ const SystemLogs = memo(({ serviceName }: { serviceName?: ServiceNames }) => {
168
190
if ( ! container ) return ;
169
191
170
192
const handleScroll = ( ) => {
171
- const isScrolledUp = container . scrollTop < container . scrollHeight - container . clientHeight - 100 ;
193
+ const isScrolledUp =
194
+ container . scrollTop <
195
+ container . scrollHeight - container . clientHeight - 100 ;
172
196
showScrollDownButton . set ( isScrolledUp ) ;
173
197
} ;
174
198
@@ -206,7 +230,10 @@ const SystemLogs = memo(({ serviceName }: { serviceName?: ServiceNames }) => {
206
230
return (
207
231
< ErrorBoundary
208
232
FallbackComponent = { ( { error } : { error : Error } ) => (
209
- < SystemLogsErrorFallback error = { error } serviceName = { serviceName as ServiceNames } />
233
+ < SystemLogsErrorFallback
234
+ error = { error }
235
+ serviceName = { serviceName as ServiceNames }
236
+ />
210
237
) }
211
238
>
212
239
< FlexBox col >
@@ -223,8 +250,8 @@ const SystemLogs = memo(({ serviceName }: { serviceName?: ServiceNames }) => {
223
250
</ FlexBox >
224
251
) : (
225
252
< FlexBox
226
- ref = { containerRef }
227
- col
253
+ ref = { containerRef }
254
+ col
228
255
sx = { { overflowY : 'auto' , maxHeight : '100%' } }
229
256
>
230
257
{ services && renderLogs ( ) }
@@ -243,10 +270,11 @@ const SystemLogs = memo(({ serviceName }: { serviceName?: ServiceNames }) => {
243
270
bottom = { 20 }
244
271
sx = { {
245
272
position : 'fixed' ,
246
- marginLeft : `calc(${ containerRef . current
273
+ marginLeft : `calc(${
274
+ containerRef . current
247
275
? containerRef . current . clientWidth / 2 - 67
248
276
: 0
249
- } px)`
277
+ } px)`
250
278
} }
251
279
>
252
280
< ExpandCircleDown fontSize = "large" color = "secondary" />
0 commit comments