loading file into text editor

This commit is contained in:
2024-09-13 21:00:08 +00:00
parent 437701ffac
commit faa45f7fcf
6 changed files with 97 additions and 36 deletions

View File

@@ -1,21 +1,35 @@
"use client"; "use client";
import React, { useCallback } from "react"; import React, { useEffect, useState } from "react";
import { useSearchParams } from "next/navigation"; import { useSearchParams } from "next/navigation";
import TextEditor from "@/components/TextEditor"; import TextEditor from "@/components/TextEditor";
import BreadCrumbs from "@/components/BreadCrumbs"; import BreadCrumbs from "@/components/BreadCrumbs";
import ServerIcon from "@/components/Icons/Server"; import ServerIcon from "@/components/Icons/Server";
import FolderIcon from "@/components/Icons/Folder"; import FolderIcon from "@/components/Icons/Folder";
import Pterodactyl from "@/components/Pterodactyl";
import Panel from "@/components/Panel";
function Page() { function Page() {
const searchParams = useSearchParams(); const searchParams = useSearchParams();
const serverId = searchParams.get("serverid") || ""; const serverId = searchParams.get("serverid") || "";
const path = searchParams.get("path") || ""; const path = searchParams.get("path") || "";
const [code, setCode] = useState("dupa");
const pterodactyl = new Pterodactyl();
const changeDirectory = useCallback((path: string) => { useEffect(() => {
console.log("changeDirectory", path); const startupApp = async () => {
}, []); const panelApp = new Panel();
const credentials = await panelApp.getCredentials();
pterodactyl.setApiKey(credentials.api_key);
pterodactyl.setServerId(serverId);
setCode(await pterodactyl.files.getContent(path));
};
if (serverId && path) {
startupApp();
}
}, [serverId, path]);
return ( return (
<> <>
@@ -54,7 +68,7 @@ function Page() {
); );
})} })}
</BreadCrumbs> </BreadCrumbs>
<TextEditor />; <TextEditor code={code} />
</> </>
); );
} }

View File

@@ -57,7 +57,7 @@ const Index = () => {
initialRenamePopupState initialRenamePopupState
); );
const [selectedFile, setSelectedFile] = useState<FileProps | null>(null); const [selectedFile, setSelectedFile] = useState<FileProps | null>(null);
const [ptero, setPtero] = useState<Pterodactyl | null>(null); const pterodactyl = useMemo(() => new Pterodactyl(), []);
const urlParams = useSearchParams(); const urlParams = useSearchParams();
const serverId = urlParams.get("serverid"); const serverId = urlParams.get("serverid");
const pathParam = urlParams.get("path") || "/"; const pathParam = urlParams.get("path") || "/";
@@ -71,21 +71,21 @@ const Index = () => {
setRenamePopup(initialRenamePopupState); setRenamePopup(initialRenamePopupState);
}, []); }, []);
const fetchFiles = useCallback(async (ptero: Pterodactyl) => { const fetchFiles = useCallback(async () => {
console.log("chwytam pliki"); console.log("chwytam pliki");
const files = await ptero.files.fetchFiles(); const files = await pterodactyl.files.fetchFiles();
setFileList(files); setFileList(files);
}, []); }, [pterodactyl]);
const handleRenameFile = useCallback( const handleRenameFile = useCallback(
async (file: FileProps, newName: string) => { async (file: FileProps, newName: string) => {
if (ptero) { if (pterodactyl) {
await ptero.files.rename(file, newName); await pterodactyl.files.rename(file, newName);
await fetchFiles(ptero); await fetchFiles();
setRenamePopup(initialRenamePopupState); setRenamePopup(initialRenamePopupState);
} }
}, },
[ptero, fetchFiles] [pterodactyl, fetchFiles]
); );
const handleClickContextMenu = useCallback( const handleClickContextMenu = useCallback(
@@ -102,29 +102,24 @@ const Index = () => {
const changeDirectory = useCallback( const changeDirectory = useCallback(
(newPath: string) => { (newPath: string) => {
if (ptero) { if (pterodactyl) {
ptero.helpers.setWorkingDirectory(newPath); pterodactyl.helpers.setWorkingDirectory(newPath);
fetchFiles(ptero); fetchFiles();
// setPath(newPath); // setPath(newPath);
} }
}, },
[ptero, fetchFiles] [pterodactyl, fetchFiles]
); );
useEffect(() => { useEffect(() => {
const setupApplication = () => {
if (!apiKey || !serverId) return; // Upewniamy się, że mamy apiKey i serverId if (!apiKey || !serverId) return; // Upewniamy się, że mamy apiKey i serverId
if (!ptero) {
const pteroInstance = new Pterodactyl(serverId, apiKey);
setPtero(pteroInstance);
pteroInstance.helpers.setWorkingDirectory(pathParam); pterodactyl.setApiKey(apiKey);
fetchFiles(pteroInstance); // Wywołanie fetchFiles raz po ustawieniu instancji pterodactyl.setServerId(serverId);
} pterodactyl.helpers.setWorkingDirectory(pathParam);
};
setupApplication(); fetchFiles(); // Wywołanie fetchFiles raz po ustawieniu instancji
}, [apiKey, serverId, ptero, pathParam, fetchFiles]); }, [apiKey, serverId, pterodactyl, pathParam, fetchFiles]);
return ( return (
<> <>

View File

@@ -0,0 +1,16 @@
interface Credentials {
api_key: string;
}
class Panel {
constructor() {}
// this method is responsible for fetching credentials from the server
async getCredentials(): Promise<Credentials> {
return {
api_key: `${process.env.NEXT_PUBLIC_API_KEY}`,
};
}
}
export default Panel;

View File

@@ -1,3 +1,4 @@
import { get } from "http";
import { File } from "./interfaces"; import { File } from "./interfaces";
export default function files(pterodactyl: any) { export default function files(pterodactyl: any) {
@@ -57,5 +58,24 @@ export default function files(pterodactyl: any) {
console.error("Error fetching data:", error); console.error("Error fetching data:", error);
} }
}, },
async getContent(filename: string) {
try {
const response = await fetch(
`${process.env.NEXT_PUBLIC_URL}/api/client/servers/${pterodactyl.server_id}/files/contents?file=${filename}`,
{
method: "GET",
headers: await pterodactyl.helpers.authHeader(),
}
);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.text();
return data;
} catch (error) {
console.error("Error fetching data:", error);
}
},
}; };
} }

