import React, { useState, useEffect } from "react"; import PropTypes from "prop-types"; const FileTable = ({ initialPath }) => { const [currentPath, setCurrentPath] = useState(initialPath || "/"); const [files, setFiles] = useState([]); // Helpers to parse entry const getType = (entry) => entry.trim().startsWith("📁") ? "Folder" : "File"; const getName = (entry) => entry.trim().replace(/^📁\s*|^📄\s*/, ""); const isFile = (entry) => getType(entry) === "File"; // Fetch and show only top-level entries (indentation = 0) const fetchFiles = async () => { try { const response = await fetch( `http://192.168.29.61:8080/api/hdfs/listFiles?hdfsPath=${currentPath}` ); const data = await response.json(); // Filter entries: only those without leading spaces const filtered = data.filter( (entry) => entry.match(/^ */)[0].length === 0 ); setFiles(filtered); } catch (error) { console.error("Failed to fetch files:", error); setFiles([]); } }; useEffect(() => { fetchFiles(); }, [currentPath]); // Navigate into a folder const handleOpenFolder = (folderName) => { const newPath = currentPath === "/" ? `/${folderName}` : `${currentPath}/${folderName}`; setCurrentPath(newPath); }; // Go up one level const goBack = () => { if (currentPath === "/") return; const parts = currentPath.split("/").filter(Boolean); parts.pop(); setCurrentPath(parts.length === 0 ? "/" : `/${parts.join("/")}`); }; return (
| File Name | Type | Action |
|---|---|---|
| No files found. | ||
| {name} | {type} | {isFile(entry) ? ( Download ) : ( )} |