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} ${numberOfKeys === 1 ? "key" : "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 JSON{" "}
51 <span key="count-span" className="text-ui-fg-subtle">
52 {numberOfKeys}
53 </span>
54 </Heading>
55 </Drawer.Title>
56 <Drawer.Description className="sr-only">
57 View the JSON representation of the draft order.
58 </Drawer.Description>
59 </div>
60 <div className="flex items-center gap-x-2">
61 <Kbd className="bg-ui-contrast-bg-subtle border-ui-contrast-border-base text-ui-contrast-fg-secondary">
62 esc
63 </Kbd>
64 <Drawer.Close asChild>
65 <IconButton
66 size="small"
67 variant="transparent"
68 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"
69 >
70 <XMarkMini />
71 </IconButton>
72 </Drawer.Close>
73 </div>
74 </div>
75 <Drawer.Body className="flex flex-1 flex-col overflow-hidden px-[5px] py-0 pb-[5px]">
76 <div className="bg-ui-contrast-bg-subtle flex-1 overflow-auto rounded-b-[4px] rounded-t-lg p-3">
77 <Suspense
78 fallback={<div className="flex size-full flex-col"></div>}
79 >
80 <Primitive
81 value={data}
82 displayDataTypes={false}
83 style={
84 {
85 "--w-rjv-font-family": "Roboto Mono, monospace",
86 "--w-rjv-line-color": "var(--contrast-border-base)",
87 "--w-rjv-curlybraces-color":
88 "var(--contrast-fg-secondary)",
89 "--w-rjv-brackets-color": "var(--contrast-fg-secondary)",
90 "--w-rjv-key-string": "var(--contrast-fg-primary)",
91 "--w-rjv-info-color": "var(--contrast-fg-secondary)",
92 "--w-rjv-type-string-color": "var(--tag-green-icon)",
93 "--w-rjv-quotes-string-color": "var(--tag-green-icon)",
94 "--w-rjv-type-boolean-color": "var(--tag-orange-icon)",
95 "--w-rjv-type-int-color": "var(--tag-orange-icon)",
96 "--w-rjv-type-float-color": "var(--tag-orange-icon)",
97 "--w-rjv-type-bigint-color": "var(--tag-orange-icon)",
98 "--w-rjv-key-number": "var(--contrast-fg-secondary)",
99 "--w-rjv-arrow-color": "var(--contrast-fg-secondary)",
100 "--w-rjv-copied-color": "var(--contrast-fg-secondary)",
101 "--w-rjv-copied-success-color":
102 "var(--contrast-fg-primary)",
103 "--w-rjv-colon-color": "var(--contrast-fg-primary)",
104 "--w-rjv-ellipsis-color": "var(--contrast-fg-secondary)",
105 } as CSSProperties
106 }
107 collapsed={1}
108 >
109 <Primitive.Quote render={() => <span />} />
110 <Primitive.Null
111 render={() => (
112 <span className="text-ui-tag-red-icon">null</span>
113 )}
114 />
115 <Primitive.Undefined
116 render={() => (
117 <span className="text-ui-tag-blue-icon">undefined</span>
118 )}
119 />
120 <Primitive.CountInfo
121 render={(_props, { value }) => {
122 return (
123 <span className="text-ui-contrast-fg-secondary ml-2">
124 {`${Object.keys(value as object).length} ${
125 Object.keys(value as object).length === 1
126 ? "key"
127 : "keys"
128 }`}
129 </span>
130 )
131 }}
132 />
133 <Primitive.Arrow>
134 <TriangleDownMini className="text-ui-contrast-fg-secondary -ml-[0.5px]" />
135 </Primitive.Arrow>
136 <Primitive.Colon>
137 <span className="mr-1">:</span>
138 </Primitive.Colon>
139 <Primitive.Copied
140 render={({ style }, { value }) => {
141 return <Copied style={style} value={value} />
142 }}
143 />
144 </Primitive>
145 </Suspense>
146 </div>
147 </Drawer.Body>
148 </Drawer.Content>
149 </Drawer>
150 </Container>
151 )
152 }
153
154 type CopiedProps = {
155 style?: CSSProperties
156 value: object | undefined
157 }
158
159 const Copied = ({ style, value }: CopiedProps) => {
160 const [copied, setCopied] = useState(false)
161
162 const handler = (e: MouseEvent<HTMLSpanElement>) => {
163 e.stopPropagation()
164 setCopied(true)
165
166 if (typeof value === "string") {
167 navigator.clipboard.writeText(value)
168 } else {
169 const json = JSON.stringify(value, null, 2)
170 navigator.clipboard.writeText(json)
171 }
172
173 setTimeout(() => {
174 setCopied(false)
175 }, 2000)
176 }
177
178 const styl = { whiteSpace: "nowrap", width: "20px" }
179
180 if (copied) {
181 return (
182 <span style={{ ...style, ...styl }}>
183 <Check className="text-ui-contrast-fg-primary" />
184 </span>
185 )
186 }
187
188 return (
189 <span style={{ ...style, ...styl }} onClick={handler}>
190 <SquareTwoStack className="text-ui-contrast-fg-secondary" />
191 </span>
192 )
193 }