about summary refs log tree commit diff
path: root/src/pages/Search.tsx
blob: 0d85cb7fc3cd009672a79a43aeff2cde0fac20b7 (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
import { useEffect, useState, useCallback } from "react"

import { useNavigate } from "react-router-dom"
import SideBar from "../components/Sidebar/Sidebar"
import { FSTracerFile } from "../lib/types";
import FileComponent from "../components/Other/FSTracerFile";

export default function Search(props: any) {
  const navigate = useNavigate()

  const numOfFilesToShow = 20;

  const [files, setFiles] = useState([])
  const [search, setSearch] = useState("")

  const [paginationOffset, setPaginationOffset] = useState(0);

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

  const fetchFiles = useCallback(async () => {
    console.log("FETCHIN FILES, pagination: ", paginationOffset, " search: ", search)
    const { data, error } = await props.supabase
      .from('file')
      .select()
      .ilike('absolute_path', `%${search}%`)
      .order('timestamp', { ascending: false })
      .range(paginationOffset, paginationOffset + numOfFilesToShow - 1)
    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, paginationOffset, search])

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

  return (
    <>
      <div className="flex h-screen">
        <SideBar currentPage="Recent" />
        <main className="flex-1 overflow-y-auto my-4">
          <div className="flex flex-col items-center">
            <label>filename</label>
            <input placeholder="filename" onChange={e => setSearch(e.target.value)} className="block bg-white w-1/2 rounded">
            </input>
          </div>
          <div className="my-4 flex flex-col items-center">
            <button className="w-20 bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded" onClick={() => fetchFiles()}>
              search
            </button>
          </div>
          <div className="flex flex-col items-center">
            <div className="flex flex-col gap-2">
              {files.map((file: FSTracerFile) => (
                <div key={file.id} role="button">
                  <FileComponent file={file} />
                </div>
              ))
              }
            </div>
          </div>
          <div className="flex flex-row gap-2 mt-4">
            <button className="ml-7 bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded" onClick={() => setPaginationOffset(paginationOffset + -numOfFilesToShow)}>
              prev
            </button>
            <button className="mr-7 ml-auto bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded" onClick={() => setPaginationOffset(paginationOffset + numOfFilesToShow)}>
              next
            </button>
          </div>
        </main>
      </div>
    </>
  )
}