import type { LanguageDescription } from '@codemirror/language'; import { languages } from '@codemirror/language-data'; import { dirname } from 'pathe'; import { useEffect, useState } from 'react'; import { useNavigate, useParams } from 'react-router-dom'; import tw from 'twin.macro'; import { httpErrorToHuman } from '@/api/http'; import getFileContents from '@/api/server/files/getFileContents'; import saveFileContents from '@/api/server/files/saveFileContents'; import FlashMessageRender from '@/components/FlashMessageRender'; import { Button } from '@/components/elements/button'; import Can from '@/components/elements/Can'; import Select from '@/components/elements/Select'; import PageContentBlock from '@/components/elements/PageContentBlock'; import { ServerError } from '@/components/elements/ScreenBlock'; import SpinnerOverlay from '@/components/elements/SpinnerOverlay'; import FileManagerBreadcrumbs from '@/components/server/files/FileManagerBreadcrumbs'; import FileNameModal from '@/components/server/files/FileNameModal'; import ErrorBoundary from '@/components/elements/ErrorBoundary'; import { Editor } from '@/components/elements/editor'; import useFlash from '@/plugins/useFlash'; import { ServerContext } from '@/state/server'; import { encodePathSegments } from '@/helpers'; export default () => { const [error, setError] = useState(''); const { action, '*': rawFilename } = useParams<{ action: 'edit' | 'new'; '*': string }>(); const [loading, setLoading] = useState(action === 'edit'); const [content, setContent] = useState(''); const [modalVisible, setModalVisible] = useState(false); const [language, setLanguage] = useState(); const [filename, setFilename] = useState(''); useEffect(() => { setFilename(decodeURIComponent(rawFilename ?? '')); }, [rawFilename]); const navigate = useNavigate(); const id = ServerContext.useStoreState(state => state.server.data!.id); const uuid = ServerContext.useStoreState(state => state.server.data!.uuid); const setDirectory = ServerContext.useStoreActions(actions => actions.files.setDirectory); const { addError, clearFlashes } = useFlash(); let fetchFileContent: null | (() => Promise) = null; useEffect(() => { if (action === 'new') { return; } if (filename === '') { return; } setError(''); setLoading(true); setDirectory(dirname(filename)); getFileContents(uuid, filename) .then(setContent) .catch(error => { console.error(error); setError(httpErrorToHuman(error)); }) .then(() => setLoading(false)); }, [action, uuid, filename]); const save = (name?: string) => { if (!fetchFileContent) { return; } setLoading(true); clearFlashes('files:view'); fetchFileContent() .then(content => saveFileContents(uuid, name ?? filename, content)) .then(() => { if (name) { navigate(`/server/${id}/files/edit/${encodePathSegments(name)}`); return; } return Promise.resolve(); }) .catch(error => { console.error(error); addError({ message: httpErrorToHuman(error), key: 'files:view' }); }) .then(() => setLoading(false)); }; if (error) { // TODO: onBack return ; } return (
{filename === '.pteroignore' ? (

You're editing a .pteroignore{' '} file. Any files or directories listed in here will be excluded from backups. Wildcards are supported by using an asterisk (*). You can negate a prior rule by prepending an exclamation point ( !).

) : null} setModalVisible(false)} onFileNamed={name => { setModalVisible(false); save(name); }} />
{ setLanguage(l); }} fetchContent={value => { fetchFileContent = value; }} onContentSaved={() => { if (action !== 'edit') { setModalVisible(true); } else { save(); } }} />
{action === 'edit' ? ( ) : ( )}
); };