about summary refs log tree commit diff
path: root/src/pages/File.tsx
blob: 5a69d01f3be648cd669bb75b8f1c489614973aa5 (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
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 { fileID } = useParams();

  console.log("FILEID: ", fileID)

  const [files, setFiles] = useState([])
  const [file, setFile] = useState<FSTracerFile>()

  const maxFilesToShow = 20;

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

  const fetchFiles = useCallback(async () => {
    console.log("FETCHING FILE")
    const { data: data1, error: err1 } = await props.supabase
      .from('file')
      .select()
      .eq('id', fileID)
    if (err1) {
      console.error(err1)
      return
    }

    if (data1.length === 0) {
      return
    }

    let fetchedFile = data1[0] as FSTracerFile;
    setFile(fetchedFile)

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

  useEffect(() => {
    fetchFiles()
  }, [props.supabase])

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

  const restoreFile = async (file: FSTracerFile) => {
    console.log("RESTORING FILE: user_id: ", props.session.user.id, " file: ", file.absolute_path, " ", file.contents, " ", file.timestamp)
    await props.supabase.from('restored_file').insert({ absolute_path: file.absolute_path, contents: file.contents, timestamp: file.timestamp, user_id: props.session.user.id })
  }

  return (
    <>
      <div className="flex h-screen">
        <SideBar />
        <main className="flex-1 overflow-y-auto my-4">
          {fileID && file !== undefined ? <>
            <div className="flex flex-col items-center">
              {
                formatFilePathAsName(file.absolute_path)
              }
            </div>
            <div className="mt-5 flex flex-col items-center">
              <div className="block border rounded shadowpy-5 px-5 bg-blue">
                <FileInfo file={file} />
              </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((currFile: FSTracerFile) => (
                  <div key={currFile.id}>
                    <a href={"/file/" + currFile.id}>{currFile.absolute_path} - {currFile.timestamp} {currFile.id === file.id && "*"}</a><button onClick={() => { restoreFile(currFile) }}><span>&nbsp;&nbsp;&nbsp;&nbsp;</span><b>Restore</b></button>
                  </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 style={{ whiteSpace: "pre-wrap" }}>Content: {props.file.contents}</p>
      <p>ID: {props.file.id}</p>
    </>
  )
}