misc_pterodactyl-panel/resources/scripts/components/server/files/FileObjectRow.tsx

70 lines
3.1 KiB
TypeScript
Raw Normal View History

import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
2020-07-05 01:46:09 +00:00
import { faFileAlt, faFileImport, faFolder } from '@fortawesome/free-solid-svg-icons';
import { bytesToHuman, cleanDirectoryPath } from '@/helpers';
import { differenceInHours, format, formatDistanceToNow } from 'date-fns';
import React from 'react';
import { FileObject } from '@/api/server/files/loadDirectory';
2019-07-30 05:10:45 +00:00
import FileDropdownMenu from '@/components/server/files/FileDropdownMenu';
import { ServerContext } from '@/state/server';
import { NavLink } from 'react-router-dom';
import useRouter from 'use-react-router';
2020-07-05 00:57:24 +00:00
import tw from 'twin.macro';
export default ({ file }: { file: FileObject }) => {
const directory = ServerContext.useStoreState(state => state.files.directory);
const setDirectory = ServerContext.useStoreActions(actions => actions.files.setDirectory);
const { match, history } = useRouter();
return (
2019-08-06 04:18:32 +00:00
<div
key={file.name}
2020-07-05 00:57:24 +00:00
css={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`}
>
<NavLink
to={`${match.url}/${file.isFile ? 'edit/' : ''}#${cleanDirectoryPath(`${directory}/${file.name}`)}`}
2020-07-05 00:57:24 +00:00
css={tw`flex flex-1 text-neutral-300 no-underline p-3`}
2019-08-06 04:18:32 +00:00
onClick={e => {
// 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-08-06 04:18:32 +00:00
}}
>
2020-07-05 00:57:24 +00:00
<div css={tw`flex-none text-neutral-400 mr-4 text-lg pl-3`}>
2019-08-06 04:18:32 +00:00
{file.isFile ?
<FontAwesomeIcon icon={file.isSymlink ? faFileImport : faFileAlt}/>
:
<FontAwesomeIcon icon={faFolder}/>
}
</div>
2020-07-05 00:57:24 +00:00
<div css={tw`flex-1`}>
2019-08-06 04:18:32 +00:00
{file.name}
</div>
{file.isFile &&
2020-07-05 00:57:24 +00:00
<div css={tw`w-1/6 text-right mr-4`}>
2019-08-06 04:18:32 +00:00
{bytesToHuman(file.size)}
</div>
}
2019-08-06 04:18:32 +00:00
<div
2020-07-05 00:57:24 +00:00
css={tw`w-1/5 text-right mr-4`}
2019-08-06 04:18:32 +00:00
title={file.modifiedAt.toString()}
>
{Math.abs(differenceInHours(file.modifiedAt, new Date())) > 48 ?
2020-07-05 00:57:24 +00:00
format(file.modifiedAt, 'MMM do, yyyy h:mma')
2019-08-06 04:18:32 +00:00
:
formatDistanceToNow(file.modifiedAt, { addSuffix: true })
2019-08-06 04:18:32 +00:00
}
</div>
</NavLink>
<FileDropdownMenu uuid={file.uuid}/>
2019-08-06 04:18:32 +00:00
</div>
);
};