From 181ac914212f2bfd4f831f63559fd968687b62e6 Mon Sep 17 00:00:00 2001 From: Fei Yang <18493471+qianlongzt@users.noreply.github.com> Date: Wed, 30 Oct 2024 17:13:49 +0800 Subject: [PATCH] keep logs (#299) --- src/renderer/src/pages/logs.tsx | 59 ++++++++++++++++++++++++++------- 1 file changed, 47 insertions(+), 12 deletions(-) diff --git a/src/renderer/src/pages/logs.tsx b/src/renderer/src/pages/logs.tsx index e1fb77f..4716e4a 100644 --- a/src/renderer/src/pages/logs.tsx +++ b/src/renderer/src/pages/logs.tsx @@ -4,10 +4,38 @@ import { useEffect, useMemo, useRef, useState } from 'react' import { Button, Divider, Input } from '@nextui-org/react' import { Virtuoso, VirtuosoHandle } from 'react-virtuoso' import { IoLocationSharp } from 'react-icons/io5' +import { CgTrash } from 'react-icons/cg' + import { includesIgnoreCase } from '@renderer/utils/includes' +const cachedLogs: { + log: IMihomoLogInfo[] + trigger: ((i: IMihomoLogInfo[]) => void) | null + clean: () => void +} = { + log: [], + trigger: null, + clean(): void { + this.log = [] + if (this.trigger !== null) { + this.trigger(this.log) + } + } +} + +window.electron.ipcRenderer.on('mihomoLogs', (_e, log: IMihomoLogInfo) => { + log.time = new Date().toLocaleString() + cachedLogs.log.push(log) + if (cachedLogs.log.length >= 500) { + cachedLogs.log.shift() + } + if (cachedLogs.trigger !== null) { + cachedLogs.trigger(cachedLogs.log) + } +}) + const Logs: React.FC = () => { - const [logs, setLogs] = useState([]) + const [logs, setLogs] = useState(cachedLogs.log) const [filter, setFilter] = useState('') const [trace, setTrace] = useState(true) @@ -30,18 +58,12 @@ const Logs: React.FC = () => { }, [filteredLogs, trace]) useEffect(() => { - window.electron.ipcRenderer.on('mihomoLogs', (_e, log: IMihomoLogInfo) => { - log.time = new Date().toLocaleString() - setLogs((prevLogs) => { - if (prevLogs.length >= 500) { - prevLogs.shift() - } - return [...prevLogs, log] - }) - }) - + const old = cachedLogs.trigger + cachedLogs.trigger = (a): void => { + setLogs([...a]) + } return (): void => { - window.electron.ipcRenderer.removeAllListeners('mihomoLogs') + cachedLogs.trigger = old } }, []) @@ -68,6 +90,19 @@ const Logs: React.FC = () => { > +