"use client"; import { formatDistanceToNow } from "date-fns"; import React, { useEffect, useState, useCallback } from "react"; import DocumentIcon from "@/components/Icons/Document"; import FolderIcon from "@/components/Icons/Folder"; import MenuIcon from "@/components/Icons/Menu"; import ContextMenuContainer from "./ContextMenu/container"; import RenamePopup from "./ContextMenu/rename"; import Pterodactyl from "@/components/Pterodactyl"; import BreadCrumbs from "./BreadCrumbs"; import { useSearchParams } from "next/navigation"; interface FileAttributes { name: string; modified_at: string; size: number; is_file: boolean; } interface FileProps { attributes: FileAttributes; } interface ContextMenuState { show: boolean; x: number; y: number; file: FileProps | null; } interface RenamePopupState { show: boolean; } const initialContextMenuState: ContextMenuState = { show: false, x: 0, y: 0, file: null, }; const initialRenamePopupState: RenamePopupState = { show: false, }; const Index = () => { const [apiKey, setApiKey] = useState(""); const [fileList, setFileList] = useState([]); const [contextMenu, setContextMenu] = useState( initialContextMenuState ); const [renamePopup, setRenamePopup] = useState( initialRenamePopupState ); const [selectedFile, setSelectedFile] = useState(null); const [ptero, setPtero] = useState(null); const [path, setPath] = useState("/"); const urlParams = useSearchParams(); const serverId = urlParams.get("serverid"); const setCredentials = useCallback(() => { setApiKey("ptlc_N77A2hEczFmSwGXm4cEXh4Gw3ZP0Ygr5NaBkGlE7pjU"); }, []); const showRenamePopup = useCallback(() => { setContextMenu(initialContextMenuState); setRenamePopup({ show: true }); }, []); const closeRenamePopup = useCallback(() => { setRenamePopup(initialRenamePopupState); }, []); const fetchFiles = useCallback(async (ptero: Pterodactyl) => { const files = await ptero.files.fetchFiles(); setFileList(files); }, []); const handleRenameFile = useCallback( async (file: FileProps, newName: string) => { if (ptero) { await ptero.files.rename(file, newName); await fetchFiles(ptero); setRenamePopup(initialRenamePopupState); } }, [ptero, fetchFiles] ); const handleClickContextMenu = useCallback( (e: React.MouseEvent, file: FileProps) => { e.preventDefault(); setContextMenu({ show: true, x: e.pageX - 100, y: e.pageY, file }); }, [] ); const handleContextMenuClose = useCallback(() => { setContextMenu(initialContextMenuState); }, []); const changeDirectory = useCallback( (newPath: string) => { if (ptero) { ptero.helpers.setWorkingDirectory(newPath); fetchFiles(ptero); setPath(newPath); } }, [ptero, fetchFiles] ); useEffect(() => { const setupApplication = async () => { await setCredentials(); if (apiKey && serverId) { const pteroInstance = new Pterodactyl(serverId, apiKey); pteroInstance.helpers.setWorkingDirectory(path); setPtero(pteroInstance); await fetchFiles(pteroInstance); } }; setupApplication(); }, [apiKey, serverId, setCredentials, path, fetchFiles]); return ( <> {renamePopup.show && ( )} {contextMenu.show && ( )} {serverId && (
)} {serverId && fileList.map((file: FileProps) => (
{file.attributes.is_file ? : }
{file.attributes.name}
{file.attributes.is_file ? `${file.attributes.size} bytes` : ""}
{formatDistanceToNow(new Date(file.attributes.modified_at), { addSuffix: true, })}
handleClickContextMenu(e, file)}>
))} ); }; export default Index;