import React, { lazy, useEffect, useState } from 'react'; import { ServerContext } from '@/state/server'; import getFileContents from '@/api/server/files/getFileContents'; import useRouter from 'use-react-router'; import { Actions, useStoreState } from 'easy-peasy'; import { ApplicationStore } from '@/state'; import { httpErrorToHuman } from '@/api/http'; import SpinnerOverlay from '@/components/elements/SpinnerOverlay'; import saveFileContents from '@/api/server/files/saveFileContents'; import FileManagerBreadcrumbs from '@/components/server/files/FileManagerBreadcrumbs'; const LazyAceEditor = lazy(() => import(/* webpackChunkName: "editor" */'@/components/elements/AceEditor')); export default () => { const { location: { hash } } = useRouter(); const [ loading, setLoading ] = useState(true); const [ content, setContent ] = useState(''); const uuid = ServerContext.useStoreState(state => state.server.data!.uuid); const addError = useStoreState((state: Actions) => state.flashes.addError); let fetchFileContent: null | (() => Promise) = null; useEffect(() => { getFileContents(uuid, hash.replace(/^#/, '')) .then(setContent) .catch(error => console.error(error)) .then(() => setLoading(false)); }, [ uuid, hash ]); const save = (e: React.MouseEvent) => { if (!fetchFileContent) { return; } setLoading(true); fetchFileContent() .then(content => { return saveFileContents(uuid, hash.replace(/^#/, ''), content); }) .catch(error => { console.error(error); addError({ message: httpErrorToHuman(error), key: 'files' }); }) .then(() => setLoading(false)); }; return (
{ fetchFileContent = value; }} onContentSaved={() => null} />
{content &&
}
); };