mirror of
https://github.com/pterodactyl/panel.git
synced 2026-12-01 03:45:53 -05:00
204 lines
8.3 KiB
TypeScript
204 lines
8.3 KiB
TypeScript
import { createContext, createElement, useContext, useLayoutEffect, useMemo, type ReactNode } from 'react';
|
|
import { Link } from '@tanstack/react-router';
|
|
import { FolderOpen } from 'lucide-react';
|
|
import { useCurrentServerIdentifier } from '@/api/server/queries';
|
|
import type { ClientFileObjectResource as FileObject } from '@/api/extensionTypes';
|
|
import { fileObjectKey } from '@/api/server/files/queries';
|
|
import { NewButton } from '@/components/elements/NewButton';
|
|
import ErrorBoundary from '@/components/elements/ErrorBoundary';
|
|
import Spinner from '@/components/elements/Spinner';
|
|
import Checkbox from '@/components/ui/Checkbox';
|
|
import { Empty, EmptyDescription, EmptyHeader, EmptyMedia, EmptyTitle } from '@/components/ui/empty';
|
|
import FileManagerBreadcrumbs from '@/components/server/files/FileManagerBreadcrumbs';
|
|
import FileManagerStatus from '@/components/server/files/FileManagerStatus';
|
|
import FileObjectRow from '@/components/server/files/FileObjectRow';
|
|
import MassActionsBar from '@/components/server/files/MassActionsBar';
|
|
import NewDirectoryButton from '@/components/server/files/NewDirectoryButton';
|
|
import UploadButton from '@/components/server/files/UploadButton';
|
|
import { FileRowActionsProvider } from '@/components/server/files/useFileActions';
|
|
import Slot from '@/extensions/Slot';
|
|
import { encodePathSegments } from '@/helpers';
|
|
import { cn } from '@/lib/cn';
|
|
import { ServerContext } from '@/state/server/context';
|
|
import { useServerStore } from '@/state/server';
|
|
import { createTestFileManager } from '@/views/fixtures';
|
|
import type { FileManagerEntry, FileManagerModel } from '@/views/files';
|
|
import { defineView, registerBaseView, type ViewPartProps, type ViewParts, type ViewProps } from '@/views/registry';
|
|
import useFileManagerExtensionData from './useFileManagerExtensionData';
|
|
|
|
export interface FileManagerSession {
|
|
model: FileManagerModel;
|
|
files: readonly FileObject[];
|
|
}
|
|
export const FileManagerContext = createContext<FileManagerSession | null>(null);
|
|
|
|
const managerActionsClass = [
|
|
'grid grid-cols-2 sm:grid-cols-3 w-full gap-4 mb-4',
|
|
'md:flex md:flex-1 md:justify-end md:mb-0',
|
|
'[&_button]:w-full [&_button:first-child]:col-span-2 sm:[&_button:first-child]:col-span-1 md:[&_button]:w-auto',
|
|
].join(' ');
|
|
|
|
type PartProps = ViewPartProps<'server.files.manager'>;
|
|
function Toolbar({ model }: PartProps) {
|
|
const id = useCurrentServerIdentifier()!;
|
|
const extensionData = useFileManagerExtensionData();
|
|
|
|
return (
|
|
<ErrorBoundary>
|
|
<div className='flex flex-wrap-reverse md:flex-nowrap mb-4'>
|
|
<FileManagerBreadcrumbs>
|
|
<Checkbox
|
|
className='mx-4'
|
|
aria-label='Select all files'
|
|
checked={model.entries.length > 0 && model.selection.length === model.entries.length}
|
|
indeterminate={model.selection.length > 0 && model.selection.length < model.entries.length}
|
|
onChange={(checked) =>
|
|
model.actions.select(checked ? model.entries.map((entry) => entry.name) : [])
|
|
}
|
|
/>
|
|
</FileManagerBreadcrumbs>
|
|
{model.permissions.create && (
|
|
<div className={managerActionsClass}>
|
|
<FileManagerStatus />
|
|
<NewDirectoryButton />
|
|
{model.permissions.update && <UploadButton />}
|
|
<Link
|
|
to='/server/$id/files/$action'
|
|
params={{ id, action: 'new' }}
|
|
hash={encodePathSegments(model.directory)}
|
|
>
|
|
<NewButton>New file</NewButton>
|
|
</Link>
|
|
</div>
|
|
)}
|
|
</div>
|
|
{extensionData && <Slot name='server.files.toolbar' data={extensionData} />}
|
|
</ErrorBoundary>
|
|
);
|
|
}
|
|
|
|
function List({ model }: PartProps) {
|
|
const { files } = useContext(FileManagerContext)!;
|
|
|
|
if (model.loading) {
|
|
return <Spinner size='large' centered />;
|
|
}
|
|
|
|
if (!files.length) {
|
|
return (
|
|
<Empty className='border'>
|
|
<EmptyHeader>
|
|
<EmptyMedia variant='icon'>
|
|
<FolderOpen />
|
|
</EmptyMedia>
|
|
<EmptyTitle>This folder is empty</EmptyTitle>
|
|
<EmptyDescription>
|
|
{model.permissions.create
|
|
? 'Upload files or create a new file or folder to get started.'
|
|
: 'There are no files in this folder.'}
|
|
</EmptyDescription>
|
|
</EmptyHeader>
|
|
</Empty>
|
|
);
|
|
}
|
|
|
|
return (
|
|
<div>
|
|
{model.truncated && (
|
|
<div className='rounded-sm bg-warning mb-px p-3'>
|
|
<p className='text-warning-foreground text-sm text-center'>
|
|
This directory is too large to display in the browser, limiting output to first 250 files.
|
|
</p>
|
|
</div>
|
|
)}
|
|
<FileRowActionsProvider>
|
|
{files.map((file) => (
|
|
<FileObjectRow key={fileObjectKey(file)} file={file} />
|
|
))}
|
|
</FileRowActionsProvider>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
function Selection({ model }: PartProps) {
|
|
return model.entries.length > 0 ? <MassActionsBar selectedFiles={model.selection} /> : null;
|
|
}
|
|
|
|
export const fileManagerParts: ViewParts<'server.files.manager'> = {
|
|
toolbar: Toolbar,
|
|
list: List,
|
|
selection: Selection,
|
|
};
|
|
export function DefaultFileManager({ className, parts, model }: ViewProps<'server.files.manager'>) {
|
|
return (
|
|
<div className={cn('min-w-0', className)}>
|
|
<parts.toolbar model={model} />
|
|
<parts.list model={model} />
|
|
<parts.selection model={model} />
|
|
</div>
|
|
);
|
|
}
|
|
|
|
DefaultFileManager.parts = fileManagerParts;
|
|
|
|
/** The browsing session's model; the file manager page holds the query, selection and mutations. */
|
|
function useFileBrowserModel(): FileManagerModel {
|
|
const session = useContext(FileManagerContext);
|
|
|
|
if (!session) {
|
|
throw new Error('The file browser renders inside the file manager page.');
|
|
}
|
|
|
|
return session.model;
|
|
}
|
|
|
|
const fixtureFile = (entry: FileManagerEntry): FileObject => ({
|
|
object: 'file_object',
|
|
attributes: {
|
|
name: entry.name,
|
|
mode: entry.mode,
|
|
mode_bits: entry.modeBits,
|
|
size: entry.size,
|
|
is_file: entry.kind !== 'directory',
|
|
is_symlink: entry.kind === 'symlink',
|
|
mimetype: entry.mimetype,
|
|
created_at: entry.modifiedAt,
|
|
modified_at: entry.modifiedAt,
|
|
},
|
|
});
|
|
|
|
function FixtureFiles({ model, children }: { model: FileManagerModel; children?: ReactNode }) {
|
|
const setSelectedFiles = useServerStore((state) => state.files.setSelectedFiles);
|
|
const value = useMemo(() => ({ model, files: model.entries.map(fixtureFile) }), [model]);
|
|
const selection = model.selection.join('/');
|
|
|
|
useLayoutEffect(() => {
|
|
setSelectedFiles({ directory: model.directory, files: selection ? selection.split('/') : [] });
|
|
}, [setSelectedFiles, model.directory, selection]);
|
|
|
|
return <FileManagerContext.Provider value={value}>{children}</FileManagerContext.Provider>;
|
|
}
|
|
|
|
/** The selection lives in a server store, so a fixture brings its own. */
|
|
function FixtureManagerSession({ model, children }: { model: FileManagerModel; children?: ReactNode }) {
|
|
return (
|
|
<ServerContext.Provider>
|
|
<FixtureFiles model={model}>{children}</FixtureFiles>
|
|
</ServerContext.Provider>
|
|
);
|
|
}
|
|
|
|
export const fileManagerView = defineView('server.files.manager', {
|
|
context: 'server',
|
|
use: useFileBrowserModel,
|
|
fixtureScope: FixtureManagerSession,
|
|
slots: ['server.files.toolbar', 'server.files.rowActions', 'server.files.selectionActions'],
|
|
placeholder: createElement(Spinner, { size: 'large', centered: true }),
|
|
fixtures: {
|
|
directory: () => createTestFileManager(),
|
|
empty: () => createTestFileManager({ entries: [] }),
|
|
loading: () => createTestFileManager({ entries: [], loading: true }),
|
|
},
|
|
});
|
|
registerBaseView('classic', 'classic', 'server.files.manager', DefaultFileManager);
|