mirror of
https://github.com/pterodactyl/panel.git
synced 2026-12-01 03:45:53 -05:00
272 lines
13 KiB
TypeScript
272 lines
13 KiB
TypeScript
import { useMemo, type ComponentType } from 'react';
|
|
import type { ClientFileObjectResource as FileObject } from '@/api/extensionTypes';
|
|
import { fileObjectKind, useServerFiles } from '@/api/server/files/queries';
|
|
import { useCurrentServerUuid } from '@/api/server/queries';
|
|
import { useFileEditorPage, useFileEditorSession } from '@/components/server/files/fileEditorSession';
|
|
import useFileManagerModel from '@/components/server/files/useFileManagerModel';
|
|
import type { FileManagerSlotData } from '@/extensions/registry';
|
|
import type { CorePanelDestination } from '@/sdk/navigation';
|
|
import { useServerDirectory } from '@/state/server';
|
|
import { implementViews, modelFromInput } from '../registry';
|
|
import {
|
|
createTestFileDetails,
|
|
createTestFileEditor,
|
|
createTestFileEditorPage,
|
|
createTestFileManager,
|
|
createTestFileObject,
|
|
} from './fileFixtures';
|
|
import { pageStatus, pageStatusFixtures, type PageStatusModel } from '../pageStatus';
|
|
|
|
export interface FileDetailsModel {
|
|
readonly name: string;
|
|
readonly kind: 'file' | 'directory' | 'archive' | 'symlink';
|
|
readonly size: number;
|
|
readonly modifiedAt: string;
|
|
}
|
|
|
|
/** A step of the path above a file listing or the editor. */
|
|
export interface FileBreadcrumb {
|
|
readonly name: string;
|
|
/** The directory's absolute path. */
|
|
readonly path: string;
|
|
/** Null for the directory being listed. */
|
|
readonly destination: CorePanelDestination | null;
|
|
}
|
|
|
|
export interface FileEditorPageModel {
|
|
/** "container" (the server's root), then each directory down to the open file's. */
|
|
readonly breadcrumbs: readonly FileBreadcrumb[];
|
|
/** The open file's name; null for a new file. */
|
|
readonly file: string | null;
|
|
/** The file's content is still loading. */
|
|
readonly loading: boolean;
|
|
/** The file could not be loaded, so the page shows this message instead. */
|
|
readonly error: string | null;
|
|
readonly back: () => void;
|
|
}
|
|
|
|
export interface FileEditorCanvasProps {
|
|
readonly className?: string;
|
|
}
|
|
/** Core loads the document, tracks the reported buffer, writes it, and guards unsaved changes. */
|
|
export interface FileEditorModel {
|
|
/** Absolute path of the open file; the target directory while a new file has no name. */
|
|
readonly path: string;
|
|
/** File name, or an empty string for a new file. */
|
|
readonly name: string;
|
|
readonly isNew: boolean;
|
|
/** Text to open: the file as loaded, or the restored draft of a new file. */
|
|
readonly content: string;
|
|
/** Syntax hint as a MIME type, for example "text/x-yaml". */
|
|
readonly language: string;
|
|
/** Every language the editor highlights, for a picker. */
|
|
readonly languages: readonly { readonly mime: string; readonly name: string }[];
|
|
/** The user may not write this file; save and saveAs resolve false. */
|
|
readonly readOnly: boolean;
|
|
readonly dirty: boolean;
|
|
readonly saving: boolean;
|
|
/** The code editor, already bound to this document; it reports every edit and saves on Ctrl+S. */
|
|
readonly Editor: ComponentType<FileEditorCanvasProps>;
|
|
/** The buffer as last reported, which a remounted editor starts from. */
|
|
readonly read: () => string;
|
|
/** Report the buffer after every edit; core derives dirty from it and saves this text. */
|
|
readonly change: (content: string) => void;
|
|
readonly setLanguage: (language: string) => void;
|
|
/** Write the reported buffer in place; a new file asks for its name first. Resolves whether it was saved. */
|
|
readonly save: () => Promise<boolean>;
|
|
/** Write the reported buffer to a name beside the open file, or to an absolute path, and open it. */
|
|
readonly saveAs: (name: string) => Promise<boolean>;
|
|
}
|
|
|
|
export interface FileManagerEntry {
|
|
readonly name: string;
|
|
readonly path: string;
|
|
readonly kind: FileDetailsModel['kind'];
|
|
readonly size: number;
|
|
readonly mimetype: string;
|
|
/** Symbolic mode such as "-rw-r--r--". */
|
|
readonly mode: string;
|
|
/** Octal mode such as "0644". */
|
|
readonly modeBits: string;
|
|
readonly modifiedAt: string;
|
|
/** open() leads somewhere: a directory, or a text file this user may read. */
|
|
readonly openable: boolean;
|
|
/** An archive extract() can unpack, symbolic links to one included. */
|
|
readonly extractable: boolean;
|
|
/** Where opening it leads; null when it cannot be opened. */
|
|
readonly destination: CorePanelDestination | null;
|
|
/** The file as the API returns it, which `server.files.details` and the row slots receive. */
|
|
readonly file: FileObject;
|
|
}
|
|
export interface FileManagerPermissions {
|
|
readonly create: boolean;
|
|
readonly update: boolean;
|
|
readonly delete: boolean;
|
|
readonly archive: boolean;
|
|
}
|
|
export interface FileUpload {
|
|
readonly id: string;
|
|
readonly name: string;
|
|
/** Bytes sent so far, of `total`. */
|
|
readonly loaded: number;
|
|
readonly total: number;
|
|
}
|
|
/**
|
|
* What `upload` takes: picked files, or a drop's `event.dataTransfer`, which tells a dropped folder
|
|
* apart. Pass the drop from inside the drop handler: the browser empties it once the handler returns.
|
|
*/
|
|
export type UploadSource = readonly File[] | DataTransfer;
|
|
/** Names are entries of the current directory. Core has already shown a toast for a rejection. */
|
|
export interface FileManagerActions {
|
|
/** Enter a directory or open a text file in the editor. */
|
|
readonly open: (name: string) => Promise<void>;
|
|
/** Show another directory, given as an absolute path or relative to the current one. */
|
|
readonly navigate: (directory: string) => Promise<void>;
|
|
/** Open the editor for a new file in the current directory. */
|
|
readonly newFile: () => Promise<void>;
|
|
readonly select: (names: readonly string[]) => void;
|
|
readonly refresh: () => Promise<void>;
|
|
readonly createDirectory: (name: string) => Promise<void>;
|
|
/** Rename in place, or move by giving a path relative to the current directory; clears the selection. */
|
|
readonly rename: (files: readonly { readonly from: string; readonly to: string }[]) => Promise<void>;
|
|
/** Move several files into a directory relative to the current one; one file is renamed to it instead. */
|
|
readonly move: (names: readonly string[], to: string) => Promise<void>;
|
|
/** Delete without a confirmation, dropping the names from the selection; ask the user first. */
|
|
readonly remove: (names: readonly string[]) => Promise<void>;
|
|
readonly copy: (name: string) => Promise<void>;
|
|
/** Compress the names into one archive, dropping them from the selection. */
|
|
readonly archive: (names: readonly string[]) => Promise<void>;
|
|
readonly extract: (name: string) => Promise<void>;
|
|
/** Change modes; clears the selection. */
|
|
readonly chmod: (files: readonly { readonly file: string; readonly mode: string }[]) => Promise<void>;
|
|
readonly download: (name: string) => Promise<void>;
|
|
/** Refuses a drop holding a folder, which cannot be uploaded. */
|
|
readonly upload: (source: UploadSource) => Promise<void>;
|
|
readonly cancelUpload: (id: string) => void;
|
|
/** Cancel every upload in progress. */
|
|
readonly clearUploads: () => void;
|
|
}
|
|
/** Core queries the directory, owns the selection, and runs every mutation and navigation. */
|
|
export interface FileManagerModel {
|
|
readonly directory: string;
|
|
/** "container" (the server's root), then each directory down to the current one. */
|
|
readonly breadcrumbs: readonly FileBreadcrumb[];
|
|
/** Directories first, then files, by name; at most 250 entries. */
|
|
readonly entries: readonly FileManagerEntry[];
|
|
/** The directory holds more entries than the panel lists. */
|
|
readonly truncated: boolean;
|
|
/** The directory has not been listed yet. */
|
|
readonly loading: boolean;
|
|
readonly refreshing: boolean;
|
|
readonly selection: readonly string[];
|
|
readonly permissions: FileManagerPermissions;
|
|
/** The editor for a new file in the current directory. */
|
|
readonly newFileDestination: CorePanelDestination;
|
|
/** Uploads in progress, by name. */
|
|
readonly uploads: readonly FileUpload[];
|
|
/** A name relative to the current directory as the path it resolves to below /home/container/. */
|
|
readonly resolve: (name: string) => string;
|
|
readonly actions: FileManagerActions;
|
|
/** What the toolbar, row and selection slots receive. */
|
|
readonly slot: FileManagerSlotData;
|
|
}
|
|
|
|
declare module '../registry' {
|
|
interface ViewModels {
|
|
'page.server.files': PageStatusModel;
|
|
'page.server.files.editor': FileEditorPageModel;
|
|
'server.files.details': FileDetailsModel;
|
|
'server.files.editor': FileEditorModel;
|
|
'server.files.editor.notice': FileEditorModel;
|
|
'server.files.editor.canvas': FileEditorModel;
|
|
'server.files.editor.language': FileEditorModel;
|
|
'server.files.editor.actions': FileEditorModel;
|
|
'server.files.manager': FileManagerModel;
|
|
'server.files.manager.toolbar': FileManagerModel;
|
|
'server.files.manager.list': FileManagerModel;
|
|
'server.files.manager.selection': FileManagerModel;
|
|
}
|
|
interface ViewInputs {
|
|
'server.files.details': { readonly file: FileObject };
|
|
'server.files.editor.notice': { readonly model: FileEditorModel };
|
|
'server.files.editor.canvas': { readonly model: FileEditorModel };
|
|
'server.files.editor.language': { readonly model: FileEditorModel };
|
|
'server.files.editor.actions': { readonly model: FileEditorModel };
|
|
'server.files.manager.toolbar': { readonly model: FileManagerModel };
|
|
'server.files.manager.list': { readonly model: FileManagerModel };
|
|
'server.files.manager.selection': { readonly model: FileManagerModel };
|
|
}
|
|
}
|
|
|
|
const useFilesPageModel = () => pageStatus(useServerFiles(useCurrentServerUuid() ?? '', useServerDirectory()));
|
|
|
|
const fileDetails = ({ file }: { readonly file: FileObject }): FileDetailsModel => ({
|
|
name: file.attributes.name,
|
|
kind: fileObjectKind(file),
|
|
size: file.attributes.size ?? 0,
|
|
modifiedAt: file.attributes.modified_at,
|
|
});
|
|
|
|
function useFileDetailsModel({ file }: { readonly file: FileObject }): FileDetailsModel {
|
|
return useMemo(() => fileDetails({ file }), [file]);
|
|
}
|
|
|
|
implementViews({
|
|
'page.server.files': {
|
|
use: useFilesPageModel,
|
|
fixtures: pageStatusFixtures,
|
|
},
|
|
'server.files.manager': {
|
|
use: useFileManagerModel,
|
|
fixtures: {
|
|
directory: () => createTestFileManager(),
|
|
selected: () => createTestFileManager({ selection: ['config.json'] }),
|
|
uploading: () =>
|
|
createTestFileManager({
|
|
uploads: [{ id: 'upload-1', name: 'world.zip', loaded: 12 * 1024 ** 2, total: 48 * 1024 ** 2 }],
|
|
}),
|
|
readOnly: () =>
|
|
createTestFileManager({ permissions: { create: false, update: false, delete: false, archive: false } }),
|
|
empty: () => createTestFileManager({ entries: [] }),
|
|
loading: () => createTestFileManager({ entries: [], loading: true }),
|
|
},
|
|
},
|
|
'server.files.details': {
|
|
use: useFileDetailsModel,
|
|
fixtureFromInput: fileDetails,
|
|
fixtures: {
|
|
file: () => createTestFileDetails(),
|
|
directory: () => createTestFileDetails({ name: 'plugins', kind: 'directory', size: 0 }),
|
|
},
|
|
fixtureInput: (model) => ({ file: createTestFileObject(model) }),
|
|
},
|
|
'page.server.files.editor': {
|
|
use: useFileEditorPage,
|
|
fixtures: {
|
|
file: () => createTestFileEditorPage(),
|
|
new: () => createTestFileEditorPage({ file: null }),
|
|
loading: () => createTestFileEditorPage({ loading: true }),
|
|
error: () => createTestFileEditorPage({ error: 'Request failed with status code 404.' }),
|
|
},
|
|
},
|
|
'server.files.editor': {
|
|
use: useFileEditorSession,
|
|
fixtures: {
|
|
file: () => createTestFileEditor(),
|
|
dirty: () => createTestFileEditor({ dirty: true }),
|
|
saving: () => createTestFileEditor({ dirty: true, saving: true }),
|
|
readOnly: () => createTestFileEditor({ readOnly: true }),
|
|
pteroignore: () =>
|
|
createTestFileEditor({ path: '/.pteroignore', name: '.pteroignore', content: '*.log\n' }),
|
|
new: () => createTestFileEditor({ path: '/config', name: '', isNew: true, content: '' }),
|
|
},
|
|
},
|
|
'server.files.editor.notice': { use: modelFromInput },
|
|
'server.files.editor.canvas': { use: modelFromInput },
|
|
'server.files.editor.language': { use: modelFromInput },
|
|
'server.files.editor.actions': { use: modelFromInput },
|
|
'server.files.manager.toolbar': { use: modelFromInput },
|
|
'server.files.manager.list': { use: modelFromInput },
|
|
'server.files.manager.selection': { use: modelFromInput },
|
|
});
|