MCPcopy
hub / github.com/premieroctet/openchakra / buildBlock

Function buildBlock

src/utils/code.ts:68–125  ·  view source on GitHub ↗
({
  component,
  components,
  forceBuildBlock = false,
}: BuildBlockParams)

Source from the content-addressed store, hash-verified

66}
67
68const buildBlock = ({
69 component,
70 components,
71 forceBuildBlock = false,
72}: BuildBlockParams) => {
73 let content = ''
74
75 component.children.forEach((key: string) => {
76 let childComponent = components[key]
77 if (!childComponent) {
78 console.error(`invalid component ${key}`)
79 } else if (forceBuildBlock || !childComponent.componentName) {
80 const componentName = capitalize(childComponent.type)
81 let propsContent = ''
82
83 const propsNames = Object.keys(childComponent.props).filter(propName => {
84 if (childComponent.type === 'Icon') {
85 return propName !== 'icon'
86 }
87
88 return true
89 })
90
91 // Special case for Highlight component
92 if (componentName === 'Highlight') {
93 const [query, children, ...restProps] = propsNames
94 propsContent += buildStyledProps([query, children], childComponent)
95
96 propsContent += `styles={{${restProps
97 .filter(propName => childComponent.props[propName])
98 .map(
99 propName => `${propName}:'${childComponent.props[propName]}'`,
100 )}}}`
101 } else {
102 propsContent += buildStyledProps(propsNames, childComponent)
103 }
104
105 if (
106 typeof childComponent.props.children === 'string' &&
107 childComponent.children.length === 0
108 ) {
109 content += `<${componentName} ${propsContent}>${childComponent.props.children}</${componentName}>`
110 } else if (childComponent.type === 'Icon') {
111 content += `<${childComponent.props.icon} ${propsContent} />`
112 } else if (childComponent.children.length) {
113 content += `<${componentName} ${propsContent}>
114 ${buildBlock({ component: childComponent, components, forceBuildBlock })}
115 </${componentName}>`
116 } else {
117 content += `<${componentName} ${propsContent} />`
118 }
119 } else {
120 content += `<${childComponent.componentName} />`
121 }
122 })
123
124 return content
125}

Callers 2

generateComponentCodeFunction · 0.85
generateCodeFunction · 0.85

Calls 2

capitalizeFunction · 0.85
buildStyledPropsFunction · 0.85

Tested by

no test coverage detected