import { type CSSProperties, type ReactNode } from 'react' import { useCopyToClipboard } from '@/hooks/useCopyToClipboard' import { useCodeWrap } from '@/hooks/useCodeWrap' import { useShikiHighlightedLines, splitCodeLines } from '@/lib/shiki' import { CopyIcon, CheckIcon, WrapIcon } from '@/components/icons' import { useTranslation } from '@/lib/use-translation' const DEFAULT_COLLAPSE_LINE_THRESHOLD = 18 const DEFAULT_COLLAPSE_CHAR_THRESHOLD = 1800 const DEFAULT_COLLAPSED_HEIGHT = 260 const DEFAULT_SCROLL_HEIGHT = 420 const GUTTER_HORIZONTAL_PADDING_REM = 1.5 function shouldCollapseCode(code: string, lineThreshold: number, charThreshold: number): boolean { if (code.length > charThreshold) return true return code.split('\n').length > lineThreshold } function formatCodeLabel(language?: string, title?: string): string { if (title && title.trim().length > 0) return title if (!language || language === 'unknown') return 'Code' return language } export function CodeBlock(props: { code: string language?: string title?: string showCopyButton?: boolean showWrapToggle?: boolean collapseLongContent?: boolean collapsedHeight?: number maxHeight?: number scrollY?: boolean size?: 'compact' | 'comfortable' collapseLineThreshold?: number collapseCharThreshold?: number /** * First line's number in the gutter (default 1). Set to the true starting * line when showing a partial slice of a file (e.g. a Read of lines 50–100) * so the gutter isn't misleadingly numbered from 1. */ startLineNumber?: number }) { const { t } = useTranslation() const showCopyButton = props.showCopyButton ?? true // The wrap toggle is a ) : null} {showCopyButton ? ( ) : null}
                    {codeLines.map((line, index) => {
                        // py lives on the first/last row cells (not the container)
                        // so the gutter background reaches the top and bottom edges.
                        const rowPad = `${index === 0 ? 'pt-3' : ''} ${index === codeLines.length - 1 ? 'pb-3' : ''}`
                        return (
                            
                                
                                
                                    {line}
                                
                            
                        )
                    })}
                
{isCollapsed ? (
{t('code.truncated')}
) : null} ) }