json data is now collapsible!

This commit is contained in:
muzam1l
2022-03-03 21:03:28 +05:30
parent c521246393
commit c9c818c8f3
5 changed files with 36 additions and 13 deletions

View File

@@ -42,6 +42,8 @@ const DebugStream = () => {
message, message,
timestamp, timestamp,
jsonData: jsonData?.result, jsonData: jsonData?.result,
linkText: 'Expand',
defaultCollapsed: true
}; };
}, []); }, []);

View File

@@ -3,6 +3,7 @@ import { styled } from "../stitches.config";
const StyledLink = styled("a", { const StyledLink = styled("a", {
color: "CurrentColor", color: "CurrentColor",
textDecoration: "underline", textDecoration: "underline",
cursor: 'pointer'
}); });
export default StyledLink; export default StyledLink;

View File

@@ -1,4 +1,4 @@
import React, { useRef, useLayoutEffect, ReactNode } from "react"; import { useRef, useLayoutEffect, ReactNode, FC, useState } from "react";
import { Notepad, Prohibit } from "phosphor-react"; import { Notepad, Prohibit } from "phosphor-react";
import useStayScrolled from "react-stay-scrolled"; import useStayScrolled from "react-stay-scrolled";
import NextLink from "next/link"; import NextLink from "next/link";
@@ -16,7 +16,7 @@ interface ILogBox {
enhanced?: boolean; enhanced?: boolean;
} }
const LogBox: React.FC<ILogBox> = ({ title, clearLog, logs, children, renderNav, enhanced }) => { const LogBox: FC<ILogBox> = ({ title, clearLog, logs, children, renderNav, enhanced }) => {
const logRef = useRef<HTMLPreElement>(null); const logRef = useRef<HTMLPreElement>(null);
const { stayScrolled /*, scrollBottom*/ } = useStayScrolled(logRef); const { stayScrolled /*, scrollBottom*/ } = useStayScrolled(logRef);
@@ -117,16 +117,7 @@ const LogBox: React.FC<ILogBox> = ({ title, clearLog, logs, children, renderNav,
p: enhanced ? "$1" : undefined, p: enhanced ? "$1" : undefined,
}} }}
> >
<LogText variant={log.type}> <Log {...log} />
{log.timestamp && <Text muted>{log.timestamp.toLocaleTimeString()} </Text>}
{log.message}{" "}
{log.link && (
<NextLink href={log.link} shallow passHref>
<Link as="a">{log.linkText}</Link>
</NextLink>
)}
{log.jsonData && <Code>{JSON.stringify(log.jsonData, null, 2)}</Code>}
</LogText>
</Box> </Box>
))} ))}
{children} {children}
@@ -136,4 +127,33 @@ const LogBox: React.FC<ILogBox> = ({ title, clearLog, logs, children, renderNav,
); );
}; };
export const Log: FC<ILog> = ({
type,
timestamp,
message,
link,
linkText,
defaultCollapsed,
jsonData,
}) => {
const [expanded, setExpanded] = useState(!defaultCollapsed);
return (
<LogText variant={type}>
{timestamp && <Text muted>{timestamp.toLocaleTimeString()} </Text>}
{message}{" "}
{link && (
<NextLink href={link} shallow passHref>
<Link as="a">{linkText}</Link>
</NextLink>
)}
{jsonData && (
<Link onClick={() => setExpanded(!expanded)} as="a">
{expanded ? "Collapse" : "Expand"}
</Link>
)}
{expanded && jsonData && <Code>{JSON.stringify(jsonData, null, 2)}</Code>}
</LogText>
);
};
export default LogBox; export default LogBox;

View File

@@ -5,7 +5,6 @@ const Text = styled("span", {
lineHeight: "$body", lineHeight: "$body",
color: "$text", color: "$text",
wordWrap: "break-word", wordWrap: "break-word",
whiteSpace: 'pre-wrap',
variants: { variants: {
variant: { variant: {
log: { log: {

View File

@@ -37,6 +37,7 @@ export interface ILog {
timestamp?: Date; timestamp?: Date;
link?: string; link?: string;
linkText?: string; linkText?: string;
defaultCollapsed?: boolean
} }
export interface ISelect<T = string> { export interface ISelect<T = string> {