import { createContext, useContext, useState } from 'react'; import Button from '@/components/elements/Button'; import CodemirrorEditor from '@/components/elements/LazyCodemirrorEditor'; import SpinnerOverlay from '@/components/elements/SpinnerOverlay'; import Select from '@/components/ui/Select'; import modes from '@/modes'; import { cn } from '@/lib/cn'; import type { ComponentPartProps, ComponentParts, DefaultComponentProps, FileEditorModel, } from '@/extensions/componentTypes'; export interface FileEditorSession { model: FileEditorModel; read: () => string; setLanguage: (language: string) => void; } export const FileEditorContext = createContext(null); const findModeByFilename = (filename: string) => { const match = modes.find((mode) => mode.file?.test(filename)); const dot = filename.lastIndexOf('.'); const ext = dot > -1 ? filename.slice(dot + 1) : undefined; return match ?? (ext ? modes.find((mode) => mode.ext?.includes(ext)) : undefined); }; export const modeForFile = (filename: string) => findModeByFilename(filename)?.mime || 'text/plain'; type PartProps = ComponentPartProps<'server.files.editor'>; function Notice({ model }: PartProps) { return model.name.endsWith('.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; } function Editor({ model }: PartProps) { const { read } = useContext(FileEditorContext)!; // Starts from the live buffer, which may be ahead of the opened content. const [initialContent] = useState(read); return (
void model.save()} />
); } function Language({ model }: PartProps) { const { setLanguage } = useContext(FileEditorContext)!; return (