'use client' import { useEffect, useState } from 'react' import { highlight } from 'sugar-high' import { getFileContent } from '~/utils/git' interface Props { path: string gitRef: string } export function FileViewer({ path, gitRef }: Props) { const [content, setContent] = useState(null) const [error, setError] = useState(null) const [loading, setLoading] = useState(true) useEffect(() => { setLoading(true) setError(null) getFileContent({ data: { ref: gitRef, path } }) .then((c: string) => { setContent(c) setLoading(false) }) .catch((e: Error) => { setError(e.message || 'Failed to load file') setLoading(false) }) }, [path, gitRef]) if (loading) { return (
Loading...
) } if (error) { return (
{error}
) } const isBinary = content !== null && content.includes('\x00') return (
{path} {content && ( {content.split('\n').length} lines )}
{isBinary ? (
Binary file
) : ( )}
) } function HighlightedCode({ code, filename, }: { code: string filename: string }) { const html = highlight(code) const lines = html.split('\n') return (
{lines.map((line, i) => ( ))}
{i + 1}
) }