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