From a7020fd46c219378df704b192e785bc444527733 Mon Sep 17 00:00:00 2001 From: huzibaca Date: Tue, 19 Nov 2024 03:29:44 +0800 Subject: [PATCH] feat: add logger highlighting --- src/components/log/log-item.tsx | 29 ++++++++++++++++++++++++++--- src/pages/logs.tsx | 12 ++++++++++-- 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/src/components/log/log-item.tsx b/src/components/log/log-item.tsx index 012eb2e3..e7891f9f 100644 --- a/src/components/log/log-item.tsx +++ b/src/components/log/log-item.tsx @@ -32,14 +32,37 @@ const Item = styled(Box)(({ theme: { palette, typography } }) => ({ color: palette.text.primary, overflowWrap: "anywhere", }, + "& .highlight": { + backgroundColor: palette.mode === "dark" ? "#ffeb3b40" : "#ffeb3b90", + borderRadius: 2, + padding: "0 2px", + }, })); interface Props { value: ILogItem; + searchText?: string; } -const LogItem = (props: Props) => { - const { value } = props; +const LogItem = ({ value, searchText }: Props) => { + const renderHighlightText = (text: string) => { + if (!searchText?.trim()) return text; + + try { + const parts = text.split(new RegExp(`(${searchText})`, "gi")); + return parts.map((part, index) => + part.toLowerCase() === searchText.toLowerCase() ? ( + + {part} + + ) : ( + part + ), + ); + } catch { + return text; + } + }; return ( @@ -50,7 +73,7 @@ const LogItem = (props: Props) => {
- {value.payload} + {renderHighlightText(value.payload)}
); diff --git a/src/pages/logs.tsx b/src/pages/logs.tsx index cd783a37..af6f8601 100644 --- a/src/pages/logs.tsx +++ b/src/pages/logs.tsx @@ -27,6 +27,7 @@ const LogPage = () => { ); const [match, setMatch] = useState(() => (_: string) => true); const logData = useLogData(logLevel); + const [searchText, setSearchText] = useState(""); const filterLogs = useMemo(() => { return logData @@ -92,7 +93,12 @@ const LogPage = () => { ERROR DEBUG - setMatch(() => match)} /> + { + setMatch(() => matcher); + setSearchText(state.text); + }} + /> { } + itemContent={(index, item) => ( + + )} followOutput={"smooth"} /> ) : (