|
1
|
"use client" |
|
2
|
|
|
3
|
import { InformationCircleSolid } from "@medusajs/icons" |
|
4
|
|
|
5
|
import { PropData, PropDataMap, PropSpecType } from "@/types/ui" |
|
6
|
import { useCallback, useMemo } from "react" |
|
7
|
import { InlineCode, MarkdownContent, Table, Tooltip } from "docs-ui" |
|
8
|
|
|
9
|
type PropTableProps = { |
|
10
|
props: PropDataMap |
|
11
|
} |
|
12
|
|
|
13
|
const PropTable = ({ props }: PropTableProps) => { |
|
14
|
return ( |
|
15
|
<Table className="!mb-0"> |
|
16
|
<Table.Header className="border-t-0"> |
|
17
|
<Table.Row> |
|
18
|
<Table.HeaderCell>Prop</Table.HeaderCell> |
|
19
|
<Table.HeaderCell>Type</Table.HeaderCell> |
|
20
|
<Table.HeaderCell className="!text-right">Default</Table.HeaderCell> |
|
21
|
</Table.Row> |
|
22
|
</Table.Header> |
|
23
|
<Table.Body className="border-b-0 [&_tr:last-child]:border-b-0"> |
|
24
|
{Object.entries(props).map(([propName, propData]) => ( |
|
25
|
<Row key={propName} propName={propName} propData={propData} /> |
|
26
|
))} |
|
27
|
</Table.Body> |
|
28
|
</Table> |
|
29
|
) |
|
30
|
} |
|
31
|
|
|
32
|
type RowProps = { |
|
33
|
propName: string |
|
34
|
propData: PropData |
|
35
|
} |
|
36
|
|
|
37
|
type TypeNode = { |
|
38
|
text: string |
|
39
|
tooltipContent?: string |
|
40
|
canBeCopied?: boolean |
|
41
|
} |
|
42
|
|
|
43
|
const Row = ({ |
|
44
|
propName, |
|
45
|
propData: { tsType: tsType, defaultValue, description }, |
|
46
|
}: RowProps) => { |
|
47
|
const normalizeRaw = (str: string): string => { |
|
48
|
return str |
|
49
|
.replaceAll("\\|", "|") |
|
50
|
.replaceAll("<", "<") |
|
51
|
.replaceAll(">", ">") |
|
52
|
} |
|
53
|
const getTypeRaw = useCallback((type: PropSpecType): string => { |
|
54
|
let raw = "raw" in type ? type.raw || type.name : type.name |
|
55
|
if ("type" in type) { |
|
56
|
if (type.type === "object") { |
|
57
|
raw = `{\n ${type.signature.properties |
|
58
|
.map((property) => `${property.key}: ${property.value.name}`) |
|
59
|
.join("\n ")}\n}` |
|
60
|
} else { |
|
61
|
raw = type.raw |
|
62
|
} |
|
63
|
} else if (type.name === "Array" && "elements" in type) { |
|
64
|
raw = type.elements.map((element) => getTypeRaw(element)).join(" | ") |
|
65
|
} else if (raw.startsWith("React.")) { |
|
66
|
return "" |
|
67
|
} |
|
68
|
|
|
69
|
return normalizeRaw(raw) |
|
70
|
}, []) |
|
71
|
const getTypeText = useCallback((type: PropSpecType): string => { |
|
72
|
if (type?.name === "signature" && "type" in type) { |
|
73
|
return type.type |
|
74
|
} else if (type?.name === "Array" && type.raw) { |
|
75
|
return normalizeRaw(type.raw) || "array" |
|
76
|
} else if ("raw" in type && type.raw?.startsWith("React.")) { |
|
77
|
return type.name.replace(/^React/, "") |
|
78
|
} |
|
79
|
|
|
80
|
return type.name || "" |
|
81
|
}, []) |
|
82
|
const getTypeTooltipContent = useCallback( |
|
83
|
(type: PropSpecType): string | undefined => { |
|
84
|
if ( |
|
85
|
(type?.name === "signature" && "type" in type) || |
|
86
|
(type?.name === "Array" && type.raw) || |
|
87
|
("raw" in type && type.raw) |
|
88
|
) { |
|
89
|
return getTypeRaw(type) |
|
90
|
} |
|
91
|
|
|
92
|
return undefined |
|
93
|
}, |
|
94
|
[getTypeRaw] |
|
95
|
) |
|
96
|
|
|
97
|
const typeNodes = useMemo((): TypeNode[] => { |
|
98
|
const typeNodes: TypeNode[] = [] |
|
99
|
if (tsType?.name === "union" && "elements" in tsType) { |
|
100
|
tsType.elements.forEach((element) => { |
|
101
|
if ( |
|
102
|
("elements" in element && element.elements.length) || |
|
103
|
"signature" in element |
|
104
|
) { |
|
105
|
const elementTypeText = getTypeText(element) |
|
106
|
const elementTooltipContent = getTypeTooltipContent(element) |
|
107
|
typeNodes.push({ |
|
108
|
text: elementTypeText, |
|
109
|
tooltipContent: |
|
110
|
elementTypeText !== elementTooltipContent |
|
111
|
? elementTooltipContent |
|
112
|
: undefined, |
|
113
|
}) |
|
114
|
} else if ("value" in element) { |
|
115
|
typeNodes.push({ |
|
116
|
text: element.value, |
|
117
|
canBeCopied: true, |
|
118
|
}) |
|
119
|
} else if ("raw" in element) { |
|
120
|
typeNodes.push({ |
|
121
|
text: getTypeText(element), |
|
122
|
tooltipContent: getTypeTooltipContent(element), |
|
123
|
}) |
|
124
|
} else { |
|
125
|
typeNodes.push({ |
|
126
|
text: element.name, |
|
127
|
}) |
|
128
|
} |
|
129
|
}) |
|
130
|
} else if (tsType) { |
|
131
|
typeNodes.push({ |
|
132
|
text: getTypeText(tsType), |
|
133
|
tooltipContent: getTypeTooltipContent(tsType), |
|
134
|
}) |
|
135
|
} |
|
136
|
|
|
137
|
return typeNodes |
|
138
|
}, [tsType, getTypeText, getTypeTooltipContent]) |
|
139
|
|
|
140
|
const defaultVal: string | undefined = defaultValue?.value as string |
|
141
|
|
|
142
|
return ( |
|
143
|
<Table.Row> |
|
144
|
<Table.Cell> |
|
145
|
<div className="flex items-center gap-x-1"> |
|
146
|
<InlineCode>{propName}</InlineCode> |
|
147
|
{description && ( |
|
148
|
<Tooltip |
|
149
|
tooltipChildren={ |
|
150
|
<MarkdownContent |
|
151
|
allowedElements={["a", "code"]} |
|
152
|
unwrapDisallowed={true} |
|
153
|
> |
|
154
|
{description} |
|
155
|
</MarkdownContent> |
|
156
|
} |
|
157
|
> |
|
158
|
<InformationCircleSolid className="text-medusa-fg-subtle" /> |
|
159
|
</Tooltip> |
|
160
|
)} |
|
161
|
</div> |
|
162
|
</Table.Cell> |
|
163
|
<Table.Cell> |
|
164
|
<div className="flex items-center flex-wrap gap-1 py-1"> |
|
165
|
{typeNodes.map((typeNode, index) => ( |
|
166
|
<div key={index} className="flex items-center gap-x-1"> |
|
167
|
{index > 0 && <span>|</span>} |
|
168
|
{typeNode.tooltipContent && ( |
|
169
|
<Tooltip |
|
170
|
tooltipChildren={<pre>{typeNode.tooltipContent}</pre>} |
|
171
|
className="font-mono !max-w-none" |
|
172
|
tooltipClassName="!text-left" |
|
173
|
> |
|
174
|
<div className="flex items-center gap-x-1"> |
|
175
|
<code>{typeNode.text}</code> |
|
176
|
<InformationCircleSolid className="text-medusa-fg-subtle" /> |
|
177
|
</div> |
|
178
|
</Tooltip> |
|
179
|
)} |
|
180
|
{!typeNode.tooltipContent && ( |
|
181
|
<> |
|
182
|
{typeNode.canBeCopied && ( |
|
183
|
<InlineCode>{typeNode.text}</InlineCode> |
|
184
|
)} |
|
185
|
{!typeNode.canBeCopied && <code>{typeNode.text}</code>} |
|
186
|
</> |
|
187
|
)} |
|
188
|
</div> |
|
189
|
))} |
|
190
|
</div> |
|
191
|
</Table.Cell> |
|
192
|
<Table.Cell className="text-right"> |
|
193
|
{defaultVal && <InlineCode>{defaultVal}</InlineCode>} |
|
194
|
{!defaultVal && " - "} |
|
195
|
</Table.Cell> |
|
196
|
</Table.Row> |
|
197
|
) |
|
198
|
} |
|
199
|
|
|
200
|
export { PropTable } |