Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 28 additions & 7 deletions src/components/CodeEditor/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import { useEffect, useRef, useState } from "react";
import { Button, CopyText, Icon, Select } from "@stellar/design-system";
import MonacoEditor, { useMonaco, type OnMount } from "@monaco-editor/react";
import MonacoEditor, { type OnMount, loader } from "@monaco-editor/react";

import { useStore } from "@/store/useStore";
import { Box } from "@/components/layout/Box";
Expand Down Expand Up @@ -47,22 +47,43 @@ export const CodeEditor = ({
maxHeightInRem,
}: CodeEditorProps) => {
const { theme } = useStore();
const monaco = useMonaco();
const headerEl = useRef<HTMLDivElement | null>(null);

const [isReady, setIsReady] = useState(false);
const [isReady, setIsReady] = useState(
// If Monaco is already loaded (e.g., another editor was mounted first),
// skip the async init to avoid a flicker.
() => {
const getMonacoInstance = (loader as any).__getMonacoInstance;
return typeof getMonacoInstance === "function" && getMonacoInstance() !== null;
},
);
const [isExpanded, setIsExpanded] = useState(false);

// Default header height is 43px
const headerHeight = headerEl?.current?.clientHeight || 43;

const [autoHeight, setAutoHeight] = useState<string | null>(null);

// Load Monaco without the unhandled-rejection bug that exists in the
// useMonaco() hook shipped by @monaco-editor/react: its internal
// loader.init().then(...) call has NO .catch(), so when React StrictMode
// double-invokes effects the cleanup calls loader.cancel() which rejects
// the promise with a plain { type: 'cancelation' } object, surfacing as
// '[object Object]' in Next.js's onUnhandledRejection handler.
useEffect(() => {
if (typeof window !== "undefined" && monaco) {
setIsReady(true);
}
}, [monaco]);
if (isReady) return;

const initPromise = loader.init();
initPromise
.then(() => setIsReady(true))
// Swallow both intentional cancellations and unexpected errors so the
// promise rejection never escapes to the global unhandledrejection event.
.catch(() => {});

return () => {
initPromise.cancel();
};
}, [isReady]);

if (!isReady) {
return null;
Expand Down