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';
|
2020-04-10 20:57:24 +00:00
|
|
|
import { bytesToHuman, cleanDirectoryPath } 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';
|
2020-07-05 02:01:49 +00:00
|
|
|
import { NavLink, useHistory, 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';
|
|
|
|
import styled from 'styled-components/macro';
|
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';
|
2019-08-04 21:58:31 +00:00
|
|
|
|
2020-07-11 05:10:51 +00:00
|
|
|
const Row = styled.div`
|
|
|
|
${tw`flex bg-neutral-700 rounded-sm mb-px text-sm hover:text-neutral-100 cursor-pointer items-center no-underline hover:bg-neutral-600`};
|
|
|
|
`;
|
|
|
|
|
2020-08-30 17:25:48 +00:00
|
|
|
const Clickable: React.FC<{ file: FileObject }> = memo(({ file, children }) => {
|
2020-09-01 02:36:30 +00:00
|
|
|
const [ canReadContents ] = usePermissions([ 'file.read-content' ]);
|
2019-08-04 21:58:31 +00:00
|
|
|
const directory = ServerContext.useStoreState(state => state.files.directory);
|
2020-07-05 02:01:49 +00:00
|
|
|
|
|
|
|
const history = useHistory();
|
|
|
|
const match = useRouteMatch();
|
2019-07-28 03:36:27 +00:00
|
|
|
|
2020-07-11 05:10:51 +00:00
|
|
|
const onRowClick = (e: React.MouseEvent<HTMLAnchorElement, MouseEvent>) => {
|
|
|
|
// Don't rely on the onClick to work with the generated URL. Because of the way this
|
|
|
|
// component re-renders you'll get redirected into a nested directory structure since
|
|
|
|
// it'll cause the directory variable to update right away when you click.
|
|
|
|
//
|
|
|
|
// Just trust me future me, leave this be.
|
|
|
|
if (!file.isFile) {
|
|
|
|
e.preventDefault();
|
|
|
|
history.push(`#${cleanDirectoryPath(`${directory}/${file.name}`)}`);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-07-28 03:36:27 +00:00
|
|
|
return (
|
2020-09-01 02:36:30 +00:00
|
|
|
(!canReadContents || (file.isFile && !file.isEditable())) ?
|
2020-10-03 23:25:39 +00:00
|
|
|
<div css={tw`flex flex-1 text-neutral-300 no-underline p-3 cursor-default overflow-hidden truncate`}>
|
2020-08-30 17:25:48 +00:00
|
|
|
{children}
|
|
|
|
</div>
|
|
|
|
:
|
2019-09-28 21:59:05 +00:00
|
|
|
<NavLink
|
2020-04-10 20:57:24 +00:00
|
|
|
to={`${match.url}/${file.isFile ? 'edit/' : ''}#${cleanDirectoryPath(`${directory}/${file.name}`)}`}
|
2020-10-03 23:25:39 +00:00
|
|
|
css={tw`flex flex-1 text-neutral-300 no-underline p-3 overflow-hidden truncate`}
|
2020-07-11 05:10:51 +00:00
|
|
|
onClick={onRowClick}
|
2019-07-28 03:36:27 +00:00
|
|
|
>
|
2020-08-30 17:25:48 +00:00
|
|
|
{children}
|
2019-09-28 21:59:05 +00:00
|
|
|
</NavLink>
|
2019-07-28 03:36:27 +00:00
|
|
|
);
|
2020-08-30 17:25:48 +00:00
|
|
|
}, isEqual);
|
|
|
|
|
|
|
|
const FileObjectRow = ({ file }: { file: FileObject }) => (
|
|
|
|
<Row
|
|
|
|
key={file.name}
|
|
|
|
onContextMenu={e => {
|
|
|
|
e.preventDefault();
|
|
|
|
window.dispatchEvent(new CustomEvent(`pterodactyl:files:ctx:${file.key}`, { detail: e.clientX }));
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<SelectFileCheckbox name={file.name}/>
|
|
|
|
<Clickable file={file}>
|
|
|
|
<div css={tw`flex-none self-center text-neutral-400 mr-4 text-lg pl-3 ml-6`}>
|
|
|
|
{file.isFile ?
|
|
|
|
<FontAwesomeIcon icon={file.isSymlink ? faFileImport : file.isArchiveType() ? faFileArchive : faFileAlt}/>
|
|
|
|
:
|
|
|
|
<FontAwesomeIcon icon={faFolder}/>
|
|
|
|
}
|
|
|
|
</div>
|
2020-10-03 23:25:39 +00:00
|
|
|
<div css={tw`flex-1 break-all truncate`}>
|
2020-08-30 17:25:48 +00:00
|
|
|
{file.name}
|
|
|
|
</div>
|
|
|
|
{file.isFile &&
|
2020-09-13 17:02:25 +00:00
|
|
|
<div css={tw`w-1/6 text-right mr-4 hidden sm:block`}>
|
2020-08-30 17:25:48 +00:00
|
|
|
{bytesToHuman(file.size)}
|
|
|
|
</div>
|
|
|
|
}
|
|
|
|
<div
|
2020-09-13 17:02:25 +00:00
|
|
|
css={tw`w-1/5 text-right mr-4 hidden md:block`}
|
2020-08-30 17:25:48 +00:00
|
|
|
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 })
|
|
|
|
}
|
|
|
|
</div>
|
|
|
|
</Clickable>
|
|
|
|
<FileDropdownMenu file={file}/>
|
|
|
|
</Row>
|
|
|
|
);
|
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);
|
|
|
|
});
|