Skip to content

Commit

Permalink
chore(web): optimize log display logic (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
HUAHUAI23 authored Jun 19, 2024
1 parent e80389f commit 09114b0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
22 changes: 13 additions & 9 deletions web/src/pages/app/mods/StatusBar/LogsModal/InitLog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@ export default function InitLog() {
}, [currentApp.appid]);

useEffect(() => {
setRowCount(0);
setPaused(false);
setLogs([]);
setRowCount(0);
setIsLoading(true);
setPaused(false);
const ctrl = fetchLogs();

return () => {
Expand All @@ -96,11 +96,15 @@ export default function InitLog() {
}, [fetchLogs]);

useEffect(() => {
const sortedLogs = logs.sort((a, b) => parseInt(a.id) - parseInt(b.id));
const concatenatedLogs = sortedLogs.map((log) => log.data).join("");
setRenderLogs(concatenatedLogs);
const totalRows = concatenatedLogs.split("\n").length;
setRowCount(totalRows);
if (logs.length === 0) return;

const sortedLogs = [...logs].sort((a, b) => parseInt(a.id) - parseInt(b.id));
const logLines = sortedLogs.flatMap((log) => log.data.split("\n"));
const filteredLogLines = logLines.filter((line) => line.trim() !== "");
const uniqueLogLines = Array.from(new Set(filteredLogLines));

setRenderLogs(uniqueLogLines.join("\n"));
setRowCount(uniqueLogLines.length);
}, [logs]);

return (
Expand All @@ -123,10 +127,10 @@ export default function InitLog() {
<LogViewer
data={renderLogs}
hasLineNumbers={false}
scrollToRow={paused ? undefined : rowCount + 300}
scrollToRow={paused ? undefined : rowCount + 1}
height={"100%"}
onScroll={(e) => {
if (e.scrollOffsetToBottom <= 0) {
if (e.scrollOffsetToBottom <= 5) {
setPaused(false);
return;
}
Expand Down
11 changes: 6 additions & 5 deletions web/src/pages/app/mods/StatusBar/LogsModal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,10 @@ export default function LogsModal(props: { children: React.ReactElement }) {

useEffect(() => {
if (!isOpen) return;
setRowCount(0);
setPaused(false);
setLogs([]);
setRowCount(0);
setIsLoading(true);
setPaused(false);
const ctrl = fetchLogs();

return () => {
Expand All @@ -170,7 +170,8 @@ export default function LogsModal(props: { children: React.ReactElement }) {
}, [podName, containerName, isOpen, refresh, fetchLogs]);

useEffect(() => {
const sortedLogs = logs.sort((a, b) => parseInt(a.id) - parseInt(b.id));
if (logs.length === 0) return;
const sortedLogs = [...logs].sort((a, b) => parseInt(a.id) - parseInt(b.id));
const concatenatedLogs = sortedLogs.map((log) => log.data).join("");
setRenderLogs(concatenatedLogs);
const totalRows = concatenatedLogs.split("\n").length;
Expand Down Expand Up @@ -262,10 +263,10 @@ export default function LogsModal(props: { children: React.ReactElement }) {
<LogViewer
data={renderLogs}
hasLineNumbers={true}
scrollToRow={paused ? undefined : rowCount + 300}
scrollToRow={paused ? undefined : rowCount + 1}
height={"98%"}
onScroll={(e) => {
if (e.scrollOffsetToBottom <= 0) {
if (e.scrollOffsetToBottom <= 5) {
setPaused(false);
return;
}
Expand Down

0 comments on commit 09114b0

Please sign in to comment.