import { useEffect, useRef, useState } from "react"; import { assertExhausted } from "@userscript-proxy/core/assertions"; import type { ErrorInfo } from "@userscript-proxy/core/errors"; import type { NoRejectPromise } from "@userscript-proxy/core/promises"; import "./ScriptEditorView.css"; type ScriptEditorState = | { tag: "Editing"; content: string; error: string | null } | { tag: "Saving"; content: string }; type Props = { filename: string; initialContent: string; onSave_NoReject: (content: string) => NoRejectPromise; onClose: () => void; }; export function ScriptEditorView({ filename, initialContent, onSave_NoReject, onClose, }: Props) { const [state, setState] = useState({ tag: "Editing", content: initialContent, error: null, }); const textareaRef = useRef(null); useEffect(() => { const textarea = textareaRef.current; if (textarea !== null) { textarea.focus(); textarea.setSelectionRange(textarea.value.length, textarea.value.length); } }, []); const isSaving = state.tag === "Saving"; const content = state.content; const error = state.tag === "Editing" ? state.error : null; return (

{filename}

{error !== null &&

{error}

}