2019-07-28 04:36:27 +01:00
|
|
|
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
|
2020-07-05 02:46:09 +01:00
|
|
|
import { faFileAlt, faFileImport, faFolder } from '@fortawesome/free-solid-svg-icons';
|
2020-04-10 21:57:24 +01:00
|
|
|
import { bytesToHuman, cleanDirectoryPath } from '@/helpers';
|
2020-07-03 21:55:33 +01:00
|
|
|
import { differenceInHours, format, formatDistanceToNow } from 'date-fns';
|
2020-07-11 06:10:51 +01:00
|
|
|
import React, { memo } from 'react';
|
2019-07-28 04:36:27 +01:00
|
|
|
import { FileObject } from '@/api/server/files/loadDirectory';
|
2019-07-30 06:10:45 +01:00
|
|
|
import FileDropdownMenu from '@/components/server/files/FileDropdownMenu';
|
2019-08-04 22:58:31 +01:00
|
|
|
import { ServerContext } from '@/state/server';
|
2020-07-05 03:01:49 +01:00
|
|
|
import { NavLink, useHistory, useRouteMatch } from 'react-router-dom';
|
2020-07-05 01:57:24 +01:00
|
|
|
import tw from 'twin.macro';
|
2020-07-11 06:10:51 +01:00
|
|
|
import isEqual from 'react-fast-compare';
|
|
|
|
import styled from 'styled-components/macro';
|
2020-07-12 00:47:13 +01:00
|
|
|
import Input from '@/components/elements/Input';
|
2019-08-04 22:58:31 +01:00
|
|
|
|
2020-07-11 06:10:51 +01: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-07-12 00:47:13 +01:00
|
|
|
const Checkbox = styled(Input)`
|
2020-07-11 23:37:59 +01:00
|
|
|
&& {
|
|
|
|
${tw`border-neutral-500`};
|
|
|
|
|
|
|
|
&:not(:checked) {
|
|
|
|
${tw`hover:border-neutral-300`};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`;
|
|
|
|
|
2020-07-11 06:10:51 +01:00
|
|
|
const FileObjectRow = ({ file }: { file: FileObject }) => {
|
2019-08-04 22:58:31 +01:00
|
|
|
const directory = ServerContext.useStoreState(state => state.files.directory);
|
2020-07-12 00:47:13 +01:00
|
|
|
const selectedFiles = ServerContext.useStoreState(state => state.files.selectedFiles);
|
2019-08-06 05:52:48 +01:00
|
|
|
const setDirectory = ServerContext.useStoreActions(actions => actions.files.setDirectory);
|
2020-07-12 00:47:13 +01:00
|
|
|
const setSelectedFiles = ServerContext.useStoreActions(actions => actions.files.setSelectedFiles);
|
2020-07-05 03:01:49 +01:00
|
|
|
|
|
|
|
const history = useHistory();
|
|
|
|
const match = useRouteMatch();
|
2019-07-28 04:36:27 +01:00
|
|
|
|
2020-07-11 06:10:51 +01: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}`)}`);
|
|
|
|
setDirectory(`${directory}/${file.name}`);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-07-28 04:36:27 +01:00
|
|
|
return (
|
2020-07-11 19:12:59 +01:00
|
|
|
<Row
|
|
|
|
key={file.name}
|
|
|
|
onContextMenu={e => {
|
|
|
|
e.preventDefault();
|
|
|
|
window.dispatchEvent(new CustomEvent(`pterodactyl:files:ctx:${file.uuid}`, { detail: e.clientX }));
|
|
|
|
}}
|
|
|
|
>
|
2020-07-11 23:37:59 +01:00
|
|
|
<label css={tw`flex-none p-4 absolute self-center z-30 cursor-pointer`}>
|
2020-07-12 00:47:13 +01:00
|
|
|
<Checkbox
|
|
|
|
name={'selectedFiles'}
|
|
|
|
value={file.name}
|
|
|
|
checked={selectedFiles.indexOf(file.name) >= 0}
|
|
|
|
type={'checkbox'}
|
|
|
|
onChange={(e: React.ChangeEvent<HTMLInputElement>) => {
|
|
|
|
if (e.currentTarget.checked) {
|
|
|
|
setSelectedFiles(selectedFiles.filter(f => f !== file.name).concat(file.name));
|
|
|
|
} else {
|
|
|
|
setSelectedFiles(selectedFiles.filter(f => f !== file.name));
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
/>
|
2020-07-11 23:37:59 +01:00
|
|
|
</label>
|
2019-09-28 22:59:05 +01:00
|
|
|
<NavLink
|
2020-04-10 21:57:24 +01:00
|
|
|
to={`${match.url}/${file.isFile ? 'edit/' : ''}#${cleanDirectoryPath(`${directory}/${file.name}`)}`}
|
2020-07-05 01:57:24 +01:00
|
|
|
css={tw`flex flex-1 text-neutral-300 no-underline p-3`}
|
2020-07-11 06:10:51 +01:00
|
|
|
onClick={onRowClick}
|
2019-07-28 04:36:27 +01:00
|
|
|
>
|
2020-07-11 23:37:59 +01:00
|
|
|
<div css={tw`flex-none self-center text-neutral-400 mr-4 text-lg pl-3 ml-6`}>
|
2019-08-06 05:18:32 +01:00
|
|
|
{file.isFile ?
|
|
|
|
<FontAwesomeIcon icon={file.isSymlink ? faFileImport : faFileAlt}/>
|
|
|
|
:
|
|
|
|
<FontAwesomeIcon icon={faFolder}/>
|
|
|
|
}
|
|
|
|
</div>
|
2020-07-05 01:57:24 +01:00
|
|
|
<div css={tw`flex-1`}>
|
2019-08-06 05:18:32 +01:00
|
|
|
{file.name}
|
|
|
|
</div>
|
|
|
|
{file.isFile &&
|
2020-07-05 01:57:24 +01:00
|
|
|
<div css={tw`w-1/6 text-right mr-4`}>
|
2019-08-06 05:18:32 +01:00
|
|
|
{bytesToHuman(file.size)}
|
|
|
|
</div>
|
2019-07-28 04:36:27 +01:00
|
|
|
}
|
2019-08-06 05:18:32 +01:00
|
|
|
<div
|
2020-07-05 01:57:24 +01:00
|
|
|
css={tw`w-1/5 text-right mr-4`}
|
2019-08-06 05:18:32 +01:00
|
|
|
title={file.modifiedAt.toString()}
|
|
|
|
>
|
|
|
|
{Math.abs(differenceInHours(file.modifiedAt, new Date())) > 48 ?
|
2020-07-05 01:57:24 +01:00
|
|
|
format(file.modifiedAt, 'MMM do, yyyy h:mma')
|
2019-08-06 05:18:32 +01:00
|
|
|
:
|
2020-07-03 21:55:33 +01:00
|
|
|
formatDistanceToNow(file.modifiedAt, { addSuffix: true })
|
2019-08-06 05:18:32 +01:00
|
|
|
}
|
|
|
|
</div>
|
2019-09-28 22:59:05 +01:00
|
|
|
</NavLink>
|
2020-07-11 06:38:07 +01:00
|
|
|
<FileDropdownMenu file={file}/>
|
2020-07-11 06:10:51 +01:00
|
|
|
</Row>
|
2019-07-28 04:36:27 +01:00
|
|
|
);
|
|
|
|
};
|
2020-07-11 06:10:51 +01:00
|
|
|
|
|
|
|
export default memo(FileObjectRow, (prevProps, nextProps) => isEqual(prevProps.file, nextProps.file));
|