fix: Log viewer json viewer fixes (#377)

This commit is contained in:
Ernest Iliiasov 2024-04-19 12:19:42 -07:00 committed by GitHub
parent ab96e7c002
commit 05517dce09
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 29 additions and 17 deletions

View file

@ -0,0 +1,5 @@
---
'@hyperdx/app': patch
---
LogViewer: better JSON parsing and other tweaks

View file

@ -1375,7 +1375,7 @@ function PropertySubpanel({
{
normallyExpanded: true,
tabulate: true,
lineWrap: true,
lineWrap: false,
useLegacyViewer: false,
},
);
@ -1623,7 +1623,7 @@ function PropertySubpanel({
}}
/>
)}
<Menu width={240}>
<Menu width={240} withinPortal={false}>
<Menu.Target>
<ActionIcon size="md" variant="filled" color="gray">
<i className="bi bi-gear" />

View file

@ -124,28 +124,35 @@ const Line = React.memo(
// mounting it for potentially hundreds of lines
const { ref, hovered } = useHover<HTMLDivElement>();
const isStringValueJsonLike = React.useMemo(() => {
const isStringValueValidJson = React.useMemo(() => {
if (!isString(value)) return false;
return (
(value.startsWith('{') && value.endsWith('}')) ||
(value.startsWith('[') && value.endsWith(']'))
);
try {
if (
(value.startsWith('{') && value.endsWith('}')) ||
(value.startsWith('[') && value.endsWith(']'))
) {
const parsed = JSON.parse(value);
return !!parsed;
}
} catch (e) {
return false;
}
}, [value]);
const [isExpanded, setIsExpanded] = React.useState(
normallyExpanded && !isStringValueJsonLike,
normallyExpanded && !isStringValueValidJson,
);
React.useEffect(() => {
setIsExpanded(normallyExpanded && !isStringValueJsonLike);
}, [isStringValueJsonLike, normallyExpanded]);
setIsExpanded(normallyExpanded && !isStringValueValidJson);
}, [isStringValueValidJson, normallyExpanded]);
const isExpandable = React.useMemo(
() =>
(isPlainObject(value) && Object.keys(value).length > 0) ||
(isArray(value) && value.length > 0) ||
isStringValueJsonLike,
[isStringValueJsonLike, value],
isStringValueValidJson,
[isStringValueValidJson, value],
);
const handleToggle = React.useCallback(() => {
@ -154,7 +161,7 @@ const Line = React.memo(
}, [isExpandable]);
const expandedData = React.useMemo(() => {
if (isStringValueJsonLike) {
if (isStringValueValidJson) {
try {
return JSON.parse(value);
} catch (e) {
@ -162,7 +169,7 @@ const Line = React.memo(
}
}
return value;
}, [isStringValueJsonLike, value]);
}, [isStringValueValidJson, value]);
const nestedLevel = parentKeyPath.length;
const keyPath = React.useMemo(
@ -198,13 +205,13 @@ const Line = React.memo(
</div>
</div>
<div className={styles.valueContainer}>
{isStringValueJsonLike ? (
{isStringValueValidJson ? (
isExpanded ? (
<div className={styles.object}>{'{}'} Parsed JSON</div>
) : (
<>
<ValueRenderer value={value} />
<div className={styles.jsonBtn}>Looks like JSON. Parse?</div>
<div className={styles.jsonBtn}>Expand JSON</div>
</>
)
) : (
@ -219,7 +226,7 @@ const Line = React.memo(
<TreeNode
data={expandedData}
keyPath={keyPath}
disableMenu={isStringValueJsonLike}
disableMenu={isStringValueValidJson}
/>
)}
</>