import { useMemo } from 'react'
import hljs from 'highlight.js'
import { Copy, Download } from 'lucide-react'
import { Button } from '@/components/ui/button'
import { Skeleton } from '@/components/ui/skeleton'
import { getRawUrl } from '@/lib/gitApi'
import type { GitBlob } from '@/lib/gitApi'
interface FileViewerProps {
blob: GitBlob
ref: string
loading?: boolean
}
// Syntax-highlighted file viewer with line numbers, copy, and download buttons.
// Uses highlight.js for highlighting; binary files show a placeholder.
export function FileViewer({ blob, ref, loading }: FileViewerProps) {
const { html, lineCount } = useMemo(() => {
if (blob.isBinary || !blob.content) return { html: '', lineCount: 0 }
const ext = blob.path.split('.').pop() ?? ''
const result = hljs.getLanguage(ext)
? hljs.highlight(blob.content, { language: ext })
: hljs.highlightAuto(blob.content)
return {
html: result.value,
lineCount: blob.content.split('\n').length,
}
}, [blob])
if (loading) return