1 import { Documentation } from "react-docgen"
2 import { Suspense } from "react"
3 import { Spinner } from "@medusajs/icons"
4 import { PropTable } from "../PropsTable"
5 import { Container } from "@medusajs/ui"
6 import Feedback from "../Feedback"
7 import { H3, MarkdownContent } from "docs-ui"
8 import MDXComponents from "../MDXComponents"
9 import slugify from "slugify"
10
11 type ComponentReferenceProps = {
12 mainComponent: string
13 componentsToShow?: string[]
14 specsSrc?: string
15 hideFeedback?: boolean
16 }
17
18 const ComponentReference = ({
19 mainComponent,
20 componentsToShow = [mainComponent],
21 specsSrc,
22 hideFeedback = false,
23 }: ComponentReferenceProps) => {
24 if (!specsSrc) {
25 return <></>
26 }
27
28 const specs = JSON.parse(specsSrc) as Documentation[]
29
30 return (
31 <>
32 {componentsToShow.map((component, index) => {
33 const componentSpec = specs?.find(
34 (spec) => spec.displayName === component
35 )
36 const hasProps =
37 componentSpec?.props && Object.keys(componentSpec.props).length > 0
38 const componentName =
39 componentsToShow.length > 1
40 ? componentSpec?.displayName || component
41 : ""
42 const componentSlug = slugify(componentName)
43 return (
44 <Suspense
45 fallback={
46 <div className="text-medusa-fg-muted flex flex-1 items-center justify-center">
47 <Spinner className="animate-spin" />
48 </div>
49 }
50 key={index}
51 >
52 {componentSpec && (
53 <>
54 {componentsToShow.length > 1 && (
55 <H3 id={componentSlug}>{componentName}</H3>
56 )}
57 {componentSpec.description && (
58 <MarkdownContent components={MDXComponents}>
59 {componentSpec.description}
60 </MarkdownContent>
61 )}
62 {hasProps && (
63 <>
64 <Container className="mb-6 mt-8 overflow-hidden p-0">
65 <Suspense
66 fallback={
67 <div className="text-medusa-fg-muted flex flex-1 items-center justify-center">
68 <Spinner className="animate-spin" />
69 </div>
70 }
71 >
72 <PropTable props={componentSpec.props!} />
73 </Suspense>
74 </Container>
75 {!hideFeedback && (
76 <Feedback
77 title={`props of ${component}`}
78 question="Was this helpful?"
79 showDottedSeparator={false}
80 />
81 )}
82 </>
83 )}
84 </>
85 )}
86 </Suspense>
87 )
88 })}
89 </>
90 )
91 }
92
93 export { ComponentReference }