'use client' import { useEffect, useRef, useState } from 'react' import * as Prism from 'prismjs' import 'prismjs/components/prism-javascript' import 'prismjs/components/prism-typescript' import 'prismjs/components/prism-python' import 'prismjs/components/prism-go' import 'prismjs/components/prism-rust' import 'prismjs/components/prism-java' import 'prismjs/components/prism-c' import 'prismjs/components/prism-cpp' import 'prismjs/components/prism-csharp' import 'prismjs/components/prism-ruby' import 'prismjs/components/prism-php' import 'prismjs/components/prism-swift' import 'prismjs/components/prism-kotlin' import 'prismjs/components/prism-bash' import 'prismjs/components/prism-json' import 'prismjs/components/prism-yaml' import 'prismjs/components/prism-markdown' import 'prismjs/components/prism-sql' import 'prismjs/components/prism-css' import 'prismjs/components/prism-scss' import 'prismjs/components/prism-jsx' import 'prismjs/components/prism-tsx' export const SUPPORTED_LANGUAGES = [ { value: 'javascript', label: 'JavaScript' }, { value: 'typescript', label: 'TypeScript' }, { value: 'python', label: 'Python' }, { value: 'go', label: 'Go' }, { value: 'rust', label: 'Rust' }, { value: 'java', label: 'Java' }, { value: 'c', label: 'C' }, { value: 'cpp', label: 'C++' }, { value: 'csharp', label: 'C#' }, { value: 'ruby', label: 'Ruby' }, { value: 'php', label: 'PHP' }, { value: 'swift', label: 'Swift' }, { value: 'kotlin', label: 'Kotlin' }, { value: 'bash', label: 'Bash' }, { value: 'json', label: 'JSON' }, { value: 'yaml', label: 'YAML' }, { value: 'sql', label: 'SQL' }, { value: 'css', label: 'CSS' }, { value: 'scss', label: 'SCSS' }, { value: 'jsx', label: 'JSX' }, { value: 'tsx', label: 'TSX' }, { value: 'markdown', label: 'Markdown' }, { value: 'text', label: 'Plain Text' }, ] as const const languagePrismMap: Record = { javascript: 'javascript', typescript: 'typescript', python: 'python', go: 'go', rust: 'rust', java: 'java', c: 'c', cpp: 'cpp', csharp: 'csharp', ruby: 'ruby', php: 'php', swift: 'swift', kotlin: 'kotlin', bash: 'bash', json: 'json', yaml: 'yaml', sql: 'sql', css: 'css', scss: 'scss', jsx: 'jsx', tsx: 'tsx', markdown: 'markdown', text: 'text', } interface CodeSnippetEmbedProps { code: string language: string filePath?: string lineStart?: number lineEnd?: number repoOwner?: string repoName?: string theme?: 'dark' | 'light' maxHeight?: string showHeader?: boolean } export default function CodeSnippetEmbed({ code, language = 'text', filePath, lineStart, lineEnd, repoOwner, repoName, theme = 'dark', maxHeight = '400px', showHeader = true, }: CodeSnippetEmbedProps) { const codeRef = useRef(null) const [copied, setCopied] = useState(false) const prismLang = languagePrismMap[language] || 'text' const displayLang = SUPPORTED_LANGUAGES.find((l) => l.value === language)?.label || language.toUpperCase() useEffect(() => { if (codeRef.current) { Prism.highlightElement(codeRef.current) } }, [code, language]) const handleCopy = async () => { try { await navigator.clipboard.writeText(code) setCopied(true) setTimeout(() => setCopied(false), 2000) } catch (err) { console.error('Failed to copy code:', err) } } const lines = code.split('\n') const startLine = lineStart || 1 const isDark = theme === 'dark' const bgColor = isDark ? 'bg-[#0d1117]' : 'bg-[#f6f8fa]' const headerBg = isDark ? 'bg-[#161b22]' : 'bg-[#e1e4e8]' const borderColor = isDark ? 'border-gray-700' : 'border-gray-300' const textColor = isDark ? 'text-gray-400' : 'text-gray-600' const lineNumColor = isDark ? 'text-gray-500' : 'text-gray-400' const repoUrl = repoOwner && repoName ? `https://github.com/${repoOwner}/${repoName}` : null const fileUrl = repoUrl && filePath ? `${repoUrl}/blob/main/${filePath}${lineStart ? `#L${lineStart}${lineEnd ? `-L${lineEnd}` : ''}` : ''}` : null return (
{showHeader && (
{/* Terminal dots */}
{filePath ? ( {filePath} ) : ( {displayLang} )} {lineStart && lineEnd && ( L{lineStart}-{lineEnd} )}
{/* View in repository link */} {fileUrl && ( View )} {/* Copy button */}
)} {/* Code content */}
{/* Line numbers */}
{lines.map((_, i) => (
{startLine + i}
))}
{/* Code */}
            
              {code}
            
          
{/* Footer with repo link */} {repoOwner && repoName && ( )}
) } // Inline editor for inserting code snippets in compose interface CodeSnippetEditorProps { onInsert: (snippet: { code: string language: string filePath?: string lineStart?: number lineEnd?: number }) => void onCancel: () => void repoOwner?: string repoName?: string } export function CodeSnippetEditor({ onInsert, onCancel, repoOwner, repoName, }: CodeSnippetEditorProps) { const [code, setCode] = useState('') const [language, setLanguage] = useState('typescript') const [filePath, setFilePath] = useState('') const [lineStart, setLineStart] = useState('') const [lineEnd, setLineEnd] = useState('') const [showPreview, setShowPreview] = useState(false) const handleInsert = () => { if (!code.trim()) return onInsert({ code: code.trim(), language, filePath: filePath.trim() || undefined, lineStart: lineStart ? parseInt(lineStart) : undefined, lineEnd: lineEnd ? parseInt(lineEnd) : undefined, }) } return (

Insert Code Snippet

{/* Language selector */}
setFilePath(e.target.value)} placeholder="src/index.ts" className="w-full rounded-lg border border-border bg-background px-3 py-2 text-sm font-mono focus:outline-none focus:ring-2 focus:ring-primary" />
{/* Line range */}
setLineStart(e.target.value)} placeholder="1" min="1" className="w-full rounded-lg border border-border bg-background px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-primary" />
setLineEnd(e.target.value)} placeholder="50" min="1" className="w-full rounded-lg border border-border bg-background px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-primary" />
{/* Code input */}