2020-07-11 18:12:59 +00:00
|
|
|
import React, { useRef, useState } from 'react';
|
2019-07-30 05:10:45 +00:00
|
|
|
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
|
2020-07-05 01:46:09 +00:00
|
|
|
import {
|
|
|
|
faCopy,
|
|
|
|
faEllipsisH,
|
2020-07-11 20:38:49 +00:00
|
|
|
faFileArchive,
|
2020-07-05 01:46:09 +00:00
|
|
|
faFileDownload,
|
|
|
|
faLevelUpAlt,
|
|
|
|
faPencilAlt,
|
|
|
|
faTrashAlt,
|
2020-07-11 05:38:07 +00:00
|
|
|
IconDefinition,
|
2020-07-05 01:46:09 +00:00
|
|
|
} from '@fortawesome/free-solid-svg-icons';
|
2019-08-03 05:22:01 +00:00
|
|
|
import RenameFileModal from '@/components/server/files/RenameFileModal';
|
2019-08-04 21:58:31 +00:00
|
|
|
import { ServerContext } from '@/state/server';
|
2019-08-04 22:46:58 +00:00
|
|
|
import { join } from 'path';
|
2020-07-11 22:37:59 +00:00
|
|
|
import deleteFiles from '@/api/server/files/deleteFiles';
|
2019-08-04 22:46:58 +00:00
|
|
|
import SpinnerOverlay from '@/components/elements/SpinnerOverlay';
|
|
|
|
import copyFile from '@/api/server/files/copyFile';
|
2020-03-30 04:42:02 +00:00
|
|
|
import Can from '@/components/elements/Can';
|
2020-04-07 03:28:14 +00:00
|
|
|
import getFileDownloadUrl from '@/api/server/files/getFileDownloadUrl';
|
|
|
|
import useServer from '@/plugins/useServer';
|
|
|
|
import useFlash from '@/plugins/useFlash';
|
2020-07-05 00:57:24 +00:00
|
|
|
import tw from 'twin.macro';
|
2020-07-11 05:38:07 +00:00
|
|
|
import { FileObject } from '@/api/server/files/loadDirectory';
|
|
|
|
import useFileManagerSwr from '@/plugins/useFileManagerSwr';
|
|
|
|
import DropdownMenu from '@/components/elements/DropdownMenu';
|
|
|
|
import styled from 'styled-components/macro';
|
2020-07-11 18:12:59 +00:00
|
|
|
import useEventListener from '@/plugins/useEventListener';
|
2020-07-11 20:38:49 +00:00
|
|
|
import compressFiles from '@/api/server/files/compressFiles';
|
2019-08-03 05:22:01 +00:00
|
|
|
|
2019-08-04 22:46:58 +00:00
|
|
|
type ModalType = 'rename' | 'move';
|
2019-07-30 05:10:45 +00:00
|
|
|
|
2020-07-11 05:38:07 +00:00
|
|
|
const StyledRow = styled.div<{ $danger?: boolean }>`
|
|
|
|
${tw`p-2 flex items-center rounded`};
|
|
|
|
${props => props.$danger ? tw`hover:bg-red-100 hover:text-red-700` : tw`hover:bg-neutral-100 hover:text-neutral-700`};
|
|
|
|
`;
|
2020-04-07 03:28:14 +00:00
|
|
|
|
2020-07-11 05:38:07 +00:00
|
|
|
interface RowProps extends React.HTMLAttributes<HTMLDivElement> {
|
|
|
|
icon: IconDefinition;
|
|
|
|
title: string;
|
|
|
|
$danger?: boolean;
|
|
|
|
}
|
2019-08-06 04:02:36 +00:00
|
|
|
|
2020-07-11 05:38:07 +00:00
|
|
|
const Row = ({ icon, title, ...props }: RowProps) => (
|
|
|
|
<StyledRow {...props}>
|
|
|
|
<FontAwesomeIcon icon={icon} css={tw`text-xs`}/>
|
|
|
|
<span css={tw`ml-2`}>{title}</span>
|
|
|
|
</StyledRow>
|
|
|
|
);
|
2019-08-04 21:58:31 +00:00
|
|
|
|
2020-07-11 05:38:07 +00:00
|
|
|
export default ({ file }: { file: FileObject }) => {
|
2020-07-11 18:12:59 +00:00
|
|
|
const onClickRef = useRef<DropdownMenu>(null);
|
2020-07-11 05:38:07 +00:00
|
|
|
const [ showSpinner, setShowSpinner ] = useState(false);
|
|
|
|
const [ modal, setModal ] = useState<ModalType | null>(null);
|
2019-07-30 05:10:45 +00:00
|
|
|
|
2020-07-11 05:38:07 +00:00
|
|
|
const { uuid } = useServer();
|
|
|
|
const { mutate } = useFileManagerSwr();
|
|
|
|
const { clearAndAddHttpError, clearFlashes } = useFlash();
|
|
|
|
const directory = ServerContext.useStoreState(state => state.files.directory);
|
2019-07-30 05:10:45 +00:00
|
|
|
|
2020-07-11 18:12:59 +00:00
|
|
|
useEventListener(`pterodactyl:files:ctx:${file.uuid}`, (e: CustomEvent) => {
|
|
|
|
if (onClickRef.current) {
|
|
|
|
onClickRef.current.triggerMenu(e.detail);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-08-04 22:46:58 +00:00
|
|
|
const doDeletion = () => {
|
2020-04-07 03:28:14 +00:00
|
|
|
clearFlashes('files');
|
2020-07-11 05:38:07 +00:00
|
|
|
|
|
|
|
// For UI speed, immediately remove the file from the listing before calling the deletion function.
|
|
|
|
// If the delete actually fails, we'll fetch the current directory contents again automatically.
|
|
|
|
mutate(files => files.filter(f => f.uuid !== file.uuid), false);
|
|
|
|
|
2020-07-11 22:37:59 +00:00
|
|
|
deleteFiles(uuid, directory, [ file.name ]).catch(error => {
|
2020-07-11 05:38:07 +00:00
|
|
|
mutate();
|
|
|
|
clearAndAddHttpError({ key: 'files', error });
|
|
|
|
});
|
2019-08-04 22:46:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const doCopy = () => {
|
|
|
|
setShowSpinner(true);
|
2020-04-07 03:28:14 +00:00
|
|
|
clearFlashes('files');
|
2020-07-11 05:38:07 +00:00
|
|
|
|
|
|
|
copyFile(uuid, join(directory, file.name))
|
|
|
|
.then(() => mutate())
|
2020-07-11 20:38:49 +00:00
|
|
|
.catch(error => clearAndAddHttpError({ key: 'files', error }))
|
|
|
|
.then(() => setShowSpinner(false));
|
2019-08-04 22:46:58 +00:00
|
|
|
};
|
|
|
|
|
2019-10-26 21:36:37 +00:00
|
|
|
const doDownload = () => {
|
2020-04-07 03:28:14 +00:00
|
|
|
setShowSpinner(true);
|
|
|
|
clearFlashes('files');
|
2020-07-11 05:38:07 +00:00
|
|
|
|
|
|
|
getFileDownloadUrl(uuid, join(directory, file.name))
|
2020-04-07 03:28:14 +00:00
|
|
|
.then(url => {
|
|
|
|
// @ts-ignore
|
|
|
|
window.location = url;
|
|
|
|
})
|
2020-07-11 05:38:07 +00:00
|
|
|
.catch(error => clearAndAddHttpError({ key: 'files', error }))
|
2020-04-07 03:28:14 +00:00
|
|
|
.then(() => setShowSpinner(false));
|
2019-10-26 21:36:37 +00:00
|
|
|
};
|
|
|
|
|
2020-07-11 20:38:49 +00:00
|
|
|
const doArchive = () => {
|
|
|
|
setShowSpinner(true);
|
|
|
|
clearFlashes('files');
|
|
|
|
|
|
|
|
compressFiles(uuid, directory, [ file.name ])
|
|
|
|
.then(() => mutate())
|
|
|
|
.catch(error => clearAndAddHttpError({ key: 'files', error }))
|
|
|
|
.then(() => setShowSpinner(false));
|
|
|
|
};
|
|
|
|
|
2019-07-30 05:10:45 +00:00
|
|
|
return (
|
2020-07-11 05:38:07 +00:00
|
|
|
<DropdownMenu
|
2020-07-11 18:12:59 +00:00
|
|
|
ref={onClickRef}
|
2020-07-11 05:38:07 +00:00
|
|
|
renderToggle={onClick => (
|
2020-07-11 05:53:52 +00:00
|
|
|
<div css={tw`p-3 hover:text-white`} onClick={onClick}>
|
2020-07-11 05:38:07 +00:00
|
|
|
<FontAwesomeIcon icon={faEllipsisH}/>
|
2020-07-11 23:49:46 +00:00
|
|
|
{!!modal &&
|
2020-07-11 05:38:07 +00:00
|
|
|
<RenameFileModal
|
2020-07-11 23:49:46 +00:00
|
|
|
visible
|
|
|
|
appear
|
2020-07-11 23:20:42 +00:00
|
|
|
files={[ file.name ]}
|
2020-07-11 05:38:07 +00:00
|
|
|
useMoveTerminology={modal === 'move'}
|
|
|
|
onDismissed={() => setModal(null)}
|
|
|
|
/>
|
2020-07-11 23:49:46 +00:00
|
|
|
}
|
2020-07-11 05:38:07 +00:00
|
|
|
<SpinnerOverlay visible={showSpinner} fixed size={'large'}/>
|
2019-07-30 05:10:45 +00:00
|
|
|
</div>
|
2020-07-11 05:38:07 +00:00
|
|
|
)}
|
|
|
|
>
|
|
|
|
<Can action={'file.update'}>
|
|
|
|
<Row onClick={() => setModal('rename')} icon={faPencilAlt} title={'Rename'}/>
|
|
|
|
<Row onClick={() => setModal('move')} icon={faLevelUpAlt} title={'Move'}/>
|
|
|
|
</Can>
|
2020-07-11 18:12:59 +00:00
|
|
|
{file.isFile &&
|
2020-07-11 05:38:07 +00:00
|
|
|
<Can action={'file.create'}>
|
|
|
|
<Row onClick={doCopy} icon={faCopy} title={'Copy'}/>
|
|
|
|
</Can>
|
2020-07-11 18:12:59 +00:00
|
|
|
}
|
2020-07-11 20:38:49 +00:00
|
|
|
<Can action={'file.archive'}>
|
|
|
|
<Row onClick={doArchive} icon={faFileArchive} title={'Archive'}/>
|
|
|
|
</Can>
|
2020-07-11 05:38:07 +00:00
|
|
|
<Row onClick={doDownload} icon={faFileDownload} title={'Download'}/>
|
|
|
|
<Can action={'file.delete'}>
|
|
|
|
<Row onClick={doDeletion} icon={faTrashAlt} title={'Delete'} $danger/>
|
|
|
|
</Can>
|
|
|
|
</DropdownMenu>
|
2019-07-30 05:10:45 +00:00
|
|
|
);
|
|
|
|
};
|