import { useState } from "react"; // The directory browser. import { Link, useNavigate } from "react-router"; import useSWR from "swr"; import type { DirectoryEntry } from "./apiTypes.ts"; import { usePath } from "./common.ts"; // @ts-ignore import File from "~icons/tabler/file"; // @ts-ignore import FilePlus from "~icons/tabler/file-plus"; // @ts-ignore import Folder from "~icons/tabler/folder"; // @ts-ignore import FolderPlus from "~icons/tabler/folder-plus"; // @ts-ignore import Home from "~icons/tabler/home"; // @ts-ignore import LayoutGrid from "~icons/tabler/layout-grid"; // @ts-ignore import LayoutGridAdd from "~icons/tabler/layout-grid-add"; // @ts-ignore import Trash from "~icons/tabler/trash"; import logo from "./assets/logo.png"; function EntryCreator(props: { label: string; icon: JSX.Element; onCreate: (name: string) => void; }) { const [isCreating, setIsCreating] = useState(false); return ( <> {isCreating ? (
) : ( )} > ); } const fetcher = (url: string) => fetch(url).then((res) => res.json()); export default function Directory() { const path = usePath().replace(/^[/]$|^[/]dir$|^[/]dir[/]/, ""); const encodedPath = encodeURIComponent(path || ""); const list = useSWR(`/api/dir/list?path=${encodedPath}`, fetcher, { dedupingInterval: 0, }); const navigate = useNavigate(); function link(item: DirectoryEntry) { if (item.type === "directory") { return `/dir/${item.name}`; } if (item.type === "workspace") { return `/edit/${item.name}`; } return `/code/${item.name}`; } function shortName(item: DirectoryEntry) { return item.name .split("/") .pop() ?.replace(/[.]lynxkite[.]json$/, ""); } function newWorkspaceIn(path: string, workspaceName: string) { const pathSlash = path ? `${path}/` : ""; navigate(`/edit/${pathSlash}${workspaceName}.lynxkite.json`, { replace: true }); } function newCodeFile(path: string, name: string) { const pathSlash = path ? `${path}/` : ""; navigate(`/code/${pathSlash}${name}`, { replace: true }); } async function newFolderIn(path: string, folderName: string) { const pathSlash = path ? `${path}/` : ""; const res = await fetch("/api/dir/mkdir", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ path: pathSlash + folderName }), }); if (res.ok) { navigate(`/dir/${pathSlash}${folderName}`); } else { alert("Failed to create folder."); } } async function deleteItem(item: DirectoryEntry) { if (!window.confirm(`Are you sure you want to delete "${item.name}"?`)) return; const apiPath = item.type === "directory" ? "/api/dir/delete" : "/api/delete"; await fetch(apiPath, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ path: item.name }), }); } return ({list.error.message}
} {list.isLoading && ( )} {list.data && ( <>