Spaces:
Running
Running
File size: 5,612 Bytes
4d9392e 58bf1b1 4d9392e 58bf1b1 4d9392e 58bf1b1 4d9392e 58bf1b1 4d9392e 58bf1b1 4d9392e 58bf1b1 4d9392e 58bf1b1 4d9392e 58bf1b1 4d9392e 58bf1b1 4d9392e 58bf1b1 4d9392e 58bf1b1 4d9392e 58bf1b1 4d9392e 58bf1b1 4d9392e 58bf1b1 4d9392e 58bf1b1 4d9392e 58bf1b1 4d9392e 58bf1b1 4d9392e 58bf1b1 4d9392e |
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 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
// The directory browser.
import { useParams, useNavigate } from "react-router";
import { useState } from "react";
import useSWR from 'swr'
import logo from './assets/logo.png';
// @ts-ignore
import Home from '~icons/tabler/home'
// @ts-ignore
import Folder from '~icons/tabler/folder'
// @ts-ignore
import FolderPlus from '~icons/tabler/folder-plus'
// @ts-ignore
import File from '~icons/tabler/file'
// @ts-ignore
import FilePlus from '~icons/tabler/file-plus'
// @ts-ignore
import Trash from '~icons/tabler/trash';
const fetcher = (url: string) => fetch(url).then((res) => res.json());
export default function () {
const { path } = useParams();
const encodedPath = encodeURIComponent(path || '');
let list = useSWR(`/api/dir/list?path=${encodedPath}`, fetcher);
const navigate = useNavigate();
const [isCreatingDir, setIsCreatingDir] = useState(false);
const [isCreatingWorkspace, setIsCreatingWorkspace] = useState(false);
function link(item: any) {
if (item.type === 'directory') {
return `/dir/${item.name}`;
} else {
return `/edit/${item.name}`;
}
}
function shortName(item: any) {
return item.name.split('/').pop();
}
function newName(list: any[], baseName: string = "Untitled") {
let i = 0;
while (true) {
const name = `${baseName}${i ? ` ${i}` : ''}`;
if (!list.find(item => item.name === name)) {
return name;
}
i++;
}
}
function newWorkspaceIn(path: string, list: any[], workspaceName?: string) {
const pathSlash = path ? `${path}/` : "";
const name = workspaceName || newName(list);
navigate(`/edit/${pathSlash}${name}`, {replace: true});
}
async function newFolderIn(path: string, list: any[], folderName?: string) {
const name = folderName || newName(list, "New Folder");
const pathSlash = path ? `${path}/` : "";
const res = await fetch(`/api/dir/mkdir`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ path: pathSlash + name }),
});
list = await res.json();
if (res.ok) {
navigate(`/dir/${pathSlash}${name}`);
} else {
alert("Failed to create folder.");
}
}
async function deleteItem(item: any) {
if (!window.confirm(`Are you sure you want to delete "${item.name}"?`)) return;
const pathSlash = path ? `${path}/` : "";
const apiPath = item.type === "directory" ? `/api/dir/delete`: `/api/delete`;
const res = await fetch(apiPath, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ path: pathSlash + item.name }),
});
list = await res.json();
}
return (
<div className="directory">
<div className="logo">
<a href="https://lynxkite.com/">
<img src={logo} className="logo-image" alt="LynxKite logo" />
</a>
<div className="tagline">The Complete Graph Data Science Platform</div>
</div>
<div className="entry-list">
{list.error && <p className="error">{list.error.message}</p>}
{list.isLoading && (
<div className="loading spinner-border" role="status">
<span className="visually-hidden">Loading...</span>
</div>
)}
{list.data && (
<>
<div className="actions">
<div className="new-workspace">
{isCreatingWorkspace &&
// @ts-ignore
<form onSubmit={(e) => {e.preventDefault(); newWorkspaceIn(path || "", list.data, e.target.workspaceName.value.trim())}}>
<input
type="text"
name="workspaceName"
defaultValue={newName(list.data)}
placeholder={newName(list.data)}
/>
</form>
}
<button type="button" onClick={() => setIsCreatingWorkspace(true)}>
<FolderPlus /> New workspace
</button>
</div>
<div className="new-folder">
{isCreatingDir &&
// @ts-ignore
<form onSubmit={(e) =>{e.preventDefault(); newFolderIn(path || "", list.data, e.target.folderName.value.trim())}}>
<input
type="text"
name="folderName"
defaultValue={newName(list.data)}
placeholder={newName(list.data)}
/>
</form>
}
<button type="button" onClick={() => setIsCreatingDir(true)}>
<FolderPlus /> New folder
</button>
</div>
</div>
{path && (
<div className="breadcrumbs">
<a href="/dir/">
<Home />
</a>{" "}
<span className="current-folder">{path}</span>
</div>
)}
{list.data.map((item: any) => (
<div key={item.name} className="entry">
<a key={link(item)} className="entry" href={link(item)}>
{item.type === 'directory' ? <Folder /> : <File />}
{shortName(item)}
</a>
<button onClick={() => { deleteItem(item) }}>
<Trash />
</button>
</div>
))}
</>
)}
</div>
</div>
);
}
|