Raw
1 import React, { useMemo } from "react"
2 import { NoteProps } from ".."
3 import clsx from "clsx"
4 import { MarkdownContent } from "../../MarkdownContent"
5
6 type StringInfo = {
7 allStringChildren: boolean
8 stringChildren: string[]
9 }
10
11 type NoteLayoutProps = NoteProps
12
13 const PUNCTIONATIONS = [".", ":", ";", ",", "!", "?"]
14
15 export const NoteLayout = ({
16 type,
17 title,
18 children,
19 forceMultiline = false,
20 }: NoteLayoutProps) => {
21 const getStringInfoFromChildren = (nodes: React.ReactNode): StringInfo => {
22 let allStringChildren = true
23 const stringChildren: string[] = []
24
25 React.Children.forEach(nodes, (child) => {
26 if (!allStringChildren) {
27 return
28 } else if (["string", "number"].includes(typeof child)) {
29 stringChildren.push(`${child}`)
30 } else if (Array.isArray(child)) {
31 const childInfo = getStringInfoFromChildren(child)
32 allStringChildren = childInfo.allStringChildren
33 stringChildren.push(...childInfo.stringChildren)
34 } else if (
35 React.isValidElement(child) &&
36 typeof child.props === "object" &&
37 child.props &&
38 "children" in child.props &&
39 child.props.children
40 ) {
41 const typeStr = child.type.toString()
42 if (
43 typeStr.includes("InlineCode") &&
44 typeof child.props.children === "string"
45 ) {
46 stringChildren.push(`\`${child.props.children}\``)
47 return
48 } else if (typeStr.includes(`li`)) {
49 allStringChildren = false
50 return
51 } else if (
52 "href" in child.props &&
53 typeof child.props.children === "string"
54 ) {
55 stringChildren.push(`[${child.props.children}](${child.props.href})`)
56 return
57 }
58
59 const childInfo = getStringInfoFromChildren(
60 child.props.children as React.ReactNode
61 )
62 allStringChildren = childInfo.allStringChildren
63 stringChildren.push(...childInfo.stringChildren)
64 }
65 })
66
67 return {
68 allStringChildren,
69 stringChildren,
70 }
71 }
72 const { allStringChildren, stringChildren } = useMemo(() => {
73 if (forceMultiline) {
74 return {
75 allStringChildren: false,
76 stringChildren: "",
77 }
78 }
79
80 const { allStringChildren, stringChildren } =
81 getStringInfoFromChildren(children)
82
83 return {
84 allStringChildren,
85 stringChildren: stringChildren.join(""),
86 }
87 }, [children, forceMultiline])
88
89 const showColon = useMemo(() => {
90 const lastChar = title?.charAt(title.length - 1) || ""
91
92 return !PUNCTIONATIONS.includes(lastChar)
93 }, [title])
94
95 return (
96 <div
97 className={clsx(
98 "py-[10px] px-docs_0.75 my-docs_1",
99 "flex gap-docs_0.75 rounded-docs_DEFAULT items-stretch",
100 "bg-medusa-bg-component border border-medusa-border-base"
101 )}
102 data-testid="note-layout"
103 >
104 <span
105 className={clsx(
106 "rounded-full w-docs_0.25",
107 // TODO remove once we use the new prerequisites component across docs
108 (type === "default" || type === "check") &&
109 "bg-medusa-tag-neutral-icon",
110 (type === "error" || type === "warning") && "bg-medusa-tag-red-icon",
111 type === "success" && "bg-medusa-tag-green-icon",
112 // TODO remove once all soon components are removed
113 type === "soon" && "bg-medusa-tag-blue-icon"
114 )}
115 data-testid="note-layout-indicator"
116 ></span>
117 <div className="flex-1">
118 <div className="text-small text-medusa-fg-subtle [&_ol]:!mb-0 [&_ul]:!mb-0">
119 <span
120 className={clsx("text-small-plus text-medusa-fg-base")}
121 data-testid="note-layout-title"
122 >
123 {title}
124 {showColon ? ":" : ""}&nbsp;
125 </span>
126 {allStringChildren && (
127 <MarkdownContent
128 allowedElements={["a", "code"]}
129 unwrapDisallowed={true}
130 >
131 {stringChildren}
132 </MarkdownContent>
133 )}
134 {!allStringChildren && children}
135 </div>
136 </div>
137 </div>
138 )
139 }