Raw
1 import { InformationCircleSolid } from "@medusajs/icons"
2
3 import {
4 HookData,
5 HookDataMap,
6 EnumType,
7 FunctionType,
8 ObjectType,
9 } from "@/types/ui"
10 import { InlineCode, Table, Tooltip } from "docs-ui"
11
12 interface HookTableProps {
13 props: HookDataMap
14 isReturn?: boolean
15 }
16
17 const HookTable = ({ props, isReturn = false }: HookTableProps) => {
18 return (
19 <Table className="!mb-0">
20 <Table.Header className="border-t-0">
21 <Table.Row>
22 <Table.HeaderCell>Name</Table.HeaderCell>
23 <Table.HeaderCell>Type</Table.HeaderCell>
24 <Table.HeaderCell className="!text-right">
25 {isReturn ? "Description" : "Default"}
26 </Table.HeaderCell>
27 </Table.Row>
28 </Table.Header>
29 <Table.Body className="border-b-0 [&_tr:last-child]:border-b-0">
30 {/* eslint-disable-next-line react/prop-types */}
31 {props.map((propData, index) => (
32 <Row key={index} {...propData} isReturn={isReturn} />
33 ))}
34 </Table.Body>
35 </Table>
36 )
37 }
38
39 interface RowProps extends HookData {
40 isReturn?: boolean
41 }
42
43 const Row = ({
44 value,
45 type,
46 description,
47 default: defaultValue,
48 isReturn = false,
49 }: RowProps) => {
50 const isEnum = (t: unknown): t is EnumType => {
51 return (t as EnumType).type !== undefined && (t as EnumType).type === "enum"
52 }
53
54 const isObject = (t: unknown): t is ObjectType => {
55 return (
56 (t as ObjectType).type !== undefined &&
57 (t as ObjectType).type === "object"
58 )
59 }
60
61 const isFunction = (t: unknown): t is FunctionType => {
62 return (
63 (t as FunctionType).type !== undefined &&
64 (t as FunctionType).type === "function"
65 )
66 }
67
68 const isComplexType = isEnum(type) || isObject(type) || isFunction(type)
69
70 return (
71 <Table.Row className="code-body">
72 <Table.Cell>
73 <div className="flex items-center gap-x-1">
74 <InlineCode>{value}</InlineCode>
75 {!isReturn && description && (
76 <Tooltip content={description} className="max-w-[350px] text-left">
77 <InformationCircleSolid className="text-medusa-fg-subtle" />
78 </Tooltip>
79 )}
80 </div>
81 </Table.Cell>
82 <Table.Cell>
83 {!isComplexType && type.toString()}
84 {isEnum(type) && (
85 <Tooltip
86 content={type.values.map((v) => `"${v}"`).join(" | ")}
87 className="font-mono"
88 >
89 <div className="flex items-center gap-x-1">
90 <span>enum</span>
91 <InformationCircleSolid className="text-medusa-fg-subtle" />
92 </div>
93 </Tooltip>
94 )}
95 {isObject(type) && (
96 <Tooltip
97 tooltipChildren={<pre>{type.shape}</pre>}
98 className="font-mono max-w-[500px]"
99 tooltipClassName="!text-left"
100 >
101 <div className="flex items-center gap-x-1">
102 <span>{type.name}</span>
103 <InformationCircleSolid className="text-medusa-fg-subtle" />
104 </div>
105 </Tooltip>
106 )}
107 {isFunction(type) && (
108 <Tooltip
109 tooltipChildren={<pre>{type.signature}</pre>}
110 className="font-mono max-w-[500px]"
111 >
112 <div className="flex items-center gap-x-1">
113 <span>function</span>
114 <InformationCircleSolid className="text-medusa-fg-subtle" />
115 </div>
116 </Tooltip>
117 )}
118 </Table.Cell>
119 <Table.Cell className="!text-right">
120 {isReturn ? (
121 description ? (
122 <span>{description}</span>
123 ) : (
124 <span className="text-medusa-fg-muted"> - </span>
125 )
126 ) : defaultValue !== undefined && defaultValue !== null ? (
127 <InlineCode>{defaultValue.toString()}</InlineCode>
128 ) : (
129 <span className="text-medusa-fg-muted"> - </span>
130 )}
131 </Table.Cell>
132 </Table.Row>
133 )
134 }
135
136 export { HookTable }