import React, { useRef, useState } from "react" import hljs from "highlight.js" import { ChevronDown, ChevronUp } from "lucide-react" import { cn } from "../../design-lib/utils" import { Icons } from "../organisms/icons" import { Button, buttonVariants } from "./button" import CopyButton from "./copy-button" import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "./select" const fileIcons = { ts: Icons.typescript, js: Icons.javascript, jsx: Icons.jsx, tsx: Icons.typescript, css: Icons.css, typescript: Icons.typescript, javascript: Icons.javascript, html: Icons.html, md: Icons.markdown, markdown: Icons.markdown, py: Icons.python, python: Icons.python, plaintext: Icons.plaintext, } function CodeBlock({ language, snippet, preElementClass, codeElementClass, copyable = true, codeWrap = false, disableCodeFont = false, overflowBlur = false, switchCode = null, filename, hideLineNumbers = false, gray = false, className, highlightLines, collapse, }: { language?: string snippet?: string preElementClass?: string codeElementClass?: string copyable?: boolean codeWrap?: boolean disableCodeFont?: boolean overflowBlur?: boolean filename?: string switchCode?: | { value: string language: string filename: string snippet: string }[] | null hideLineNumbers?: boolean gray?: boolean className?: string highlightLines?: number[] collapse?: boolean }) { const [switchState, setSwitch] = useState<{ value: string language: string filename: string snippet: string } | null>(switchCode ? switchCode[0] : null) const [languageState, setLanguage] = useState( switchCode ? switchCode[0].language : language! ) const [filenameState, setFilename] = useState( switchCode ? switchCode[0].filename : filename ) const [snippetState, setSnippet] = useState( switchCode ? switchCode[0].snippet : snippet ) const [codeExpanded, setCodeExpanded] = useState(false) const getLines = (highlightedCode: string) => { return highlightedCode.split("\n") } // Function to wrap each line with the .line div const wrapLinesWithClass = (highlightedCode: string) => { const lines = getLines(highlightedCode) return lines .map( (line: string, index) => `
${line}
` ) .join("\n") .split("\n") .slice(0, -1) .join("\n") } snippet = snippetState || "" hljs.getLanguage(languageState) ? (language = languageState) : (language = "plaintext") const highlightedCode = hljs.highlight(snippetState!, { language }).value // Wrap each line in a div with the class .line const wrappedHighlightedCode = wrapLinesWithClass(highlightedCode) const codeRef = useRef(null) const isLanguageInIcons = Object.keys(fileIcons).includes(languageState) const fileIcon = isLanguageInIcons ? (fileIcons as any)[languageState] : fileIcons.plaintext return (
{filenameState && (

{fileIcon && ( {React.createElement(fileIcon, { className: "h-4 w-4", })} )}{" "} {filenameState}

{switchCode && ( )}
)}
= (collapse ? 20 : 200) &&
              !codeExpanded,
          },
          preElementClass
        )}
      >
        {copyable && !filenameState && (
          
        )}

        {overflowBlur && (
          
)} = (collapse ? 20 : 200) && !codeExpanded, }, codeElementClass )} >
{getLines(highlightedCode).length >= (collapse ? 20 : 200) && (
{!codeExpanded && (
)}
)}
) } const InlineCode = (props: any) => ( ) export { CodeBlock, InlineCode }