UI: Do not close scripts in editor when their servers are deleted (#2049)

This commit is contained in:
catloversg
2025-03-30 10:26:45 +07:00
committed by GitHub
parent 12d18c21f1
commit 0900f46edb
4 changed files with 18 additions and 12 deletions

View File

@@ -2,4 +2,5 @@
export enum RamCalculationErrorCode {
SyntaxError = -1,
ImportError = -2,
InvalidServer = -3,
}

View File

@@ -45,6 +45,9 @@ export function ScriptEditorContextProvider({ children }: { children: React.Reac
case RamCalculationErrorCode.ImportError:
errorType = "Import Error";
break;
case RamCalculationErrorCode.InvalidServer:
errorType = "Invalid server";
break;
default:
errorType = "Unknown Error";
break;

View File

@@ -180,14 +180,6 @@ function Root(props: IProps): React.ReactElement {
let decorations: monaco.editor.IEditorDecorationsCollection | undefined;
// Prevent Crash if script is open on deleted server
for (let i = openScripts.length - 1; i >= 0; i--) {
GetServer(openScripts[i].hostname) === null && openScripts.splice(i, 1);
}
if (currentScript && GetServer(currentScript.hostname) === null) {
currentScript = openScripts[0] ?? null;
}
const save = useCallback(() => {
if (currentScript === null) {
console.error("currentScript is null when it shouldn't be. Unable to save script");
@@ -304,10 +296,17 @@ function Root(props: IProps): React.ReactElement {
const debouncedCodeParsing = debounce((newCode: string) => {
let server;
if (!currentScript || !hasScriptExtension(currentScript.path) || !(server = GetServer(currentScript.hostname))) {
if (!currentScript || !hasScriptExtension(currentScript.path)) {
showRAMError();
return;
}
if (!(server = GetServer(currentScript.hostname))) {
showRAMError({
errorCode: RamCalculationErrorCode.InvalidServer,
errorMessage: `Server ${currentScript.hostname} does not exist`,
});
return;
}
let ast;
try {
ast = parseAST(currentScript.path, currentScript.hostname, newCode, getFileType(currentScript.path));
@@ -398,7 +397,8 @@ function Root(props: IProps): React.ReactElement {
function saveScript(scriptToSave: OpenScript): void {
const server = GetServer(scriptToSave.hostname);
if (!server) {
throw new Error("Server should not be null but it is.");
dialogBoxCreate(`Server ${scriptToSave.hostname} does not exist.`);
return;
}
// Show a warning message if the file is on a non-home server.
if (scriptToSave.hostname !== SpecialServers.Home) {
@@ -478,7 +478,7 @@ function Root(props: IProps): React.ReactElement {
const indexOffset = openScripts.length === index ? -1 : 0;
currentScript = openScripts[index + indexOffset];
if (editorRef.current !== null) {
if (currentScript.model.isDisposed() || !currentScript.model) {
if (!currentScript.model || currentScript.model.isDisposed()) {
currentScript.regenerateModel();
}
editorRef.current.setModel(currentScript.model);

View File

@@ -7,7 +7,9 @@ import { throwIfReachable } from "../../utils/helpers/throwIfReachable";
function getServerCode(scripts: OpenScript[], index: number): string | null {
const openScript = scripts[index];
const server = GetServer(openScript.hostname);
if (server === null) throw new Error(`Server '${openScript.hostname}' should not be null, but it is.`);
if (server === null) {
return null;
}
const data = server.getContentFile(openScript.path)?.content ?? null;
return data;
}