Skip to content

Commit 2f093d6

Browse files
committed
perf: improve JSON display performance with large payload
1 parent 766d229 commit 2f093d6

1 file changed

Lines changed: 53 additions & 4 deletions

File tree

src/client/components/DataRender.tsx

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
import { useTranslation } from '@i18next-toolkit/react';
22
import { Empty } from 'antd';
33
import React from 'react';
4+
import { Button } from './ui/button';
45

56
interface DataRenderProps {
67
className?: string;
78
type: string;
89
value: any;
910
}
11+
12+
const MAX_DISPLAY_LENGTH = 5000;
13+
1014
export const DataRender: React.FC<DataRenderProps> = React.memo((props) => {
1115
const { t } = useTranslation();
16+
const [isExpanded, setIsExpanded] = React.useState(false);
1217

1318
if (props.type === 'imageUrl') {
1419
if (props.value) {
@@ -19,14 +24,58 @@ export const DataRender: React.FC<DataRenderProps> = React.memo((props) => {
1924
}
2025

2126
if (props.type === 'json') {
27+
const contentString =
28+
typeof props.value === 'object'
29+
? JSON.stringify(props.value, null, 2)
30+
: String(props.value);
31+
32+
const isLongContent = contentString.length > MAX_DISPLAY_LENGTH;
33+
const displayContent =
34+
isExpanded || !isLongContent
35+
? contentString
36+
: contentString.substring(0, MAX_DISPLAY_LENGTH) + '...';
37+
const remainingLength = contentString.length - MAX_DISPLAY_LENGTH;
38+
39+
const toggleButtons = isLongContent && (
40+
<>
41+
{!isExpanded && (
42+
<Button
43+
className="ml-2"
44+
variant="outline"
45+
size="sm"
46+
onClick={() => setIsExpanded(true)}
47+
>
48+
{t('Expand')} (+{remainingLength.toLocaleString()} {t('characters')}
49+
)
50+
</Button>
51+
)}
52+
{isExpanded && (
53+
<Button
54+
className="ml-2"
55+
variant="outline"
56+
size="sm"
57+
onClick={() => setIsExpanded(false)}
58+
>
59+
{t('Collapse')}
60+
</Button>
61+
)}
62+
</>
63+
);
64+
2265
if (typeof props.value === 'object') {
2366
return (
24-
<pre className={props.className}>
25-
{JSON.stringify(props.value, null, 2)}
26-
</pre>
67+
<>
68+
<pre className={props.className}>{displayContent}</pre>
69+
{toggleButtons}
70+
</>
2771
);
2872
} else {
29-
return <span className={props.className}>{String(props.value)}</span>;
73+
return (
74+
<>
75+
<span className={props.className}>{displayContent}</span>
76+
{toggleButtons}
77+
</>
78+
);
3079
}
3180
}
3281

0 commit comments

Comments
 (0)