Prepare filesystem explorer

This commit is contained in:
momo5502
2025-04-29 20:57:20 +02:00
parent 2fce53b3e7
commit da0cd03c57
9 changed files with 709 additions and 75 deletions

View File

@@ -0,0 +1,79 @@
import React from "react";
import { Folder, FolderElement, FolderElementType } from "./components/folder";
import { Filesystem } from "./filesystem";
export interface FilesystemExplorerProps {
filesystem: Filesystem;
}
export interface FilesystemExplorerState {
path: string[];
}
function getFolderElements(filesystem: Filesystem, path: string[]) {
const fullPath = "/root/filesys/" + path.join("/");
const files = filesystem.readDir(fullPath);
return files
.filter((f) => {
if (f == ".") {
return false;
}
if (path.length == 0 && f == "..") {
return false;
}
return true;
})
.map((f) => {
const element: FolderElement = {
name: f,
type: filesystem.isFolder(`${fullPath}/${f}`)
? FolderElementType.Folder
: FolderElementType.File,
};
return element;
});
}
export class FilesystemExplorer extends React.Component<
FilesystemExplorerProps,
FilesystemExplorerState
> {
constructor(props: FilesystemExplorerProps) {
super(props);
this._onElementSelect = this._onElementSelect.bind(this);
this.state = {
path: [],
};
}
_onElementSelect(element: FolderElement) {
if (element.type != FolderElementType.Folder) {
return;
}
this.setState((s) => {
const path = [...s.path];
if (element.name == "..") {
path.pop();
} else {
path.push(element.name);
}
return {
path,
};
});
}
render() {
const elements = getFolderElements(this.props.filesystem, this.state.path);
return <Folder elements={elements} clickHandler={this._onElementSelect} />;
}
}