about summary refs log tree commit diff
path: root/src/pages/File.tsx
blob: bbbdac236fb98d9f00225f63d2e8c0220fc350e9 (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
98
99
100
101
102
103
import { useNavigate, useParams } from "react-router-dom"
import SideBar from "../components/Sidebar/Sidebar"
import { useCallback, useEffect, useState } from "react";
import { FSTracerFile } from "../lib/types";

export default function File(props: any) {
  const navigate = useNavigate()
  let { filepath } = useParams();

  console.log("FILEPATH: ", filepath)

  const [files, setFiles] = useState([])

  const latestFile = files[0] as FSTracerFile | undefined

  const maxFilesToShow = 20;

  useEffect(() => {
    if (!props.session) {
      navigate('/login')
    }
  }, [props.session])

  const fetchFiles = useCallback(async () => {
    console.log("FETCHIN FILES")
    const { data, error } = await props.supabase
      .from('file')
      .select()
      .eq('absolute_path', filepath)
      .order('timestamp', { ascending: false })
      .range(0, maxFilesToShow)
    if (error) {
      console.error(error)
      return
    }
    console.log("RAW FILES: ", data)
    setFiles(data.map((file: any) => {
      return file as File
    }))
    console.log("FETCHED FILES")
  }, [props.supabase])

  useEffect(() => {
    fetchFiles()
  }, [])

  const formatFilePathAsName = (filepath: string) => {
    const parts = filepath.split('/')
    return parts[parts.length - 1]
  }

  return (
    <>
      <div className="flex h-screen">
        <SideBar />
        <main className="flex-1 overflow-y-auto my-4">
          {filepath && latestFile ? <>
            <div className="flex flex-col items-center">
              {
                formatFilePathAsName(filepath)
              }
            </div>
            <div className="mt-5 flex flex-col items-center">
              <div className="block border rounded shadowpy-5 px-5 bg-blue">
                {latestFile && <FileInfo file={latestFile} />}
              </div>
            </div>
            <div className="mt-5 flex flex-col items-center">
              <div className="block border rounded shadow py-5 px-5 bg-blue">
                {files.map((file: FSTracerFile) => (
                  <div key={file.id}>
                    <p>{file.absolute_path} - {file.timestamp}</p>
                  </div>
                ))
                }
              </div>
            </div>
          </> : <>
            <div className="flex flex-col items-center">
              <p>File not found</p>
            </div>
          </>
          }
        </main>
      </div>
    </>
  )
}

interface FileInfoProps {
  file: FSTracerFile
}

function FileInfo(props: FileInfoProps) {
  return (
    <>
      <p>Absolute path: {props.file.absolute_path}</p>
      <p>Timestamp: {props.file.timestamp}</p>
      <p>Content: {props.file.contents}</p>
      <p>ID: {props.file.id}</p>
    </>
  )
}