2019-07-28 03:36:27 +00:00
|
|
|
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
|
2020-07-17 05:21:06 +00:00
|
|
|
import { faFileAlt, faFileArchive, faFileImport, faFolder } from '@fortawesome/free-solid-svg-icons';
|
2022-06-26 18:34:09 +00:00
|
|
|
import { encodePathSegments } from '@/helpers';
|
2020-07-03 20:55:33 +00:00
|
|
|
import { differenceInHours, format, formatDistanceToNow } from 'date-fns';
|
2020-07-11 05:10:51 +00:00
|
|
|
import React, { memo } from 'react';
|
2019-07-28 03:36:27 +00:00
|
|
|
import { FileObject } from '@/api/server/files/loadDirectory';
|
2019-07-30 05:10:45 +00:00
|
|
|
import FileDropdownMenu from '@/components/server/files/FileDropdownMenu';
|
2019-08-04 21:58:31 +00:00
|
|
|
import { ServerContext } from '@/state/server';
|
2021-01-02 00:55:09 +00:00
|
|
|
import { NavLink, useRouteMatch } from 'react-router-dom';
|
2020-07-05 00:57:24 +00:00
|
|
|
import tw from 'twin.macro';
|
2020-07-11 05:10:51 +00:00
|
|
|
import isEqual from 'react-fast-compare';
|
2020-07-11 23:57:30 +00:00
|
|
|
import SelectFileCheckbox from '@/components/server/files/SelectFileCheckbox';
|
2020-09-01 02:36:30 +00:00
|
|
|
import { usePermissions } from '@/plugins/usePermissions';
|
2021-01-02 00:55:09 +00:00
|
|
|
import { join } from 'path';
|
2022-06-26 18:34:09 +00:00
|
|
|
import { bytesToString } from '@/lib/formatters';
|
2022-07-03 18:44:00 +00:00
|
|
|
import styles from './style.module.css';
|
2020-07-11 05:10:51 +00:00
|
|
|
|
2020-08-30 17:25:48 +00:00
|
|
|
const Clickable: React.FC<{ file: FileObject }> = memo(({ file, children }) => {
|
2023-05-30 14:11:07 +00:00
|
|
|
const [canRead] = usePermissions(['file.read']);
|
2022-06-26 19:13:52 +00:00
|
|
|
const [canReadContents] = usePermissions(['file.read-content']);
|
|
|
|
const directory = ServerContext.useStoreState((state) => state.files.directory);
|
2020-07-05 02:01:49 +00:00
|
|
|
|
|
|
|
const match = useRouteMatch();
|
2019-07-28 03:36:27 +00:00
|
|
|
|
2023-05-30 14:11:07 +00:00
|
|
|
return (file.isFile && (!file.isEditable() || !canReadContents)) || (!file.isFile && !canRead) ? (
|
2022-07-03 18:44:00 +00:00
|
|
|
<div className={styles.details}>{children}</div>
|
2022-06-26 19:13:52 +00:00
|
|
|
) : (
|
|
|
|
<NavLink
|
2022-07-03 18:44:00 +00:00
|
|
|
className={styles.details}
|
2022-06-26 19:13:52 +00:00
|
|
|
to={`${match.url}${file.isFile ? '/edit' : ''}#${encodePathSegments(join(directory, file.name))}`}
|
|
|
|
>
|
|
|
|
{children}
|
|
|
|
</NavLink>
|
2019-07-28 03:36:27 +00:00
|
|
|
);
|
2020-08-30 17:25:48 +00:00
|
|
|
}, isEqual);
|
|
|
|
|
|
|
|
const FileObjectRow = ({ file }: { file: FileObject }) => (
|
2022-07-03 18:44:00 +00:00
|
|
|
<div
|
|
|
|
className={styles.file_row}
|
2020-08-30 17:25:48 +00:00
|
|
|
key={file.name}
|
2022-06-26 19:13:52 +00:00
|
|
|
onContextMenu={(e) => {
|
2020-08-30 17:25:48 +00:00
|
|
|
e.preventDefault();
|
|
|
|
window.dispatchEvent(new CustomEvent(`pterodactyl:files:ctx:${file.key}`, { detail: e.clientX }));
|
|
|
|
}}
|
|
|
|
>
|
2022-06-26 19:13:52 +00:00
|
|
|
<SelectFileCheckbox name={file.name} />
|
2020-08-30 17:25:48 +00:00
|
|
|
<Clickable file={file}>
|
2022-07-03 18:44:00 +00:00
|
|
|
<div css={tw`flex-none text-neutral-400 ml-6 mr-4 text-lg pl-3`}>
|
2022-06-26 19:13:52 +00:00
|
|
|
{file.isFile ? (
|
|
|
|
<FontAwesomeIcon
|
|
|
|
icon={file.isSymlink ? faFileImport : file.isArchiveType() ? faFileArchive : faFileAlt}
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<FontAwesomeIcon icon={faFolder} />
|
|
|
|
)}
|
2020-08-30 17:25:48 +00:00
|
|
|
</div>
|
2022-06-26 19:13:52 +00:00
|
|
|
<div css={tw`flex-1 truncate`}>{file.name}</div>
|
|
|
|
{file.isFile && <div css={tw`w-1/6 text-right mr-4 hidden sm:block`}>{bytesToString(file.size)}</div>}
|
|
|
|
<div css={tw`w-1/5 text-right mr-4 hidden md:block`} title={file.modifiedAt.toString()}>
|
|
|
|
{Math.abs(differenceInHours(file.modifiedAt, new Date())) > 48
|
|
|
|
? format(file.modifiedAt, 'MMM do, yyyy h:mma')
|
|
|
|
: formatDistanceToNow(file.modifiedAt, { addSuffix: true })}
|
2020-08-30 17:25:48 +00:00
|
|
|
</div>
|
|
|
|
</Clickable>
|
2022-06-26 19:13:52 +00:00
|
|
|
<FileDropdownMenu file={file} />
|
2022-07-03 18:44:00 +00:00
|
|
|
</div>
|
2020-08-30 17:25:48 +00:00
|
|
|
);
|
2020-07-11 05:10:51 +00:00
|
|
|
|
2020-09-27 16:30:05 +00:00
|
|
|
export default memo(FileObjectRow, (prevProps, nextProps) => {
|
|
|
|
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
|
|
const { isArchiveType, isEditable, ...prevFile } = prevProps.file;
|
|
|
|
const { isArchiveType: nextIsArchiveType, isEditable: nextIsEditable, ...nextFile } = nextProps.file;
|
|
|
|
/* eslint-enable @typescript-eslint/no-unused-vars */
|
|
|
|
|
|
|
|
return isEqual(prevFile, nextFile);
|
|
|
|
});
|