-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDebugPrettyPrint.tsx
130 lines (122 loc) · 3.27 KB
/
DebugPrettyPrint.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
WARNING: This component is not referenced anywhere, but is used
for development purposes. for development purposes. Don't REMOVE it!
*/
import {
HStack,
IOColors,
IconButton,
LabelSmall,
useTypographyFactory,
} from "@pagopa/io-app-design-system";
import React from "react";
import { StyleSheet, View } from "react-native";
import { clipboardSetStringWithFeedback } from "../../utils/clipboard";
import _ from "lodash";
type ExpandableProps =
| {
expandable: true;
isExpanded?: boolean;
}
| {
expandable?: false;
isExpanded?: undefined;
};
type Props = {
title: string;
data: any;
} & ExpandableProps;
const MAX_CHARACTERS = 256;
/**
* This component allows to print the content of an object in an elegant and readable way.
* and to copy its content to the clipboard by pressing on the title.
* The component it is rendered only if debug mode is enabled
*/
export const DebugPrettyPrint = ({
title,
data,
expandable = true,
isExpanded = false,
}: Props) => {
const [expanded, setExpanded] = React.useState(isExpanded);
const clipboardData = React.useMemo(() => JSON.stringify(data), [data]);
const prettyData = React.useMemo(() => {
try {
const json = JSON.parse(JSON.stringify(data));
return JSON.stringify(
_.cloneDeepWith(json, (value) =>
typeof value === "string"
? _.truncate(value, { length: MAX_CHARACTERS })
: undefined
),
null,
2
);
} catch (e) {
const value = JSON.stringify(data, null, 2);
return _.truncate(value, { length: MAX_CHARACTERS });
}
}, [data]);
const content = React.useMemo(() => {
if ((expandable && !expanded) || !expandable) {
return null;
}
return (
<View style={styles.content} pointerEvents="box-only">
<CustomBodyMonospace>{prettyData}</CustomBodyMonospace>
</View>
);
}, [prettyData, expandable, expanded]);
return (
<View testID="DebugPrettyPrintTestID" style={styles.container}>
<View style={styles.header}>
<LabelSmall weight="Bold" color="white">
{title}
</LabelSmall>
<HStack space={16}>
<IconButton
icon={"copy"}
accessibilityLabel="copy"
onPress={() => clipboardSetStringWithFeedback(clipboardData)}
color="contrast"
/>
{expandable && (
<IconButton
icon={expanded ? "chevronTop" : "chevronBottom"}
accessibilityLabel="expand"
onPress={() => setExpanded((_) => !_)}
color="contrast"
/>
)}
</HStack>
</View>
{content}
</View>
);
};
const CustomBodyMonospace = (props: { children?: React.ReactNode }) =>
useTypographyFactory({
...props,
defaultWeight: "Medium",
defaultColor: "bluegrey",
font: "DMMono",
fontStyle: { fontSize: 12, lineHeight: 18 },
});
const styles = StyleSheet.create({
container: {
borderRadius: 4,
overflow: "hidden",
marginVertical: 4,
},
header: {
backgroundColor: IOColors["error-600"],
padding: 12,
flex: 1,
flexDirection: "row",
justifyContent: "space-between",
},
content: {
backgroundColor: IOColors["grey-50"],
padding: 8,
},
});