View File

@@ -2,15 +2,21 @@ import filesModule from "./files";
import helpersModule from "./helpers"; import helpersModule from "./helpers";
class Pterodactyl { class Pterodactyl {
server_id: string; server_id: string = "";
api_key: string; api_key: string = "";
files: any; files: any;
helpers: any; helpers: any;
workingDirectory: string = "/"; workingDirectory: string = "/";
constructor(server_id: string, api_key: string) { setApiKey(api_key: string) {
this.server_id = server_id;
this.api_key = api_key; this.api_key = api_key;
}
setServerId(server_id: string) {
this.server_id = server_id;
}
constructor() {
this.files = filesModule(this); this.files = filesModule(this);
this.helpers = helpersModule(this); this.helpers = helpersModule(this);
} }

View File

@@ -1,4 +1,4 @@
import { useState } from "react"; import { useState, useEffect } from "react";
import MonacoEditor, { OnChange } from "@monaco-editor/react"; import MonacoEditor, { OnChange } from "@monaco-editor/react";
interface EditorProps { interface EditorProps {
@@ -6,11 +6,14 @@ interface EditorProps {
width?: string; width?: string;
theme?: string; theme?: string;
language?: string; language?: string;
code?: string;
onChange?: OnChange; onChange?: OnChange;
} }
const Editor = (props: EditorProps) => { const Editor = (props: EditorProps) => {
const [code, setCode] = useState<string>('console.log("Hello, Monaco!");'); const [code, setCode] = useState<string>(
props.code || 'console.log("Hello, Monaco!");'
);
const handleEditorChange: OnChange = (value) => { const handleEditorChange: OnChange = (value) => {
setCode(value || ""); setCode(value || "");
@@ -21,16 +24,23 @@ const Editor = (props: EditorProps) => {
width = "100%", width = "100%",
theme = "vs-dark", theme = "vs-dark",
language = "typescript", language = "typescript",
code: _code = code, // z props, jeśli jest dostępne
onChange = handleEditorChange, onChange = handleEditorChange,
} = props; } = props;
// Efekt do synchronizacji props.code z wewnętrznym stanem code
useEffect(() => {
if (_code !== undefined) {
setCode(_code);
}
}, [_code]);
return ( return (
<MonacoEditor <MonacoEditor
height={height} height={height}
width={width} width={width}
language={language} language={language}
theme={theme} theme={theme}
defaultValue={code}
value={code} value={code}
onChange={onChange} onChange={onChange}
/> />