2019-07-30 05:10:45 +00:00
|
|
|
import React, { createRef, useEffect, useState } from 'react';
|
|
|
|
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
|
2020-07-05 01:46:09 +00:00
|
|
|
import {
|
|
|
|
faCopy,
|
|
|
|
faEllipsisH,
|
|
|
|
faFileDownload,
|
|
|
|
faLevelUpAlt,
|
|
|
|
faPencilAlt,
|
|
|
|
faTrashAlt,
|
|
|
|
} 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';
|
|
|
|
import deleteFile from '@/api/server/files/deleteFile';
|
|
|
|
import SpinnerOverlay from '@/components/elements/SpinnerOverlay';
|
|
|
|
import copyFile from '@/api/server/files/copyFile';
|
2020-03-30 04:42:02 +00:00
|
|
|
import { httpErrorToHuman } from '@/api/http';
|
|
|
|
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';
|
|
|
|
import Fade from '@/components/elements/Fade';
|
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
|
|
|
|
2019-08-04 21:58:31 +00:00
|
|
|
export default ({ uuid }: { uuid: string }) => {
|
2019-07-30 05:10:45 +00:00
|
|
|
const menu = createRef<HTMLDivElement>();
|
2019-08-06 04:02:36 +00:00
|
|
|
const menuButton = createRef<HTMLDivElement>();
|
2019-08-06 04:07:34 +00:00
|
|
|
const [ menuVisible, setMenuVisible ] = useState(false);
|
2019-08-04 22:46:58 +00:00
|
|
|
const [ showSpinner, setShowSpinner ] = useState(false);
|
2019-08-03 05:22:01 +00:00
|
|
|
const [ modal, setModal ] = useState<ModalType | null>(null);
|
|
|
|
const [ posX, setPosX ] = useState(0);
|
2019-07-30 05:10:45 +00:00
|
|
|
|
2020-04-07 03:28:14 +00:00
|
|
|
const server = useServer();
|
|
|
|
const { addError, clearFlashes } = useFlash();
|
|
|
|
|
2019-08-04 21:58:31 +00:00
|
|
|
const file = ServerContext.useStoreState(state => state.files.contents.find(file => file.uuid === uuid));
|
2019-08-04 22:46:58 +00:00
|
|
|
const directory = ServerContext.useStoreState(state => state.files.directory);
|
|
|
|
const { removeFile, getDirectoryContents } = ServerContext.useStoreActions(actions => actions.files);
|
2019-08-06 04:02:36 +00:00
|
|
|
|
2019-08-04 21:58:31 +00:00
|
|
|
if (!file) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-07-30 05:10:45 +00:00
|
|
|
const windowListener = (e: MouseEvent) => {
|
2019-08-06 04:07:34 +00:00
|
|
|
if (e.button === 2 || !menuVisible || !menu.current) {
|
2019-07-30 05:10:45 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (e.target === menu.current || menu.current.contains(e.target as Node)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (e.target !== menu.current && !menu.current.contains(e.target as Node)) {
|
2019-08-06 04:07:34 +00:00
|
|
|
setMenuVisible(false);
|
2019-07-30 05:10:45 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-08-04 22:46:58 +00:00
|
|
|
const doDeletion = () => {
|
|
|
|
setShowSpinner(true);
|
2020-04-07 03:28:14 +00:00
|
|
|
clearFlashes('files');
|
2019-08-04 22:46:58 +00:00
|
|
|
deleteFile(server.uuid, join(directory, file.name))
|
|
|
|
.then(() => removeFile(uuid))
|
|
|
|
.catch(error => {
|
|
|
|
console.error('Error while attempting to delete a file.', error);
|
2020-04-07 03:28:14 +00:00
|
|
|
addError({ key: 'files', message: httpErrorToHuman(error) });
|
2019-08-04 22:46:58 +00:00
|
|
|
setShowSpinner(false);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const doCopy = () => {
|
|
|
|
setShowSpinner(true);
|
2020-04-07 03:28:14 +00:00
|
|
|
clearFlashes('files');
|
2019-08-04 22:46:58 +00:00
|
|
|
copyFile(server.uuid, join(directory, file.name))
|
|
|
|
.then(() => getDirectoryContents(directory))
|
|
|
|
.catch(error => {
|
|
|
|
console.error('Error while attempting to copy file.', error);
|
2020-04-07 03:28:14 +00:00
|
|
|
addError({ key: 'files', message: httpErrorToHuman(error) });
|
2019-08-04 22:46:58 +00:00
|
|
|
setShowSpinner(false);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2019-10-26 21:36:37 +00:00
|
|
|
const doDownload = () => {
|
2020-04-07 03:28:14 +00:00
|
|
|
setShowSpinner(true);
|
|
|
|
clearFlashes('files');
|
|
|
|
getFileDownloadUrl(server.uuid, join(directory, file.name))
|
|
|
|
.then(url => {
|
|
|
|
// @ts-ignore
|
|
|
|
window.location = url;
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
console.error(error);
|
|
|
|
addError({ key: 'files', message: httpErrorToHuman(error) });
|
|
|
|
})
|
|
|
|
.then(() => setShowSpinner(false));
|
2019-10-26 21:36:37 +00:00
|
|
|
};
|
|
|
|
|
2019-07-30 05:10:45 +00:00
|
|
|
useEffect(() => {
|
2019-08-06 04:07:34 +00:00
|
|
|
menuVisible
|
2019-07-30 05:10:45 +00:00
|
|
|
? document.addEventListener('click', windowListener)
|
|
|
|
: document.removeEventListener('click', windowListener);
|
|
|
|
|
2019-08-06 04:07:34 +00:00
|
|
|
if (menuVisible && menu.current) {
|
2019-07-30 05:10:45 +00:00
|
|
|
menu.current.setAttribute(
|
2019-08-03 05:22:01 +00:00
|
|
|
'style', `margin-top: -0.35rem; left: ${Math.round(posX - menu.current.clientWidth)}px`,
|
2019-07-30 05:10:45 +00:00
|
|
|
);
|
|
|
|
}
|
2019-08-06 04:07:34 +00:00
|
|
|
}, [ menuVisible ]);
|
2019-07-30 05:10:45 +00:00
|
|
|
|
|
|
|
useEffect(() => () => {
|
|
|
|
document.removeEventListener('click', windowListener);
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
return (
|
2019-08-04 22:34:46 +00:00
|
|
|
<div key={`dropdown:${file.uuid}`}>
|
2019-07-30 05:10:45 +00:00
|
|
|
<div
|
2019-08-06 04:02:36 +00:00
|
|
|
ref={menuButton}
|
2020-07-05 00:57:24 +00:00
|
|
|
css={tw`p-3 hover:text-white`}
|
2019-07-30 05:10:45 +00:00
|
|
|
onClick={e => {
|
|
|
|
e.preventDefault();
|
2019-08-06 04:07:34 +00:00
|
|
|
if (!menuVisible) {
|
2019-07-30 05:10:45 +00:00
|
|
|
setPosX(e.clientX);
|
|
|
|
}
|
2019-08-06 04:07:34 +00:00
|
|
|
setModal(null);
|
|
|
|
setMenuVisible(!menuVisible);
|
2019-07-30 05:10:45 +00:00
|
|
|
}}
|
|
|
|
>
|
|
|
|
<FontAwesomeIcon icon={faEllipsisH}/>
|
2019-08-06 04:07:34 +00:00
|
|
|
<RenameFileModal
|
|
|
|
file={file}
|
|
|
|
visible={modal === 'rename' || modal === 'move'}
|
|
|
|
useMoveTerminology={modal === 'move'}
|
|
|
|
onDismissed={() => {
|
|
|
|
setModal(null);
|
|
|
|
setMenuVisible(false);
|
|
|
|
}}
|
|
|
|
/>
|
2020-07-05 00:57:24 +00:00
|
|
|
<SpinnerOverlay visible={showSpinner} fixed size={'large'}/>
|
2019-07-30 05:10:45 +00:00
|
|
|
</div>
|
2020-07-05 00:57:24 +00:00
|
|
|
<Fade timeout={250} in={menuVisible} unmountOnExit classNames={'fade'}>
|
2019-07-30 05:10:45 +00:00
|
|
|
<div
|
|
|
|
ref={menu}
|
2020-03-30 04:42:02 +00:00
|
|
|
onClick={e => {
|
|
|
|
e.stopPropagation();
|
|
|
|
setMenuVisible(false);
|
|
|
|
}}
|
2020-07-05 00:57:24 +00:00
|
|
|
css={tw`absolute bg-white p-2 rounded border border-neutral-700 shadow-lg text-neutral-500 min-w-48`}
|
2019-07-30 05:10:45 +00:00
|
|
|
>
|
2020-03-30 04:42:02 +00:00
|
|
|
<Can action={'file.update'}>
|
|
|
|
<div
|
|
|
|
onClick={() => setModal('rename')}
|
2020-07-05 00:57:24 +00:00
|
|
|
css={tw`hover:text-neutral-700 p-2 flex items-center hover:bg-neutral-100 rounded`}
|
2020-03-30 04:42:02 +00:00
|
|
|
>
|
2020-07-05 00:57:24 +00:00
|
|
|
<FontAwesomeIcon icon={faPencilAlt} css={tw`text-xs`}/>
|
|
|
|
<span css={tw`ml-2`}>Rename</span>
|
2020-03-30 04:42:02 +00:00
|
|
|
</div>
|
|
|
|
<div
|
|
|
|
onClick={() => setModal('move')}
|
2020-07-05 00:57:24 +00:00
|
|
|
css={tw`hover:text-neutral-700 p-2 flex items-center hover:bg-neutral-100 rounded`}
|
2020-03-30 04:42:02 +00:00
|
|
|
>
|
2020-07-05 00:57:24 +00:00
|
|
|
<FontAwesomeIcon icon={faLevelUpAlt} css={tw`text-xs`}/>
|
|
|
|
<span css={tw`ml-2`}>Move</span>
|
2020-03-30 04:42:02 +00:00
|
|
|
</div>
|
|
|
|
</Can>
|
|
|
|
<Can action={'file.create'}>
|
|
|
|
<div
|
|
|
|
onClick={() => doCopy()}
|
2020-07-05 00:57:24 +00:00
|
|
|
css={tw`hover:text-neutral-700 p-2 flex items-center hover:bg-neutral-100 rounded`}
|
2020-03-30 04:42:02 +00:00
|
|
|
>
|
2020-07-05 00:57:24 +00:00
|
|
|
<FontAwesomeIcon icon={faCopy} css={tw`text-xs`}/>
|
|
|
|
<span css={tw`ml-2`}>Copy</span>
|
2020-03-30 04:42:02 +00:00
|
|
|
</div>
|
|
|
|
</Can>
|
2019-10-26 21:36:37 +00:00
|
|
|
<div
|
2020-07-05 00:57:24 +00:00
|
|
|
css={tw`hover:text-neutral-700 p-2 flex items-center hover:bg-neutral-100 rounded`}
|
2019-10-26 21:36:37 +00:00
|
|
|
onClick={() => doDownload()}
|
|
|
|
>
|
2020-07-05 00:57:24 +00:00
|
|
|
<FontAwesomeIcon icon={faFileDownload} css={tw`text-xs`}/>
|
|
|
|
<span css={tw`ml-2`}>Download</span>
|
2019-07-30 05:10:45 +00:00
|
|
|
</div>
|
2020-03-30 04:42:02 +00:00
|
|
|
<Can action={'file.delete'}>
|
|
|
|
<div
|
|
|
|
onClick={() => doDeletion()}
|
2020-07-05 00:57:24 +00:00
|
|
|
css={tw`hover:text-red-700 p-2 flex items-center hover:bg-red-100 rounded`}
|
2020-03-30 04:42:02 +00:00
|
|
|
>
|
2020-07-05 00:57:24 +00:00
|
|
|
<FontAwesomeIcon icon={faTrashAlt} css={tw`text-xs`}/>
|
|
|
|
<span css={tw`ml-2`}>Delete</span>
|
2020-03-30 04:42:02 +00:00
|
|
|
</div>
|
|
|
|
</Can>
|
2019-07-30 05:10:45 +00:00
|
|
|
</div>
|
2020-07-05 00:57:24 +00:00
|
|
|
</Fade>
|
2019-07-30 05:10:45 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|