2021-07-21 18:18:53 +00:00
|
|
|
import React, { useEffect, useState } from 'react';
|
|
|
|
import { useHistory, useLocation, useParams } from 'react-router';
|
|
|
|
import { dirname } from 'path';
|
|
|
|
import tw from 'twin.macro';
|
2019-10-26 20:16:27 +00:00
|
|
|
import { httpErrorToHuman } from '@/api/http';
|
2021-07-21 18:18:53 +00:00
|
|
|
import getFileContents from '@/api/server/files/getFileContents';
|
2019-10-26 20:16:27 +00:00
|
|
|
import saveFileContents from '@/api/server/files/saveFileContents';
|
2020-03-30 04:42:02 +00:00
|
|
|
import Can from '@/components/elements/Can';
|
2021-07-21 18:18:53 +00:00
|
|
|
import Editor, { modes } from '@/components/elements/Editor';
|
|
|
|
import ErrorBoundary from '@/components/elements/ErrorBoundary';
|
2020-03-30 04:42:02 +00:00
|
|
|
import FlashMessageRender from '@/components/FlashMessageRender';
|
2021-07-21 18:18:53 +00:00
|
|
|
import Button from '@/components/elements/Button';
|
2020-04-17 18:17:01 +00:00
|
|
|
import PageContentBlock from '@/components/elements/PageContentBlock';
|
2021-01-31 02:01:32 +00:00
|
|
|
import { ServerError } from '@/components/elements/ScreenBlock';
|
2020-08-20 04:11:29 +00:00
|
|
|
import Select from '@/components/elements/Select';
|
2021-07-21 18:18:53 +00:00
|
|
|
import SpinnerOverlay from '@/components/elements/SpinnerOverlay';
|
|
|
|
import FileManagerBreadcrumbs from '@/components/server/files/FileManagerBreadcrumbs';
|
|
|
|
import FileNameModal from '@/components/server/files/FileNameModal';
|
2020-08-20 04:11:29 +00:00
|
|
|
import useFlash from '@/plugins/useFlash';
|
2020-08-26 04:39:00 +00:00
|
|
|
import { ServerContext } from '@/state/server';
|
2021-01-02 00:55:09 +00:00
|
|
|
import { encodePathSegments, hashToPath } from '@/helpers';
|
2019-10-19 21:51:10 +00:00
|
|
|
|
2019-08-17 18:40:51 +00:00
|
|
|
export default () => {
|
2020-04-17 18:27:52 +00:00
|
|
|
const [ error, setError ] = useState('');
|
2021-05-08 17:37:18 +00:00
|
|
|
const { action } = useParams<{ action: 'new' | string }>();
|
2019-12-22 00:38:40 +00:00
|
|
|
const [ loading, setLoading ] = useState(action === 'edit');
|
2019-10-20 00:35:01 +00:00
|
|
|
const [ content, setContent ] = useState('');
|
2019-12-22 00:38:40 +00:00
|
|
|
const [ modalVisible, setModalVisible ] = useState(false);
|
2021-07-21 18:18:53 +00:00
|
|
|
const [ mode, setMode ] = useState<string>('text/plain');
|
2019-10-26 20:16:27 +00:00
|
|
|
|
2020-07-05 02:01:49 +00:00
|
|
|
const history = useHistory();
|
|
|
|
const { hash } = useLocation();
|
|
|
|
|
2020-08-26 04:39:00 +00:00
|
|
|
const id = ServerContext.useStoreState(state => state.server.data!.id);
|
|
|
|
const uuid = ServerContext.useStoreState(state => state.server.data!.uuid);
|
2020-10-13 04:02:51 +00:00
|
|
|
const setDirectory = ServerContext.useStoreActions(actions => actions.files.setDirectory);
|
2020-08-20 04:11:29 +00:00
|
|
|
const { addError, clearFlashes } = useFlash();
|
2019-09-28 21:59:05 +00:00
|
|
|
|
2021-07-21 18:18:53 +00:00
|
|
|
// eslint-disable-next-line prefer-const
|
2019-10-26 20:16:27 +00:00
|
|
|
let fetchFileContent: null | (() => Promise<string>) = null;
|
2019-10-12 22:29:45 +00:00
|
|
|
|
2020-08-26 01:47:26 +00:00
|
|
|
useEffect(() => {
|
|
|
|
if (action === 'new') return;
|
|
|
|
|
|
|
|
setError('');
|
2020-10-13 04:02:51 +00:00
|
|
|
setLoading(true);
|
2021-01-02 00:55:09 +00:00
|
|
|
const path = hashToPath(hash);
|
|
|
|
setDirectory(dirname(path));
|
|
|
|
getFileContents(uuid, path)
|
2020-08-26 01:47:26 +00:00
|
|
|
.then(setContent)
|
|
|
|
.catch(error => {
|
|
|
|
console.error(error);
|
|
|
|
setError(httpErrorToHuman(error));
|
|
|
|
})
|
|
|
|
.then(() => setLoading(false));
|
|
|
|
}, [ action, uuid, hash ]);
|
2019-08-17 18:40:51 +00:00
|
|
|
|
2019-12-22 00:38:40 +00:00
|
|
|
const save = (name?: string) => {
|
2019-10-26 20:16:27 +00:00
|
|
|
if (!fetchFileContent) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
setLoading(true);
|
2020-03-30 04:42:02 +00:00
|
|
|
clearFlashes('files:view');
|
2020-04-17 18:27:52 +00:00
|
|
|
fetchFileContent()
|
2021-01-02 00:55:09 +00:00
|
|
|
.then(content => saveFileContents(uuid, name || hashToPath(hash), content))
|
2019-12-22 00:38:40 +00:00
|
|
|
.then(() => {
|
|
|
|
if (name) {
|
2021-01-02 00:55:09 +00:00
|
|
|
history.push(`/server/${id}/files/edit#/${encodePathSegments(name)}`);
|
2019-12-22 00:38:40 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Promise.resolve();
|
2019-10-26 20:16:27 +00:00
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
console.error(error);
|
2020-03-30 04:42:02 +00:00
|
|
|
addError({ message: httpErrorToHuman(error), key: 'files:view' });
|
2019-10-26 20:16:27 +00:00
|
|
|
})
|
|
|
|
.then(() => setLoading(false));
|
|
|
|
};
|
|
|
|
|
2020-04-17 18:27:52 +00:00
|
|
|
if (error) {
|
|
|
|
return (
|
2020-08-20 04:11:29 +00:00
|
|
|
<ServerError message={error} onBack={() => history.goBack()}/>
|
2020-04-17 18:27:52 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-08-17 18:40:51 +00:00
|
|
|
return (
|
2020-04-17 18:17:01 +00:00
|
|
|
<PageContentBlock>
|
2020-07-05 00:57:24 +00:00
|
|
|
<FlashMessageRender byKey={'files:view'} css={tw`mb-4`}/>
|
2020-10-23 04:18:46 +00:00
|
|
|
<ErrorBoundary>
|
2020-12-07 03:42:05 +00:00
|
|
|
<div css={tw`mb-4`}>
|
|
|
|
<FileManagerBreadcrumbs withinFileEditor isNewFile={action !== 'edit'}/>
|
|
|
|
</div>
|
2020-10-23 04:18:46 +00:00
|
|
|
</ErrorBoundary>
|
2020-07-05 00:57:24 +00:00
|
|
|
{hash.replace(/^#/, '').endsWith('.pteroignore') &&
|
|
|
|
<div css={tw`mb-4 p-4 border-l-4 bg-neutral-900 rounded border-cyan-400`}>
|
|
|
|
<p css={tw`text-neutral-300 text-sm`}>
|
|
|
|
You're editing
|
|
|
|
a <code css={tw`font-mono bg-black rounded py-px px-1`}>.pteroignore</code> file.
|
2020-04-20 03:04:39 +00:00
|
|
|
Any files or directories listed in here will be excluded from backups. Wildcards are supported by
|
2020-07-05 00:57:24 +00:00
|
|
|
using an asterisk (<code css={tw`font-mono bg-black rounded py-px px-1`}>*</code>). You can
|
2020-04-20 03:04:39 +00:00
|
|
|
negate a prior rule by prepending an exclamation point
|
2020-07-05 00:57:24 +00:00
|
|
|
(<code css={tw`font-mono bg-black rounded py-px px-1`}>!</code>).
|
2020-04-20 03:04:39 +00:00
|
|
|
</p>
|
|
|
|
</div>
|
|
|
|
}
|
2019-12-22 00:38:40 +00:00
|
|
|
<FileNameModal
|
|
|
|
visible={modalVisible}
|
|
|
|
onDismissed={() => setModalVisible(false)}
|
|
|
|
onFileNamed={(name) => {
|
|
|
|
setModalVisible(false);
|
|
|
|
save(name);
|
|
|
|
}}
|
|
|
|
/>
|
2020-07-05 00:57:24 +00:00
|
|
|
<div css={tw`relative`}>
|
2019-10-26 20:16:27 +00:00
|
|
|
<SpinnerOverlay visible={loading}/>
|
2021-07-21 18:18:53 +00:00
|
|
|
<Editor
|
|
|
|
style={{ height: 'calc(100vh - 20rem)' }}
|
|
|
|
overrides={tw`rounded h-full`}
|
2019-10-26 20:16:27 +00:00
|
|
|
initialContent={content}
|
2021-07-21 18:18:53 +00:00
|
|
|
filename={hash.replace(/^#/, '')}
|
|
|
|
onModeChanged={m => setMode(m.mime)}
|
2019-10-26 20:16:27 +00:00
|
|
|
fetchContent={value => {
|
|
|
|
fetchFileContent = value;
|
|
|
|
}}
|
2020-09-27 16:30:24 +00:00
|
|
|
onContentSaved={() => {
|
|
|
|
if (action !== 'edit') {
|
|
|
|
setModalVisible(true);
|
|
|
|
} else {
|
|
|
|
save();
|
|
|
|
}
|
|
|
|
}}
|
2019-10-26 20:16:27 +00:00
|
|
|
/>
|
|
|
|
</div>
|
2020-07-05 00:57:24 +00:00
|
|
|
<div css={tw`flex justify-end mt-4`}>
|
2020-09-13 17:33:12 +00:00
|
|
|
<div css={tw`flex-1 sm:flex-none rounded bg-neutral-900 mr-4`}>
|
2020-08-20 04:11:29 +00:00
|
|
|
<Select value={mode} onChange={e => setMode(e.currentTarget.value)}>
|
2020-09-16 03:53:23 +00:00
|
|
|
{modes.map(mode => (
|
|
|
|
<option key={`${mode.name}_${mode.mime}`} value={mode.mime}>
|
|
|
|
{mode.name}
|
|
|
|
</option>
|
|
|
|
))}
|
2020-08-20 04:11:29 +00:00
|
|
|
</Select>
|
|
|
|
</div>
|
2019-12-22 00:38:40 +00:00
|
|
|
{action === 'edit' ?
|
2020-03-30 04:42:02 +00:00
|
|
|
<Can action={'file.update'}>
|
2020-09-13 17:33:12 +00:00
|
|
|
<Button css={tw`flex-1 sm:flex-none`} onClick={() => save()}>
|
2020-03-30 04:42:02 +00:00
|
|
|
Save Content
|
2020-07-05 00:57:24 +00:00
|
|
|
</Button>
|
2020-03-30 04:42:02 +00:00
|
|
|
</Can>
|
2019-12-22 00:38:40 +00:00
|
|
|
:
|
2020-03-30 04:42:02 +00:00
|
|
|
<Can action={'file.create'}>
|
2020-09-13 17:33:12 +00:00
|
|
|
<Button css={tw`flex-1 sm:flex-none`} onClick={() => setModalVisible(true)}>
|
2020-03-30 04:42:02 +00:00
|
|
|
Create File
|
2020-07-05 00:57:24 +00:00
|
|
|
</Button>
|
2020-03-30 04:42:02 +00:00
|
|
|
</Can>
|
2019-12-22 00:38:40 +00:00
|
|
|
}
|
2019-10-20 00:35:01 +00:00
|
|
|
</div>
|
2020-04-17 18:17:01 +00:00
|
|
|
</PageContentBlock>
|
2019-08-17 18:40:51 +00:00
|
|
|
);
|
|
|
|
};
|