|
1
|
import { |
|
2
|
ArrowUpRightOnBox, |
|
3
|
Check, |
|
4
|
SquareTwoStack, |
|
5
|
TriangleDownMini, |
|
6
|
XMarkMini, |
|
7
|
} from "@medusajs/icons"; |
|
8
|
import { |
|
9
|
Badge, |
|
10
|
Container, |
|
11
|
Drawer, |
|
12
|
Heading, |
|
13
|
IconButton, |
|
14
|
Kbd, |
|
15
|
} from "@medusajs/ui"; |
|
16
|
import Primitive from "@uiw/react-json-view"; |
|
17
|
import { CSSProperties, MouseEvent, Suspense, useState } from "react"; |
|
18
|
|
|
19
|
type JsonViewSectionProps = { |
|
20
|
data: object; |
|
21
|
title?: string; |
|
22
|
}; |
|
23
|
|
|
24
|
export const JsonViewSection = ({ data }: JsonViewSectionProps) => { |
|
25
|
const numberOfKeys = Object.keys(data).length; |
|
26
|
|
|
27
|
return ( |
|
28
|
<Container className="flex items-center justify-between px-6 py-4"> |
|
29
|
<div className="flex items-center gap-x-4"> |
|
30
|
<Heading level="h2">JSON</Heading> |
|
31
|
<Badge size="2xsmall" rounded="full"> |
|
32
|
{numberOfKeys} keys |
|
33
|
</Badge> |
|
34
|
</div> |
|
35
|
<Drawer> |
|
36
|
<Drawer.Trigger asChild> |
|
37
|
<IconButton |
|
38
|
size="small" |
|
39
|
variant="transparent" |
|
40
|
className="text-ui-fg-muted hover:text-ui-fg-subtle" |
|
41
|
> |
|
42
|
<ArrowUpRightOnBox /> |
|
43
|
</IconButton> |
|
44
|
</Drawer.Trigger> |
|
45
|
<Drawer.Content className="bg-ui-contrast-bg-base text-ui-code-fg-subtle !shadow-elevation-commandbar overflow-hidden border border-none max-md:inset-x-2 max-md:max-w-[calc(100%-16px)]"> |
|
46
|
<div className="bg-ui-code-bg-base flex items-center justify-between px-6 py-4"> |
|
47
|
<div className="flex items-center gap-x-4"> |
|
48
|
<Drawer.Title asChild> |
|
49
|
<Heading className="text-ui-contrast-fg-primary"> |
|
50
|
<span className="text-ui-fg-subtle">{numberOfKeys}</span> |
|
51
|
</Heading> |
|
52
|
</Drawer.Title> |
|
53
|
</div> |
|
54
|
<div className="flex items-center gap-x-2"> |
|
55
|
<Kbd className="bg-ui-contrast-bg-subtle border-ui-contrast-border-base text-ui-contrast-fg-secondary"> |
|
56
|
esc |
|
57
|
</Kbd> |
|
58
|
<Drawer.Close asChild> |
|
59
|
<IconButton |
|
60
|
size="small" |
|
61
|
variant="transparent" |
|
62
|
className="text-ui-contrast-fg-secondary hover:text-ui-contrast-fg-primary hover:bg-ui-contrast-bg-base-hover active:bg-ui-contrast-bg-base-pressed focus-visible:bg-ui-contrast-bg-base-hover focus-visible:shadow-borders-interactive-with-active" |
|
63
|
> |
|
64
|
<XMarkMini /> |
|
65
|
</IconButton> |
|
66
|
</Drawer.Close> |
|
67
|
</div> |
|
68
|
</div> |
|
69
|
<Drawer.Body className="flex flex-1 flex-col overflow-hidden px-[5px] py-0 pb-[5px]"> |
|
70
|
<div className="bg-ui-contrast-bg-subtle flex-1 overflow-auto rounded-b-[4px] rounded-t-lg p-3"> |
|
71
|
<Suspense |
|
72
|
fallback={<div className="flex size-full flex-col"></div>} |
|
73
|
> |
|
74
|
<Primitive |
|
75
|
value={data} |
|
76
|
displayDataTypes={false} |
|
77
|
style={ |
|
78
|
{ |
|
79
|
"--w-rjv-font-family": "Roboto Mono, monospace", |
|
80
|
"--w-rjv-line-color": "var(--contrast-border-base)", |
|
81
|
"--w-rjv-curlybraces-color": |
|
82
|
"var(--contrast-fg-secondary)", |
|
83
|
"--w-rjv-brackets-color": "var(--contrast-fg-secondary)", |
|
84
|
"--w-rjv-key-string": "var(--contrast-fg-primary)", |
|
85
|
"--w-rjv-info-color": "var(--contrast-fg-secondary)", |
|
86
|
"--w-rjv-type-string-color": "var(--tag-green-icon)", |
|
87
|
"--w-rjv-quotes-string-color": "var(--tag-green-icon)", |
|
88
|
"--w-rjv-type-boolean-color": "var(--tag-orange-icon)", |
|
89
|
"--w-rjv-type-int-color": "var(--tag-orange-icon)", |
|
90
|
"--w-rjv-type-float-color": "var(--tag-orange-icon)", |
|
91
|
"--w-rjv-type-bigint-color": "var(--tag-orange-icon)", |
|
92
|
"--w-rjv-key-number": "var(--contrast-fg-secondary)", |
|
93
|
"--w-rjv-arrow-color": "var(--contrast-fg-secondary)", |
|
94
|
"--w-rjv-copied-color": "var(--contrast-fg-secondary)", |
|
95
|
"--w-rjv-copied-success-color": |
|
96
|
"var(--contrast-fg-primary)", |
|
97
|
"--w-rjv-colon-color": "var(--contrast-fg-primary)", |
|
98
|
"--w-rjv-ellipsis-color": "var(--contrast-fg-secondary)", |
|
99
|
} as CSSProperties |
|
100
|
} |
|
101
|
collapsed={1} |
|
102
|
> |
|
103
|
<Primitive.Quote render={() => <span />} /> |
|
104
|
<Primitive.Null |
|
105
|
render={() => ( |
|
106
|
<span className="text-ui-tag-red-icon">null</span> |
|
107
|
)} |
|
108
|
/> |
|
109
|
<Primitive.Undefined |
|
110
|
render={() => ( |
|
111
|
<span className="text-ui-tag-blue-icon">undefined</span> |
|
112
|
)} |
|
113
|
/> |
|
114
|
<Primitive.CountInfo |
|
115
|
render={(_props, { value }) => { |
|
116
|
return ( |
|
117
|
<span className="text-ui-contrast-fg-secondary ml-2"> |
|
118
|
{Object.keys(value as object).length} items |
|
119
|
</span> |
|
120
|
); |
|
121
|
}} |
|
122
|
/> |
|
123
|
<Primitive.Arrow> |
|
124
|
<TriangleDownMini className="text-ui-contrast-fg-secondary -ml-[0.5px]" /> |
|
125
|
</Primitive.Arrow> |
|
126
|
<Primitive.Colon> |
|
127
|
<span className="mr-1">:</span> |
|
128
|
</Primitive.Colon> |
|
129
|
<Primitive.Copied |
|
130
|
render={({ style }, { value }) => { |
|
131
|
return <Copied style={style} value={value} />; |
|
132
|
}} |
|
133
|
/> |
|
134
|
</Primitive> |
|
135
|
</Suspense> |
|
136
|
</div> |
|
137
|
</Drawer.Body> |
|
138
|
</Drawer.Content> |
|
139
|
</Drawer> |
|
140
|
</Container> |
|
141
|
); |
|
142
|
}; |
|
143
|
|
|
144
|
type CopiedProps = { |
|
145
|
style?: CSSProperties; |
|
146
|
value: object | undefined; |
|
147
|
}; |
|
148
|
|
|
149
|
const Copied = ({ style, value }: CopiedProps) => { |
|
150
|
const [copied, setCopied] = useState(false); |
|
151
|
|
|
152
|
const handler = (e: MouseEvent<HTMLSpanElement>) => { |
|
153
|
e.stopPropagation(); |
|
154
|
setCopied(true); |
|
155
|
|
|
156
|
if (typeof value === "string") { |
|
157
|
navigator.clipboard.writeText(value); |
|
158
|
} else { |
|
159
|
const json = JSON.stringify(value, null, 2); |
|
160
|
navigator.clipboard.writeText(json); |
|
161
|
} |
|
162
|
|
|
163
|
setTimeout(() => { |
|
164
|
setCopied(false); |
|
165
|
}, 2000); |
|
166
|
}; |
|
167
|
|
|
168
|
const styl = { whiteSpace: "nowrap", width: "20px" }; |
|
169
|
|
|
170
|
if (copied) { |
|
171
|
return ( |
|
172
|
<span style={{ ...style, ...styl }}> |
|
173
|
<Check className="text-ui-contrast-fg-primary" /> |
|
174
|
</span> |
|
175
|
); |
|
176
|
} |
|
177
|
|
|
178
|
return ( |
|
179
|
<span style={{ ...style, ...styl }} onClick={handler}> |
|
180
|
<SquareTwoStack className="text-ui-contrast-fg-secondary" /> |
|
181
|
</span> |
|
182
|
); |
|
183
|
}; |