From c5b3da72cdfab3f8b4a73b0e8aef2952decfb8c7 Mon Sep 17 00:00:00 2001 From: sylenien Date: Wed, 9 Nov 2022 18:03:37 +0100 Subject: [PATCH] fix(ui): fix very long diffs --- .../components/Session_/Storage/DiffRow.tsx | 52 ++++++++++++++++--- .../components/Session_/Storage/Storage.js | 15 +++--- 2 files changed, 52 insertions(+), 15 deletions(-) diff --git a/frontend/app/components/Session_/Storage/DiffRow.tsx b/frontend/app/components/Session_/Storage/DiffRow.tsx index f34be1b40..3ecb4f615 100644 --- a/frontend/app/components/Session_/Storage/DiffRow.tsx +++ b/frontend/app/components/Session_/Storage/DiffRow.tsx @@ -1,4 +1,5 @@ import React from 'react'; +import cn from 'classnames'; interface Props { shades: Record; @@ -11,7 +12,7 @@ const getCircularReplacer = () => { const seen = new WeakSet(); // @ts-ignore return (key, value) => { - if (typeof value === "object" && value !== null) { + if (typeof value === 'object' && value !== null) { if (seen.has(value)) { return; } @@ -23,20 +24,57 @@ const getCircularReplacer = () => { function DiffRow({ diff, path }: Props) { const [shorten, setShorten] = React.useState(true); + const [shortenOldVal, setShortenOldVal] = React.useState(true); + const [shortenNewVal, setShortenNewVal] = React.useState(true); - const oldValue = diff.item ? JSON.stringify(diff.item.lhs, getCircularReplacer()) : JSON.stringify(diff.lhs, getCircularReplacer()); - const newValue = diff.item ? JSON.stringify(diff.item.rhs, getCircularReplacer()) : JSON.stringify(diff.rhs, getCircularReplacer()); + const oldValue = diff.item + ? JSON.stringify(diff.item.lhs, getCircularReplacer(), 1) + : JSON.stringify(diff.lhs, getCircularReplacer(), 1); + const newValue = diff.item + ? JSON.stringify(diff.item.rhs, getCircularReplacer(), 1) + : JSON.stringify(diff.rhs, getCircularReplacer(), 1); + + const length = path.length; + const diffLengths = [oldValue?.length || 0, newValue?.length || 0]; + + const pathStr = + length > 20 && shorten ? path.slice(0, 5) + '...' + path.slice(length - 10, length) : path; + + const oldValueSafe = + diffLengths[0] > 50 && shortenOldVal + ? `${oldValue.slice(0, 10)} ... ${oldValue.slice(diffLengths[0] - 25, diffLengths[0])}` + : oldValue; + const newValueSafe = + diffLengths[1] > 50 && shortenNewVal + ? `${newValue.slice(0, 10)} ... ${newValue.slice(diffLengths[1] - 25, diffLengths[1])}` + : newValue; - const pathStr = path.length > 15 && shorten ? path.slice(0, 5) + '...' + path.slice(10) : path; return (
- 15 ? 'cursor-pointer' : ''} onClick={() => setShorten(false)}> + 20 ? 'cursor-pointer' : ''} onClick={() => setShorten(!shorten)}> {pathStr} {': '} - {oldValue || 'undefined'} + setShortenOldVal(!shortenOldVal)} + className={cn( + 'line-through text-disabled-text', + diffLengths[0] > 50 ? 'cursor-pointer' : '' + )} + > + {oldValueSafe || 'undefined'} + {' -> '} - {newValue || 'undefined'} + setShortenNewVal(!shortenNewVal)} + className={cn( + 'whitespace-pre', + newValue ? 'text-red' : 'text-green', + diffLengths[1] > 50 ? 'cursor-pointer' : '' + )} + > + {newValueSafe || 'undefined'} +
); } diff --git a/frontend/app/components/Session_/Storage/Storage.js b/frontend/app/components/Session_/Storage/Storage.js index f159ab8ce..22d231fcf 100644 --- a/frontend/app/components/Session_/Storage/Storage.js +++ b/frontend/app/components/Session_/Storage/Storage.js @@ -77,14 +77,14 @@ export default class Storage extends React.PureComponent { if (!stateDiff) { return ( -
+
No diff
); } return ( -
+
{stateDiff.map((d, i) => this.renderDiffs(d, i))}
); @@ -166,13 +166,12 @@ export default class Storage extends React.PureComponent { > {src === null ? (
- {' '} - {name}{' '} + {name}
) : ( <> {this.renderDiff(item, prevItem)} -
+
)} -
+
{typeof item.duration === 'number' && (
{formatMs(item.duration)}
)} @@ -214,11 +213,11 @@ export default class Storage extends React.PureComponent {
{showStore &&

{'STATE'}

} {this.state.showDiffs ? ( -

+

DIFFS

) : null} -

{getActionsName(type)}

+

{getActionsName(type)}

TTE