feat: add logger highlighting

This commit is contained in:
huzibaca 2024-11-19 03:29:44 +08:00
parent 1ef2b1aaf1
commit a7020fd46c
2 changed files with 36 additions and 5 deletions

View File

@ -32,14 +32,37 @@ const Item = styled(Box)(({ theme: { palette, typography } }) => ({
color: palette.text.primary, color: palette.text.primary,
overflowWrap: "anywhere", overflowWrap: "anywhere",
}, },
"& .highlight": {
backgroundColor: palette.mode === "dark" ? "#ffeb3b40" : "#ffeb3b90",
borderRadius: 2,
padding: "0 2px",
},
})); }));
interface Props { interface Props {
value: ILogItem; value: ILogItem;
searchText?: string;
} }
const LogItem = (props: Props) => { const LogItem = ({ value, searchText }: Props) => {
const { value } = 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() ? (
<span key={index} className="highlight">
{part}
</span>
) : (
part
),
);
} catch {
return text;
}
};
return ( return (
<Item> <Item>
@ -50,7 +73,7 @@ const LogItem = (props: Props) => {
</span> </span>
</div> </div>
<div> <div>
<span className="data">{value.payload}</span> <span className="data">{renderHighlightText(value.payload)}</span>
</div> </div>
</Item> </Item>
); );

View File

@ -27,6 +27,7 @@ const LogPage = () => {
); );
const [match, setMatch] = useState(() => (_: string) => true); const [match, setMatch] = useState(() => (_: string) => true);
const logData = useLogData(logLevel); const logData = useLogData(logLevel);
const [searchText, setSearchText] = useState("");
const filterLogs = useMemo(() => { const filterLogs = useMemo(() => {
return logData return logData
@ -92,7 +93,12 @@ const LogPage = () => {
<MenuItem value="error">ERROR</MenuItem> <MenuItem value="error">ERROR</MenuItem>
<MenuItem value="debug">DEBUG</MenuItem> <MenuItem value="debug">DEBUG</MenuItem>
</BaseStyledSelect> </BaseStyledSelect>
<BaseSearchBox onSearch={(match) => setMatch(() => match)} /> <BaseSearchBox
onSearch={(matcher, state) => {
setMatch(() => matcher);
setSearchText(state.text);
}}
/>
</Box> </Box>
<Box <Box
@ -107,7 +113,9 @@ const LogPage = () => {
<Virtuoso <Virtuoso
initialTopMostItemIndex={999} initialTopMostItemIndex={999}
data={filterLogs} data={filterLogs}
itemContent={(index, item) => <LogItem value={item} />} itemContent={(index, item) => (
<LogItem value={item} searchText={searchText} />
)}
followOutput={"smooth"} followOutput={"smooth"}
/> />
) : ( ) : (