102 lines · 2.5 KB
'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<string | null>(null)
  const [error, setError] = useState<string | null>(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 (
      <div className="flex h-full items-center justify-center text-gray-600">
        Loading...
      </div>
    )
  }

  if (error) {
    return (
      <div className="flex h-full items-center justify-center text-red-500">