Skip to content

Commit e2e5018

Browse files
committed
Added memo to prevent re-render in the list and optimise FPS drops and performance
1 parent 901cd26 commit e2e5018

File tree

2 files changed

+28
-19
lines changed

2 files changed

+28
-19
lines changed

web-server/src/components/Service/SystemLog/LogSearch.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
} from '@mui/material';
1414
import { styled } from '@mui/material/styles';
1515
import debounce from 'lodash/debounce';
16-
import { useState, useCallback, useMemo, useEffect, useRef } from 'react';
16+
import { memo, useState, useCallback, useMemo, useEffect } from 'react';
1717

1818
import { MotionBox } from '@/components/MotionComponents';
1919

@@ -57,7 +57,7 @@ interface LogSearchProps {
5757
totalMatches: number;
5858
}
5959

60-
export const LogSearch = ({
60+
const LogSearch = memo(({
6161
onSearch,
6262
onNavigate,
6363
currentMatch,
@@ -171,5 +171,9 @@ export const LogSearch = ({
171171
)}
172172
</SearchContainer>
173173
);
174-
};
174+
});
175+
176+
LogSearch.displayName = 'LogSearch';
177+
178+
export { LogSearch };
175179

web-server/src/content/Service/SystemLogs.tsx

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { ExpandCircleDown } from '@mui/icons-material';
22
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';
44
import { ErrorBoundary } from 'react-error-boundary';
55

66
import { FlexBox } from '@/components/FlexBox';
@@ -16,7 +16,7 @@ import { parseLogLine } from '@/utils/logFormatter';
1616

1717
import { MotionBox } from '../../components/MotionComponents';
1818

19-
export const SystemLogs = ({ serviceName }: { serviceName?: ServiceNames }) => {
19+
const SystemLogs = memo(({ serviceName }: { serviceName?: ServiceNames }) => {
2020
const { services, loading, logs } = useSystemLogs({ serviceName });
2121
const containerRef = useRef<HTMLDivElement>(null);
2222
const showScrollDownButton = useBoolState(false);
@@ -25,7 +25,7 @@ export const SystemLogs = ({ serviceName }: { serviceName?: ServiceNames }) => {
2525
const [totalMatches, setTotalMatches] = useState(0);
2626
const [highlightedElements, setHighlightedElements] = useState<HTMLElement[]>([]);
2727
const currentHighlightRef = useRef<HTMLElement | null>(null);
28-
28+
const isInitialLoad = useRef(true);
2929

3030
const scrollToBottom = useCallback(() => {
3131
if (containerRef.current) {
@@ -103,17 +103,26 @@ export const SystemLogs = ({ serviceName }: { serviceName?: ServiceNames }) => {
103103
}, [currentMatch, totalMatches, highlightedElements, updateHighlight]);
104104

105105
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;
108108
requestAnimationFrame(() => {
109109
containerRef.current?.scrollTo({
110110
top: containerRef.current.scrollHeight,
111-
behavior: 'auto',
111+
behavior: 'auto'
112112
});
113113
});
114114
}
115115
}, [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]);
117126

118127
return (
119128
<ErrorBoundary
@@ -135,14 +144,7 @@ export const SystemLogs = ({ serviceName }: { serviceName?: ServiceNames }) => {
135144
</FlexBox>
136145
) : (
137146
<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()}
146148
</FlexBox>
147149
)}
148150

@@ -170,4 +172,7 @@ export const SystemLogs = ({ serviceName }: { serviceName?: ServiceNames }) => {
170172
</FlexBox>
171173
</ErrorBoundary>
172174
);
173-
};
175+
});
176+
177+
178+
export { SystemLogs };

0 commit comments

Comments
 (0)