({ children, onClickExecute, codeBlockMaxWidthAtom }: CodeBlockProps)
| 125 | }; |
| 126 | |
| 127 | const CodeBlock = ({ children, onClickExecute, codeBlockMaxWidthAtom }: CodeBlockProps) => { |
| 128 | const codeBlockMaxWidth = useAtomValueSafe(codeBlockMaxWidthAtom); |
| 129 | const getLanguage = (children: any): string => { |
| 130 | if (children?.props?.className) { |
| 131 | const match = children.props.className.match(/language-([\w+-]+)/i); |
| 132 | if (match) return match[1]; |
| 133 | } |
| 134 | return "text"; |
| 135 | }; |
| 136 | |
| 137 | const handleCopy = async (e: React.MouseEvent) => { |
| 138 | const textToCopy = extractText(children).replace(/\n$/, ""); |
| 139 | await navigator.clipboard.writeText(textToCopy); |
| 140 | }; |
| 141 | |
| 142 | const handleExecute = (e: React.MouseEvent) => { |
| 143 | const cmd = extractText(children).replace(/\n$/, ""); |
| 144 | if (onClickExecute) { |
| 145 | onClickExecute(cmd); |
| 146 | return; |
| 147 | } |
| 148 | }; |
| 149 | |
| 150 | const language = getLanguage(children); |
| 151 | |
| 152 | return ( |
| 153 | <div |
| 154 | className={cn("rounded-lg overflow-hidden bg-black my-4", codeBlockMaxWidth && "max-w-full")} |
| 155 | style={ |
| 156 | codeBlockMaxWidth |
| 157 | ? { maxWidth: codeBlockMaxWidth, minWidth: Math.min(400, codeBlockMaxWidth) } |
| 158 | : undefined |
| 159 | } |
| 160 | > |
| 161 | <div className="flex items-center justify-between pl-3 pr-2 pt-2 pb-1.5"> |
| 162 | <span className="text-[11px] text-white/50">{language}</span> |
| 163 | <div className="flex items-center gap-2"> |
| 164 | <CopyButton onClick={handleCopy} title="Copy" /> |
| 165 | {onClickExecute && ( |
| 166 | <IconButton |
| 167 | decl={{ |
| 168 | elemtype: "iconbutton", |
| 169 | icon: "regular@square-terminal", |
| 170 | click: handleExecute, |
| 171 | }} |
| 172 | /> |
| 173 | )} |
| 174 | </div> |
| 175 | </div> |
| 176 | <pre className="px-4 pb-2 pt-0 overflow-x-auto m-0 text-secondary max-w-full">{children}</pre> |
| 177 | </div> |
| 178 | ); |
| 179 | }; |
| 180 | |
| 181 | function Collapsible({ title, children, defaultOpen = false }) { |
| 182 | const [isOpen, setIsOpen] = useState(defaultOpen); |
nothing calls this directly
no test coverage detected