summaryrefslogtreecommitdiff
path: root/frontend/src/components/FileDetail.tsx
blob: 31228efcbcfb9671b8f9f10f9d97db504d3e4fcf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import React, { useEffect, useState } from 'react'
import { useParams, Link } from 'react-router-dom'

interface File {
  id: string
  name: string
  description?: string
  body?: string
  contract_id?: string
  version: number
  created_at: string
}

export function FileDetail() {
  const { contractId, fileId } = useParams<{ contractId: string; fileId: string }>()
  const [file, setFile] = useState<File | null>(null)
  const [loading, setLoading] = useState(true)
  const [error, setError] = useState<string | null>(null)

  useEffect(() => {
    async function fetchFile() {
      if (!fileId) return

      try {
        setLoading(true)
        const response = await fetch(`/api/v1/files/${fileId}`)
        if (!response.ok) {
          throw new Error(`Failed to fetch file: ${response.statusText}`)
        }
        const data = await response.json()
        setFile(data)
      } catch (err) {
        setError(err instanceof Error ? err.message : 'Unknown error')
      } finally {
        setLoading(false)
      }
    }

    fetchFile()
  }, [fileId])

  if (loading) {
    return (
      <div className="file-detail-container">
        <div className="loading">Loading file...</div>
      </div>
    )
  }

  if (error) {
    return (
      <div className="file-detail-container">
        <div className="error">Error: {error}</div>
        <Link to={`/contracts/${contractId}`} className="back-link">
          Back to Contract
        </Link>
      </div>
    )
  }

  if (!file) {
    return (
      <div className="file-detail-container">
        <div className="not-found">File not found</div>
        <Link to={`/contracts/${contractId}`} className="back-link">
          Back to Contract
        </Link>
      </div>
    )
  }

  return (
    <div className="file-detail-container">
      <div className="file-detail-header">
        <Link to={`/contracts/${contractId}`} className="back-link">
          Back to Contract
        </Link>
        <h1 className="file-title">{file.name}</h1>
        {file.description && (
          <p className="file-description">{file.description}</p>
        )}
        <div className="file-meta">
          <span>Version: {file.version}</span>
          <span>Created: {new Date(file.created_at).toLocaleString()}</span>
        </div>
      </div>

      <div className="file-detail-body">
        {file.body ? (
          <pre className="file-content">{file.body}</pre>
        ) : (
          <p className="no-content">No content</p>
        )}
      </div>
    </div>
  )
}