|
| 1 | +import { useEffect, useState } from "react" |
| 2 | + |
| 3 | +// UI |
| 4 | +import { |
| 5 | + Table, |
| 6 | + TableBody, |
| 7 | + TableCell, |
| 8 | + TableHead, |
| 9 | + TableHeader, |
| 10 | + TableRow, |
| 11 | +} from "@/components/ui/table" |
| 12 | +import { ScrollArea } from "@/components/ui/scroll-area" |
| 13 | +import { Separator } from "../ui/separator" |
| 14 | +import { Button } from "@/components/ui/button" |
| 15 | +import LoadingState from "../ui/LoadingState" |
| 16 | +import { |
| 17 | + Select, |
| 18 | + SelectContent, |
| 19 | + SelectItem, |
| 20 | + SelectTrigger, |
| 21 | + SelectValue, |
| 22 | +} from "@/components/ui/select" |
| 23 | + |
| 24 | + |
| 25 | +// STORES |
| 26 | +import useAllFiles from "@/stores/backend/useAllFiles" |
| 27 | + |
| 28 | +// INTERFACES |
| 29 | +import { fetchedWorkspaces_interface } from "@/interfaces" |
| 30 | + |
| 31 | +// SERVICES |
| 32 | +import { fetchAllFiles } from "@/backend/services/files/fetchAllFiles" |
| 33 | + |
| 34 | +// ICONS |
| 35 | +import { FaRegFolderOpen } from "react-icons/fa"; |
| 36 | +import { Link } from "react-router-dom" |
| 37 | + |
| 38 | + |
| 39 | + |
| 40 | +export default function CTA_Main() { |
| 41 | + |
| 42 | + |
| 43 | + // Loading screen |
| 44 | + const [loadingFiles, setLoadingFiles] = useState<boolean>(true); |
| 45 | + |
| 46 | + // Collect Search Data |
| 47 | + const [searchData, setSearchData] = useState<{ |
| 48 | + searchBy: string, |
| 49 | + searchValue: string, |
| 50 | + }>({ |
| 51 | + searchBy: "fileId", |
| 52 | + searchValue: "", |
| 53 | + }); |
| 54 | + |
| 55 | + // Get all files |
| 56 | + const { allFiles } = useAllFiles(); |
| 57 | + |
| 58 | + async function initialize() { |
| 59 | + const getFiles = await fetchAllFiles(searchData); |
| 60 | + if (getFiles) { |
| 61 | + setLoadingFiles(false); |
| 62 | + } else { |
| 63 | + setLoadingFiles(false); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + const handleSearch = () => { |
| 68 | + setLoadingFiles(true); |
| 69 | + initialize(); |
| 70 | + }; |
| 71 | + |
| 72 | + useEffect(() => { |
| 73 | + initialize(); |
| 74 | + }, []); |
| 75 | + |
| 76 | + return ( |
| 77 | + <div className="flex-col space-y-4 max-w-[800px] mx-auto"> |
| 78 | + <Separator className="w-[500px] mx-auto" /> |
| 79 | + |
| 80 | + {/* START NEW WORKSPACE */} |
| 81 | + <div className="flex justify-between space-x-2"> |
| 82 | + |
| 83 | + {/* Search By */} |
| 84 | + <Select defaultValue="fileId" onValueChange={(value) => setSearchData({ ...searchData, searchBy: value })}> |
| 85 | + <SelectTrigger className="max-w-[200px] h-[40px] bg-white border-[1px] shadow-sm border-gray-200 rounded-md font-semibold"> |
| 86 | + <SelectValue placeholder="Search By" /> |
| 87 | + </SelectTrigger> |
| 88 | + <SelectContent> |
| 89 | + <SelectItem value="fileId">File ID <span className="text-gray-700">(recommended)</span></SelectItem> |
| 90 | + <SelectItem value="fileName">File Title</SelectItem> |
| 91 | + <SelectItem value="fileCreator">File Creator</SelectItem> |
| 92 | + </SelectContent> |
| 93 | + </Select> |
| 94 | + |
| 95 | + |
| 96 | + {/* Search Bar */} |
| 97 | + <input |
| 98 | + type="text" |
| 99 | + placeholder={`Search By ${searchData.searchBy === "fileId" ? "file ID" : searchData.searchBy === "fileName" ? "file title" : searchData.searchBy === "fileCreator" ? "Creator name" : 'file ID'}`.toLowerCase()} |
| 100 | + className="w-full h-[40px] px-4 border-[1px] shadow-sm border-gray-200 rounded-md capitalize" |
| 101 | + onChange={(e) => setSearchData({ ...searchData, searchValue: e.target.value })} |
| 102 | + /> |
| 103 | + |
| 104 | + <Button onClick={handleSearch} className="h-[40px] shadow-sm">Search</Button> |
| 105 | + |
| 106 | + {/* Vertical line for space */} |
| 107 | + <div className="w-[2px] h-[40px] bg-gray-200"></div> |
| 108 | + |
| 109 | + {/* Start New Workspace Btn */} |
| 110 | + <Button |
| 111 | + className="w-[200px] h-[40px] shadow-md bg-blue-500 hover:bg-blue-600 active:bg-blue-700 font-semibold text-center" |
| 112 | + > Start New Workspace ✨ |
| 113 | + </Button> |
| 114 | + </div> |
| 115 | + |
| 116 | + {/* WORKSPACES Table */} |
| 117 | + <div className="border-[1px] border-gray-200 rounded-md overflow-hidden p-1 bg-white"> |
| 118 | + |
| 119 | + {loadingFiles ? ( |
| 120 | + <div className="flex justify-center items-center w-full h-[270px]"> |
| 121 | + <LoadingState setWidth="50" /> |
| 122 | + <p className="text-gray-500 font-semibold">Loading files..</p> |
| 123 | + </div> |
| 124 | + ) : ( |
| 125 | + <> |
| 126 | + <Table> |
| 127 | + <TableHeader className="sticky top-0 bg-white z-10 uppercase font-semibold"> |
| 128 | + <TableRow> |
| 129 | + <TableHead className="w-[95px]">File ID</TableHead> |
| 130 | + <TableHead className="w-[100px]">Title</TableHead> |
| 131 | + <TableHead className="w-[100px]">Creator</TableHead> |
| 132 | + <TableHead className="w-[100px]">Latest Editor</TableHead> |
| 133 | + <TableHead className="w-[100px]">Last Update</TableHead> |
| 134 | + <TableHead className="w-[100px] text-right"></TableHead> |
| 135 | + </TableRow> |
| 136 | + </TableHeader> |
| 137 | + </Table> |
| 138 | + <ScrollArea className="h-[250px] bg-white"> |
| 139 | + <Table> |
| 140 | + <TableBody> |
| 141 | + {allFiles?.length > 0 ? allFiles?.map((workspace: fetchedWorkspaces_interface) => ( |
| 142 | + <TableRow key={workspace.id}> |
| 143 | + <TableCell className="w-[95px] max-w-[95px] font-medium text-ellipsis overflow-hidden whitespace-nowrap">{workspace.fileId}</TableCell> |
| 144 | + <TableCell className="w-[100px] max-w-[100px]">{workspace.fileName}</TableCell> |
| 145 | + <TableCell className="w-[100px] max-w-[100px] text-ellipsis overflow-hidden whitespace-nowrap">{workspace.fileCreator}</TableCell> |
| 146 | + <TableCell className="w-[100px] max-w-[100px] text-ellipsis overflow-hidden whitespace-nowrap">{workspace.fileLatestEditor === "" ? workspace.fileCreator : workspace.fileLatestEditor}</TableCell> |
| 147 | + <TableCell className="w-[100px] max-w-[100px] text-ellipsis overflow-hidden whitespace-nowrap"> |
| 148 | + {new Date(workspace.$updatedAt).toISOString().split('T')[0]} |
| 149 | + </TableCell> |
| 150 | + <TableCell className="w-[100px] max-w-[100px] text-right"> |
| 151 | + <Link to={`/${workspace.fileId}`}> |
| 152 | + <Button variant="default" size="sm" className="w-full flex justify-between items-center"> |
| 153 | + Open file |
| 154 | + <FaRegFolderOpen className="!size-4" /> |
| 155 | + </Button> |
| 156 | + </Link> |
| 157 | + </TableCell> |
| 158 | + </TableRow> |
| 159 | + )) : ( |
| 160 | + <div className="flex justify-center items-center w-full h-[250px]"> |
| 161 | + <span className="text-center mx-auto text-gray-500 font-semibold">No files found</span> |
| 162 | + </div> |
| 163 | + )} |
| 164 | + </TableBody> |
| 165 | + </Table> |
| 166 | + </ScrollArea> |
| 167 | + </> |
| 168 | + )} |
| 169 | + |
| 170 | + </div> |
| 171 | + </div> |
| 172 | + ) |
| 173 | +} |
| 174 | + |
0 commit comments