PteroTheme/resources/scripts/components/server/files/FileEditContainer.tsx

39 lines
1.3 KiB
TypeScript
Raw Normal View History

2019-10-20 01:35:01 +01:00
import React, { lazy, useEffect, useState } from 'react';
import { ServerContext } from '@/state/server';
2019-10-20 01:35:01 +01:00
import getFileContents from '@/api/server/files/getFileContents';
import useRouter from 'use-react-router';
2019-10-12 23:29:45 +01:00
2019-10-19 23:31:02 +01:00
const LazyAceEditor = lazy(() => import(/* webpackChunkName: "editor" */'@/components/elements/AceEditor'));
2019-08-17 19:40:51 +01:00
export default () => {
2019-10-20 01:35:01 +01:00
const { location: { hash } } = useRouter();
const [ content, setContent ] = useState('');
const uuid = ServerContext.useStoreState(state => state.server.data!.uuid);
2019-10-19 23:31:02 +01:00
let ref: null| (() => Promise<string>) = null;
2019-10-12 23:29:45 +01:00
2019-10-20 01:35:01 +01:00
useEffect(() => {
getFileContents(uuid, hash.replace(/^#/, ''))
.then(setContent)
.catch(error => console.error(error));
}, [ uuid, hash ]);
2019-08-17 19:40:51 +01:00
return (
2019-10-20 01:35:01 +01:00
<div className={'my-10 mb-4'}>
2019-10-19 23:31:02 +01:00
<LazyAceEditor
2019-10-20 01:35:01 +01:00
initialModePath={hash.replace(/^#/, '') || 'plain_text'}
initialContent={content}
2019-10-19 23:31:02 +01:00
fetchContent={value => {
ref = value;
}}
onContentSaved={() => null}
/>
2019-10-20 01:35:01 +01:00
<div className={'flex justify-end mt-4'}>
<button className={'btn btn-primary btn-sm'}>
Save Content
</button>
</div>
2019-08-17 19:40:51 +01:00
</div>
);
};