Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(frontend): load monaco lazily via a CodeEditor facade (#62957)

Paul D'Ambra2d3c8b997f48
diff --git a/.oxlintrc.json b/.oxlintrc.json
index 98cef8e0d2bc..a429f0b20e2b 100644
--- a/.oxlintrc.json
+++ b/.oxlintrc.json
@@ -101,6 +101,19 @@
{
"name": "~/queries/schema",
"message": "Import from 'queries/schema/schema-foo' to avoid Webpack/Sucrase enum export issues."
+ },
+ {
+ "name": "monaco-editor",
+ "message": "Value imports of monaco-editor statically couple ~4 MB of editor code into your chunk. Use `import type`, or render via the lazy facade in lib/monaco/CodeEditor.",
+ "allowTypeImports": true
+ },
+ {
+ "name": "lib/monaco/CodeEditorImpl",
+ "message": "Import CodeEditor from lib/monaco/CodeEditor instead — the lazy facade keeps monaco out of your chunk."
+ },
+ {
+ "name": "~/lib/monaco/CodeEditorImpl",
+ "message": "Import CodeEditor from lib/monaco/CodeEditor instead — the lazy facade keeps monaco out of your chunk."
}
]
}
@@ -196,6 +209,39 @@
"rules": {
"react/forbid-elements": "off"
}
+ },
+ {
+ "files": [
+ "frontend/src/lib/monaco/**",
+ "frontend/src/lib/components/MonacoDiffEditor.tsx",
+ "frontend/src/scenes/data-warehouse/editor/sqlEditorLogic.tsx",
+ "frontend/src/scenes/hog-functions/configuration/HogFunctionTest.tsx"
+ ],
+ "rules": {
+ "no-restricted-imports": [
+ "error",
+ {
+ "paths": [
+ {
+ "name": "dayjs",
+ "message": "Do not directly import dayjs. Only import the dayjs exported from 'lib/dayjs'."
+ },
+ {
+ "name": "chart.js",
+ "message": "Do not directly import chart.js. Only import the Chart and friends exported from 'lib/Chart'."
+ },
+ {
+ "name": "chart.js/auto",
+ "message": "Do not directly import chart.js/auto. Only import the Chart and friends exported from 'lib/Chart'."
+ },
+ {
+ "name": "~/queries/schema",
+ "message": "Import from 'queries/schema/schema-foo' to avoid Webpack/Sucrase enum export issues."
+ }
+ ]
+ }
+ ]
+ }
}
]
}
diff --git a/frontend/src/lib/monaco/CodeEditor.tsx b/frontend/src/lib/monaco/CodeEditor.tsx
index cbcbe534dc91..90e51cefc1f0 100644
--- a/frontend/src/lib/monaco/CodeEditor.tsx
+++ b/frontend/src/lib/monaco/CodeEditor.tsx
@@ -1,572 +1,25 @@
-import './CodeEditor.scss'
+import { Suspense, lazy } from 'react'
-import MonacoEditor, { type EditorProps, Monaco, DiffEditor as MonacoDiffEditor } from '@monaco-editor/react'
-import { BuiltLogic, useActions, useMountedLogic, useValues } from 'kea'
-import * as monacoModule from 'monaco-editor'
-import { IDisposable, editor, editor as importedEditor } from 'monaco-editor'
-import { useEffect, useRef, useState } from 'react'
-
-import 'lib/monaco/monacoEnvironment'
-import { useOnMountEffect } from 'lib/hooks/useOnMountEffect'
-import { usePageVisibility } from 'lib/hooks/usePageVisibility'
import { Spinner } from 'lib/lemon-ui/Spinner'
-import { codeEditorLogic } from 'lib/monaco/codeEditorLogic'
-import { codeEditorLogicType } from 'lib/monaco/codeEditorLogicType'
-import { findNextFocusableElement, findPreviousFocusableElement } from 'lib/monaco/domUtils'
-import { initHogLanguage } from 'lib/monaco/languages/hog'
-import { initHogJsonLanguage } from 'lib/monaco/languages/hogJson'
-import { initHogQLLanguage } from 'lib/monaco/languages/hogQL'
-import { initHogTemplateLanguage } from 'lib/monaco/languages/hogTemplate'
-import { initLiquidLanguage } from 'lib/monaco/languages/liquid'
-import { sharedMonacoOverflowRoot } from 'lib/monaco/sharedMonacoOverflowRoot'
-import { inStorybookTestRunner } from 'lib/utils'
-
-import { themeLogic } from '~/layout/navigation-3000/themeLogic'
-import { AnyDataNode, HogLanguage, HogQLMetadataResponse, NodeKind } from '~/queries/schema/schema-general'
-
-export interface CodeEditorProps extends Omit<EditorProps, 'loading' | 'theme'> {
- queryKey?: string
- autocompleteContext?: string
- onPressCmdEnter?: (value: string, selectionType: 'selection' | 'full') => void
- /** Run the innermost subquery at cursor (Cmd+Shift+Enter) */
- onPressCmdShiftEnter?: () => void
- /** Pressed up in an empty code editor, likely to edit the previous message in a list */
- onPressUpNoValue?: () => void
- autoFocus?: boolean
- sourceQuery?: AnyDataNode
- globals?: Record<string, any>
- schema?: Record<string, any> | null
- onMetadata?: (metadata: HogQLMetadataResponse | null) => void
- onMetadataLoading?: (loading: boolean) => void
- onFixWithAI?: (prompt: string) => void
- onError?: (error: string | null) => void
- /** Override the query sent for metadata validation (e.g. active query in multi-query mode) */
- metadataQuery?: string
- /** Character offset of metadataQuery within the full editor text, for correct marker positioning */
- metadataQueryOffset?: number
- /** The original value to compare against - renders it in diff mode */
- originalValue?: string
- /** Enable vim keybindings */
- enableVimMode?: boolean
-}
-let codeEditorIndex = 0
-
-export function initModel(model: editor.ITextModel, builtCodeEditorLogic: BuiltLogic<codeEditorLogicType>): void {
- ;(model as any).codeEditorLogic = builtCodeEditorLogic
-}
-
-export function clearLogicReference(model: editor.ITextModel): void {
- ;(model as any).codeEditorLogic = undefined
-}
-
-function initEditor(
- monaco: Monaco,
- editor: importedEditor.IStandaloneCodeEditor,
- editorProps: Omit<CodeEditorProps, 'options' | 'onMount' | 'queryKey' | 'value'>,
- options: editor.IStandaloneEditorConstructionOptions,
- builtCodeEditorLogic: BuiltLogic<codeEditorLogicType>
-): void {
- // This gives autocomplete access to the specific editor
- const model = editor.getModel()
- if (model) {
- initModel(model, builtCodeEditorLogic)
- }
-
- if (editorProps?.language === 'hog') {
- initHogLanguage(monaco)
- }
- if (editorProps?.language === 'hogQL' || editorProps?.language === 'hogQLExpr') {
- initHogQLLanguage(monaco, editorProps.language as HogLanguage)
- }
- if (editorProps?.language === 'hogTemplate') {
- initHogTemplateLanguage(monaco)
- }
- if (editorProps?.language === 'hogJson') {
- initHogJsonLanguage(monaco)
- }
- if (editorProps?.language === 'liquid') {
- initLiquidLanguage(monaco)
- }
-
- editor.onKeyDown((evt) => {
- if (evt.keyCode === monaco.KeyCode.Space) {
- evt.stopPropagation()
- }
-
- if (options.tabFocusMode) {
- if (evt.keyCode === monaco.KeyCode.Tab && !evt.metaKey && !evt.ctrlKey) {
- const selection = editor.getSelection()
- if (
- selection &&
- (selection.startColumn !== selection.endColumn ||
- selection.startLineNumber !== selection.endLineNumber)
- ) {
- return
- }
- evt.preventDefault()
- evt.stopPropagation()
-
- const element: HTMLElement | null = evt.target?.parentElement?.parentElement?.parentElement ?? null
- if (!element) {
- return
- }
- const nextElement = evt.shiftKey
- ? findPreviousFocusableElement(element)
- : findNextFocusableElement(element)
-
- if (nextElement && 'focus' in nextElement) {
- nextElement.focus()
- }
- }
- }
- if (editorProps.onPressUpNoValue) {
- if (evt.keyCode === monaco.KeyCode.UpArrow && !evt.metaKey && !evt.ctrlKey && editor.getValue() === '') {
- evt.preventDefault()
- evt.stopPropagation()
- editorProps.onPressUpNoValue()
- }
- }
- })
-}
-export function CodeEditor({
- queryKey,
- options,
- onMount,
- value,
- onPressCmdEnter,
- onPressCmdShiftEnter,
- autoFocus,
- globals,
- sourceQuery,
- schema,
- onError,
- onMetadata,
- onMetadataLoading,
- onFixWithAI,
- metadataQuery,
- metadataQueryOffset,
- originalValue,
- enableVimMode,
- ...editorProps
-}: CodeEditorProps): JSX.Element {
- const { isDarkModeOn } = useValues(themeLogic)
- const scrollbarRendering = !inStorybookTestRunner() ? 'auto' : 'hidden'
- const [monacoAndEditor, setMonacoAndEditor] = useState(
- null as [Monaco, importedEditor.IStandaloneCodeEditor] | null
- )
- const [monaco, editor] = monacoAndEditor ?? []
-
- // Keep a ref to the editor for cleanup - ensures we can dispose it even if state is stale
- const editorRef = useRef<importedEditor.IStandaloneCodeEditor | null>(null)
- // In diff mode editorRef holds the inner modified sub-editor; this
- // ref holds the parent diff editor so cleanup can dispose the whole
- // diff (original editor, diff widget, decorations, view zones).
- const diffEditorRef = useRef<importedEditor.IStandaloneDiffEditor | null>(null)
-
- const vimModeRef = useRef<{ dispose: () => void } | null>(null)
- const vimStatusBarRef = useRef<HTMLDivElement | null>(null)
-
- const [realKey] = useState(() => codeEditorIndex++)
- const builtCodeEditorLogic = codeEditorLogic({
- key: queryKey ?? `new/${realKey}`,
- query: value ?? '',
- metadataQuery: metadataQuery,
- metadataQueryOffset: metadataQueryOffset,
- language: editorProps.language ?? 'text',
- globals,
- sourceQuery,
- monaco: monaco,
- editor: editor,
- onError,
- onMetadata,
- onMetadataLoading,
- onFixWithAI,
- metadataFilters: sourceQuery?.kind === NodeKind.HogQLQuery ? sourceQuery.filters : undefined,
- })
- useMountedLogic(builtCodeEditorLogic)
-
- const { vimCommandHistory } = useValues(builtCodeEditorLogic)
- const { appendVimCommand } = useActions(builtCodeEditorLogic)
-
- const { isVisible } = usePageVisibility()
-
- const monacoRoot = sharedMonacoOverflowRoot()
-
- // Using useRef, not useState, as we don't want to reload the component when this changes.
- const monacoDisposables = useRef([] as IDisposable[])
- const mutationObserver = useRef<MutationObserver | null>(null)
- // Track every model this editor instance has been attached to, so we can
- // dispose them on unmount. Monaco models live in a global registry until
- // explicitly disposed; without this, models accumulate forever and retain
- // their attached `codeEditorLogic` BuiltLogic via `(model as any).codeEditorLogic`.
- const editorModelsRef = useRef<Set<editor.ITextModel>>(new Set())
- // Live ref to the Monaco API so the cleanup closure (captured by
- // `useOnMountEffect`'s [] dep) can read the *current* value rather than
- // the `null` it had at mount time. Without this the `stillInUse` guard
- // in `disposeTrackedModels` is dead code: `monaco.editor.getEditors()`
- // returns `[]` and every tracked model is unconditionally disposed.
- const monacoApiRef = useRef<Monaco | null>(null)
-
- const disposeMonacoDisposables = (): void => {
- monacoDisposables.current.forEach((d) => d?.dispose())
- monacoDisposables.current = []
- }
-
- const disconnectMutationObserver = (): void => {
- mutationObserver.current?.disconnect()
- mutationObserver.current = null
- }
-
- const disposeTrackedModels = (): void => {
- const models = editorModelsRef.current
- if (models.size === 0) {
- return
- }
- const monacoApi = monacoApiRef.current
- // Skip a model if any OTHER live editor still holds it as its current
- // model — disposing would break that editor, and nulling its
- // `codeEditorLogic` would silently break HogQL autocomplete /
- // metadata providers in the surviving editor.
- const otherEditors = (monacoApi?.editor.getEditors?.() ?? []).filter(
- (e: importedEditor.ICodeEditor) => e !== editorRef.current
- )
- for (const model of models) {
- if (model.isDisposed()) {
- continue
- }
- const stillInUse = otherEditors.some((e: importedEditor.ICodeEditor) => e.getModel() === model)
- if (stillInUse) {
- continue
- }
- // Null the back-reference only on models we're actually about to
- // dispose. Doing it for shared models would break consumers
- // (e.g. hogQLAutocompleteProvider, hogQLMetadataProvider) that
- // read `model.codeEditorLogic` to look up logic state.
- clearLogicReference(model)
- try {
- model.dispose()
- } catch {
- // already disposed or in invalid state
- }
- }
- models.clear()
- }
-
- const trackEditorModels = (editorInstance: importedEditor.IStandaloneCodeEditor, monacoApi: Monaco): void => {
- monacoApiRef.current = monacoApi
- const initial = editorInstance.getModel()
- if (initial) {
- editorModelsRef.current.add(initial)
- }
- const disposable = editorInstance.onDidChangeModel((e) => {
- if (!e.newModelUrl) {
- return
- }
- const next = monacoApi.editor.getModel(e.newModelUrl)
- if (next) {
- editorModelsRef.current.add(next)
- }
- })
- monacoDisposables.current.push(disposable)
- }
+import type { CodeEditorProps } from './CodeEditorImpl'
- const disposeEditor = (): void => {
- try {
- if (diffEditorRef.current) {
- diffEditorRef.current.dispose()
- } else {
- editorRef.current?.dispose()
- }
- } catch {
- // already disposed
- }
- editorRef.current = null
- diffEditorRef.current = null
- }
-
- useOnMountEffect(() => {
- return () => {
- disposeMonacoDisposables()
- disconnectMutationObserver()
-
- // Dispose the editor BEFORE @monaco-editor/react's own cleanup
- // runs: Monaco's services (HoverService, ContextView,
- // DomListener) keep refs to the editor's container DOM that
- // survive the library's dispose. Disposing while we still hold
- // a strong reference lets Monaco tear down its services in an
- // order that releases those DOM refs.
- disposeEditor()
-
- // Now that our editor is disposed, dispose every model this
- // editor used and was uniquely owned by us. `disposeTrackedModels`
- // also nulls the `codeEditorLogic` back-reference on each
- // disposed model. Models still held by another live editor are
- // skipped on both counts, so HogQL autocomplete/metadata
- // providers (which read `model.codeEditorLogic`) keep working
- // for the surviving editor.
- disposeTrackedModels()
-
- setMonacoAndEditor(null)
-
- // Do NOT remove monacoRoot — it's a shared singleton that
- // Monaco's global services have permanent DomListeners on.
- }
- })
-
- useEffect(() => {
- if (!monaco) {
- return
- }
- monacoModule.typescript.typescriptDefaults.setCompilerOptions({
- jsx: editorProps?.path?.endsWith('.tsx')
- ? monacoModule.typescript.JsxEmit.React
- : monacoModule.typescript.JsxEmit.Preserve,
- esModuleInterop: true,
- })
- }, [monaco, editorProps.path])
-
- useEffect(() => {
- if (!monaco) {
- return
- }
- monacoModule.json.jsonDefaults.setDiagnosticsOptions({
- validate: true,
- schemas: schema
- ? [
- {
- uri: 'http://internal/node-schema.json',
- fileMatch: ['*'],
- schema: schema,
- },
- ]
- : [],
- })
- }, [monaco, schema])
-
- useEffect(() => {
- if (!editor) {
- return
- }
-
- let cancelled = false
-
- if (enableVimMode && vimStatusBarRef.current) {
- const statusBar = vimStatusBarRef.current
- void import('lib/monaco/vimMode').then(({ setupVimMode }) => {
- if (cancelled) {
- return
- }
- vimModeRef.current = setupVimMode(editor, statusBar, {
- initialHistory: vimCommandHistory,
- onCommandExecuted: appendVimCommand,
- })
- })
- } else if (vimModeRef.current) {
- vimModeRef.current.dispose()
- vimModeRef.current = null
- }
-
- return () => {
- cancelled = true
- if (vimModeRef.current) {
- vimModeRef.current.dispose()
- vimModeRef.current = null
- }
- }
- }, [editor, enableVimMode, vimCommandHistory, appendVimCommand])
-
- const editorOptions: editor.IStandaloneEditorConstructionOptions = {
- minimap: {
- enabled: false,
- },
- scrollBeyondLastLine: false,
- automaticLayout: true,
- fixedOverflowWidgets: true,
- glyphMargin: false,
- folding: true,
- wordWrap: 'off',
- lineNumbers: 'on',
- tabFocusMode: false,
- overviewRulerBorder: true,
- hideCursorInOverviewRuler: false,
- overviewRulerLanes: 3,
- overflowWidgetsDomNode: monacoRoot,
- ...options,
- padding: { bottom: enableVimMode ? 28 : 8, top: 8 },
- scrollbar: {
- vertical: scrollbarRendering,
- horizontal: scrollbarRendering,
- alwaysConsumeMouseWheel: false,
- ...options?.scrollbar,
- },
- }
-
- const editorOnMount = (editor: importedEditor.IStandaloneCodeEditor, monaco: Monaco): void => {
- editorRef.current = editor
- trackEditorModels(editor, monaco)
- setMonacoAndEditor([monaco, editor])
- initEditor(monaco, editor, editorProps, options ?? {}, builtCodeEditorLogic)
-
- // Override Monaco's suggestion widget styling to prevent truncation
- const styleId = 'monaco-suggestion-widget-fix'
- const overrideSuggestionWidgetStyling = (): void => {
- // Only add style tag if it doesn't already exist
- if (!document.getElementById(styleId)) {
- const style = document.createElement('style')
- style.id = styleId
- style.textContent = `
- .monaco-editor .suggest-widget .monaco-list .monaco-list-row.string-label>.contents>.main>.left>.monaco-icon-label {
- flex-shrink: 0;
- }
- `
- document.head.appendChild(style)
- }
- }
-
- // Apply styling immediately
- overrideSuggestionWidgetStyling()
-
- // Monitor for suggestion widget creation and apply styling
- const observer = new MutationObserver(() => {
- const suggestWidget = document.querySelector('.monaco-editor .suggest-widget')
- if (suggestWidget) {
- overrideSuggestionWidgetStyling()
- }
- })
-
- mutationObserver.current = observer
- observer.observe(document.body, { childList: true, subtree: true })
-
- // Clean up observers
- monacoDisposables.current.push({
- dispose: () => observer.disconnect(),
- })
-
- monacoDisposables.current.push(
- monaco.editor.registerCommand('posthog.hogql.fixWithAI', (_: unknown, prompt: unknown) => {
- if (typeof prompt === 'string' && prompt.length > 0) {
- onFixWithAI?.(prompt)
- }
- })
- )
-
- if (onPressCmdEnter) {
- monacoDisposables.current.push(
- editor.addAction({
- id: 'saveAndRunPostHog',
- label: 'Save and run query',
- keybindings: [monaco.KeyMod.CtrlCmd | monaco.KeyCode.Enter],
- run: () => {
- const selection = editor.getSelection()
- const model = editor.getModel()
- if (selection && model) {
- const highlightedText = model.getValueInRange(selection)
- onPressCmdEnter(highlightedText, 'selection')
- return
- }
-
- onPressCmdEnter(editor.getValue(), 'full')
- },
- })
- )
- }
- if (onPressCmdShiftEnter) {
- monacoDisposables.current.push(
- editor.addAction({
- id: 'runSubqueryPostHog',
- label: 'Run subquery at cursor',
- keybindings: [monaco.KeyMod.CtrlCmd | monaco.KeyMod.Shift | monaco.KeyCode.Enter],
- run: () => {
- onPressCmdShiftEnter()
- },
- })
- )
- }
- if (autoFocus) {
- editor.focus()
- const model = editor.getModel()
- if (model) {
- editor.setPosition({
- column: model.getLineContent(model.getLineCount()).length + 1,
- lineNumber: model.getLineCount(),
- })
- }
- }
-
- onMount?.(editor, monaco)
- }
-
- useEffect(() => {
- if (!mutationObserver.current) {
- return
- }
-
- if (isVisible) {
- mutationObserver.current.observe(document.body, { childList: true, subtree: true })
- } else {
- mutationObserver.current.disconnect()
- }
- }, [isVisible])
-
- if (originalValue) {
- // If originalValue is provided, we render a diff editor instead
- const diffEditorOnMount = (diff: importedEditor.IStandaloneDiffEditor, monaco: Monaco): void => {
- const modifiedEditor = diff.getModifiedEditor()
- editorRef.current = modifiedEditor
- diffEditorRef.current = diff
- trackEditorModels(modifiedEditor, monaco)
- const original = diff.getOriginalEditor().getModel()
- if (original) {
- editorModelsRef.current.add(original)
- }
- setMonacoAndEditor([monaco, modifiedEditor])
-
- if (editorProps.onChange) {
- const disposable = modifiedEditor.onDidChangeModelContent((event: editor.IModelContentChangedEvent) => {
- editorProps.onChange?.(modifiedEditor.getValue(), event)
- })
- monacoDisposables.current.push(disposable)
- }
- onMount?.(modifiedEditor, monaco)
- }
+export type { CodeEditorProps } from './CodeEditorImpl'
+export { clearLogicReference, initModel } from './modelLogicReference'
- return (
- <MonacoDiffEditor
- key={queryKey}
- loading={<Spinner />}
- theme={isDarkModeOn ? 'vs-dark' : 'vs-light'}
- original={originalValue}
- modified={value}
- options={{
- ...editorOptions,
- renderSideBySide: false,
- acceptSuggestionOnEnter: 'on',
- renderGutterMenu: false,
- }}
- onMount={diffEditorOnMount}
- {...editorProps}
- />
- )
- }
+const LazyCodeEditor = lazy(() => import('./CodeEditorImpl').then((m) => ({ default: m.CodeEditor })))
+/** Lazy facade so importing CodeEditor doesn't pull monaco (~4 MB) into the importer's chunk. */
+export function CodeEditor(props: CodeEditorProps): JSX.Element {
return (
- <div className="CodeEditor relative h-full w-full">
- <MonacoEditor // eslint-disable-line react/forbid-elements
- key={queryKey}
- theme={isDarkModeOn ? 'vs-dark' : 'vs-light'}
- loading={<Spinner />}
- value={value}
- options={editorOptions}
- onMount={editorOnMount}
- {...editorProps}
- />
- {enableVimMode && (
- <div
- ref={vimStatusBarRef}
- className="CodeEditor__vim-status-bar absolute bottom-0 left-0 right-0 font-mono text-xs px-2 py-0.5 bg-bg-light border-t z-10"
- />
- )}
- </div>
+ <Suspense
+ fallback={
+ <div className="CodeEditor relative h-full w-full">
+ <Spinner />
+ </div>
+ }
+ >
+ <LazyCodeEditor {...props} />
+ </Suspense>
)
}
diff --git a/frontend/src/lib/monaco/CodeEditor.scss b/frontend/src/lib/monaco/CodeEditorImpl.scss
similarity index 100%
rename from frontend/src/lib/monaco/CodeEditor.scss
rename to frontend/src/lib/monaco/CodeEditorImpl.scss
diff --git a/frontend/src/lib/monaco/CodeEditorImpl.tsx b/frontend/src/lib/monaco/CodeEditorImpl.tsx
new file mode 100644
index 000000000000..757e91717ba7
--- /dev/null
+++ b/frontend/src/lib/monaco/CodeEditorImpl.tsx
@@ -0,0 +1,567 @@
+import './CodeEditorImpl.scss'
+
+import MonacoEditor, { type EditorProps, Monaco, DiffEditor as MonacoDiffEditor } from '@monaco-editor/react'
+import { BuiltLogic, useActions, useMountedLogic, useValues } from 'kea'
+import * as monacoModule from 'monaco-editor'
+import { IDisposable, editor, editor as importedEditor } from 'monaco-editor'
+import { useEffect, useRef, useState } from 'react'
+
+import 'lib/monaco/monacoEnvironment'
+import { useOnMountEffect } from 'lib/hooks/useOnMountEffect'
+import { usePageVisibility } from 'lib/hooks/usePageVisibility'
+import { Spinner } from 'lib/lemon-ui/Spinner'
+import { codeEditorLogic } from 'lib/monaco/codeEditorLogic'
+import { codeEditorLogicType } from 'lib/monaco/codeEditorLogicType'
+import { findNextFocusableElement, findPreviousFocusableElement } from 'lib/monaco/domUtils'
+import { initHogLanguage } from 'lib/monaco/languages/hog'
+import { initHogJsonLanguage } from 'lib/monaco/languages/hogJson'
+import { initHogQLLanguage } from 'lib/monaco/languages/hogQL'
+import { initHogTemplateLanguage } from 'lib/monaco/languages/hogTemplate'
+import { initLiquidLanguage } from 'lib/monaco/languages/liquid'
+import { clearLogicReference, initModel } from 'lib/monaco/modelLogicReference'
+import { sharedMonacoOverflowRoot } from 'lib/monaco/sharedMonacoOverflowRoot'
+import { inStorybookTestRunner } from 'lib/utils'
+
+import { themeLogic } from '~/layout/navigation-3000/themeLogic'
+import { AnyDataNode, HogLanguage, HogQLMetadataResponse, NodeKind } from '~/queries/schema/schema-general'
+
+export interface CodeEditorProps extends Omit<EditorProps, 'loading' | 'theme'> {
+ queryKey?: string
+ autocompleteContext?: string
+ onPressCmdEnter?: (value: string, selectionType: 'selection' | 'full') => void
+ /** Run the innermost subquery at cursor (Cmd+Shift+Enter) */
+ onPressCmdShiftEnter?: () => void
+ /** Pressed up in an empty code editor, likely to edit the previous message in a list */
+ onPressUpNoValue?: () => void
+ autoFocus?: boolean
+ sourceQuery?: AnyDataNode
+ globals?: Record<string, any>
+ schema?: Record<string, any> | null
+ onMetadata?: (metadata: HogQLMetadataResponse | null) => void
+ onMetadataLoading?: (loading: boolean) => void
+ onFixWithAI?: (prompt: string) => void
+ onError?: (error: string | null) => void
+ /** Override the query sent for metadata validation (e.g. active query in multi-query mode) */
+ metadataQuery?: string
+ /** Character offset of metadataQuery within the full editor text, for correct marker positioning */
+ metadataQueryOffset?: number
+ /** The original value to compare against - renders it in diff mode */
+ originalValue?: string
+ /** Enable vim keybindings */
+ enableVimMode?: boolean
+}
+let codeEditorIndex = 0
+
+function initEditor(
+ monaco: Monaco,
+ editor: importedEditor.IStandaloneCodeEditor,
+ editorProps: Omit<CodeEditorProps, 'options' | 'onMount' | 'queryKey' | 'value'>,
+ options: editor.IStandaloneEditorConstructionOptions,
+ builtCodeEditorLogic: BuiltLogic<codeEditorLogicType>
+): void {
+ // This gives autocomplete access to the specific editor
+ const model = editor.getModel()
+ if (model) {
+ initModel(model, builtCodeEditorLogic)
+ }
+
+ if (editorProps?.language === 'hog') {
+ initHogLanguage(monaco)
+ }
+ if (editorProps?.language === 'hogQL' || editorProps?.language === 'hogQLExpr') {
+ initHogQLLanguage(monaco, editorProps.language as HogLanguage)
+ }
+ if (editorProps?.language === 'hogTemplate') {
+ initHogTemplateLanguage(monaco)
+ }
+ if (editorProps?.language === 'hogJson') {
+ initHogJsonLanguage(monaco)
+ }
+ if (editorProps?.language === 'liquid') {
+ initLiquidLanguage(monaco)
+ }
+
+ editor.onKeyDown((evt) => {
+ if (evt.keyCode === monaco.KeyCode.Space) {
+ evt.stopPropagation()
+ }
+
+ if (options.tabFocusMode) {
+ if (evt.keyCode === monaco.KeyCode.Tab && !evt.metaKey && !evt.ctrlKey) {
+ const selection = editor.getSelection()
+ if (
+ selection &&
+ (selection.startColumn !== selection.endColumn ||
+ selection.startLineNumber !== selection.endLineNumber)
+ ) {
+ return
+ }
+ evt.preventDefault()
+ evt.stopPropagation()
+
+ const element: HTMLElement | null = evt.target?.parentElement?.parentElement?.parentElement ?? null
+ if (!element) {
+ return
+ }
+ const nextElement = evt.shiftKey
+ ? findPreviousFocusableElement(element)
+ : findNextFocusableElement(element)
+
+ if (nextElement && 'focus' in nextElement) {
+ nextElement.focus()
+ }
+ }
+ }
+ if (editorProps.onPressUpNoValue) {
+ if (evt.keyCode === monaco.KeyCode.UpArrow && !evt.metaKey && !evt.ctrlKey && editor.getValue() === '') {
+ evt.preventDefault()
+ evt.stopPropagation()
+ editorProps.onPressUpNoValue()
+ }
+ }
+ })
+}
+
+export function CodeEditor({
+ queryKey,
+ options,
+ onMount,
+ value,
+ onPressCmdEnter,
+ onPressCmdShiftEnter,
+ autoFocus,
+ globals,
+ sourceQuery,
+ schema,
+ onError,
+ onMetadata,
+ onMetadataLoading,
+ onFixWithAI,
+ metadataQuery,
+ metadataQueryOffset,
+ originalValue,
+ enableVimMode,
+ ...editorProps
+}: CodeEditorProps): JSX.Element {
+ const { isDarkModeOn } = useValues(themeLogic)
+ const scrollbarRendering = !inStorybookTestRunner() ? 'auto' : 'hidden'
+ const [monacoAndEditor, setMonacoAndEditor] = useState(
+ null as [Monaco, importedEditor.IStandaloneCodeEditor] | null
+ )
+ const [monaco, editor] = monacoAndEditor ?? []
+
+ // Keep a ref to the editor for cleanup - ensures we can dispose it even if state is stale
+ const editorRef = useRef<importedEditor.IStandaloneCodeEditor | null>(null)
+ // In diff mode editorRef holds the inner modified sub-editor; this
+ // ref holds the parent diff editor so cleanup can dispose the whole
+ // diff (original editor, diff widget, decorations, view zones).
+ const diffEditorRef = useRef<importedEditor.IStandaloneDiffEditor | null>(null)
+
+ const vimModeRef = useRef<{ dispose: () => void } | null>(null)
+ const vimStatusBarRef = useRef<HTMLDivElement | null>(null)
+
+ const [realKey] = useState(() => codeEditorIndex++)
+ const builtCodeEditorLogic = codeEditorLogic({
+ key: queryKey ?? `new/${realKey}`,
+ query: value ?? '',
+ metadataQuery: metadataQuery,
+ metadataQueryOffset: metadataQueryOffset,
+ language: editorProps.language ?? 'text',
+ globals,
+ sourceQuery,
+ monaco: monaco,
+ editor: editor,
+ onError,
+ onMetadata,
+ onMetadataLoading,
+ onFixWithAI,
+ metadataFilters: sourceQuery?.kind === NodeKind.HogQLQuery ? sourceQuery.filters : undefined,
+ })
+ useMountedLogic(builtCodeEditorLogic)
+
+ const { vimCommandHistory } = useValues(builtCodeEditorLogic)
+ const { appendVimCommand } = useActions(builtCodeEditorLogic)
+
+ const { isVisible } = usePageVisibility()
+
+ const monacoRoot = sharedMonacoOverflowRoot()
+
+ // Using useRef, not useState, as we don't want to reload the component when this changes.
+ const monacoDisposables = useRef([] as IDisposable[])
+ const mutationObserver = useRef<MutationObserver | null>(null)
+ // Track every model this editor instance has been attached to, so we can
+ // dispose them on unmount. Monaco models live in a global registry until
+ // explicitly disposed; without this, models accumulate forever and retain
+ // their attached `codeEditorLogic` BuiltLogic via `(model as any).codeEditorLogic`.
+ const editorModelsRef = useRef<Set<editor.ITextModel>>(new Set())
+ // Live ref to the Monaco API so the cleanup closure (captured by
+ // `useOnMountEffect`'s [] dep) can read the *current* value rather than
+ // the `null` it had at mount time. Without this the `stillInUse` guard
+ // in `disposeTrackedModels` is dead code: `monaco.editor.getEditors()`
+ // returns `[]` and every tracked model is unconditionally disposed.
+ const monacoApiRef = useRef<Monaco | null>(null)
+
+ const disposeMonacoDisposables = (): void => {
+ monacoDisposables.current.forEach((d) => d?.dispose())
+ monacoDisposables.current = []
+ }
+
+ const disconnectMutationObserver = (): void => {
+ mutationObserver.current?.disconnect()
+ mutationObserver.current = null
+ }
+
+ const disposeTrackedModels = (): void => {
+ const models = editorModelsRef.current
+ if (models.size === 0) {
+ return
+ }
+ const monacoApi = monacoApiRef.current
+ // Skip a model if any OTHER live editor still holds it as its current
+ // model — disposing would break that editor, and nulling its
+ // `codeEditorLogic` would silently break HogQL autocomplete /
+ // metadata providers in the surviving editor.
+ const otherEditors = (monacoApi?.editor.getEditors?.() ?? []).filter(
+ (e: importedEditor.ICodeEditor) => e !== editorRef.current
+ )
+ for (const model of models) {
+ if (model.isDisposed()) {
+ continue
+ }
+ const stillInUse = otherEditors.some((e: importedEditor.ICodeEditor) => e.getModel() === model)
+ if (stillInUse) {
+ continue
+ }
+ // Null the back-reference only on models we're actually about to
+ // dispose. Doing it for shared models would break consumers
+ // (e.g. hogQLAutocompleteProvider, hogQLMetadataProvider) that
+ // read `model.codeEditorLogic` to look up logic state.
+ clearLogicReference(model)
+ try {
+ model.dispose()
+ } catch {
+ // already disposed or in invalid state
+ }
+ }
+ models.clear()
+ }
+
+ const trackEditorModels = (editorInstance: importedEditor.IStandaloneCodeEditor, monacoApi: Monaco): void => {
+ monacoApiRef.current = monacoApi
+ const initial = editorInstance.getModel()
+ if (initial) {
+ editorModelsRef.current.add(initial)
+ }
+ const disposable = editorInstance.onDidChangeModel((e) => {
+ if (!e.newModelUrl) {
+ return
+ }
+ const next = monacoApi.editor.getModel(e.newModelUrl)
+ if (next) {
+ editorModelsRef.current.add(next)
+ }
+ })
+ monacoDisposables.current.push(disposable)
+ }
+
+ const disposeEditor = (): void => {
+ try {
+ if (diffEditorRef.current) {
+ diffEditorRef.current.dispose()
+ } else {
+ editorRef.current?.dispose()
+ }
+ } catch {
+ // already disposed
+ }
+ editorRef.current = null
+ diffEditorRef.current = null
+ }
+
+ useOnMountEffect(() => {
+ return () => {
+ disposeMonacoDisposables()
+ disconnectMutationObserver()
+
+ // Dispose the editor BEFORE @monaco-editor/react's own cleanup
+ // runs: Monaco's services (HoverService, ContextView,
+ // DomListener) keep refs to the editor's container DOM that
+ // survive the library's dispose. Disposing while we still hold
+ // a strong reference lets Monaco tear down its services in an
+ // order that releases those DOM refs.
+ disposeEditor()
+
+ // Now that our editor is disposed, dispose every model this
+ // editor used and was uniquely owned by us. `disposeTrackedModels`
+ // also nulls the `codeEditorLogic` back-reference on each
+ // disposed model. Models still held by another live editor are
+ // skipped on both counts, so HogQL autocomplete/metadata
+ // providers (which read `model.codeEditorLogic`) keep working
+ // for the surviving editor.
+ disposeTrackedModels()
+
+ setMonacoAndEditor(null)
+
+ // Do NOT remove monacoRoot — it's a shared singleton that
+ // Monaco's global services have permanent DomListeners on.
+ }
+ })
+
+ useEffect(() => {
+ if (!monaco) {
+ return
+ }
+ monacoModule.typescript.typescriptDefaults.setCompilerOptions({
+ jsx: editorProps?.path?.endsWith('.tsx')
+ ? monacoModule.typescript.JsxEmit.React
+ : monacoModule.typescript.JsxEmit.Preserve,
+ esModuleInterop: true,
+ })
+ }, [monaco, editorProps.path])
+
+ useEffect(() => {
+ if (!monaco) {
+ return
+ }
+ monacoModule.json.jsonDefaults.setDiagnosticsOptions({
+ validate: true,
+ schemas: schema
+ ? [
+ {
+ uri: 'http://internal/node-schema.json',
+ fileMatch: ['*'],
+ schema: schema,
+ },
+ ]
+ : [],
+ })
+ }, [monaco, schema])
+
+ useEffect(() => {
+ if (!editor) {
+ return
+ }
+
+ let cancelled = false
+
+ if (enableVimMode && vimStatusBarRef.current) {
+ const statusBar = vimStatusBarRef.current
+ void import('lib/monaco/vimMode').then(({ setupVimMode }) => {
+ if (cancelled) {
+ return
+ }
+ vimModeRef.current = setupVimMode(editor, statusBar, {
+ initialHistory: vimCommandHistory,
+ onCommandExecuted: appendVimCommand,
+ })
+ })
+ } else if (vimModeRef.current) {
+ vimModeRef.current.dispose()
+ vimModeRef.current = null
+ }
+
+ return () => {
+ cancelled = true
+ if (vimModeRef.current) {
+ vimModeRef.current.dispose()
+ vimModeRef.current = null
+ }
+ }
+ }, [editor, enableVimMode, vimCommandHistory, appendVimCommand])
+
+ const editorOptions: editor.IStandaloneEditorConstructionOptions = {
+ minimap: {
+ enabled: false,
+ },
+ scrollBeyondLastLine: false,
+ automaticLayout: true,
+ fixedOverflowWidgets: true,
+ glyphMargin: false,
+ folding: true,
+ wordWrap: 'off',
+ lineNumbers: 'on',
+ tabFocusMode: false,
+ overviewRulerBorder: true,
+ hideCursorInOverviewRuler: false,
+ overviewRulerLanes: 3,
+ overflowWidgetsDomNode: monacoRoot,
+ ...options,
+ padding: { bottom: enableVimMode ? 28 : 8, top: 8 },
+ scrollbar: {
+ vertical: scrollbarRendering,
+ horizontal: scrollbarRendering,
+ alwaysConsumeMouseWheel: false,
+ ...options?.scrollbar,
+ },
+ }
+
+ const editorOnMount = (editor: importedEditor.IStandaloneCodeEditor, monaco: Monaco): void => {
+ editorRef.current = editor
+ trackEditorModels(editor, monaco)
+ setMonacoAndEditor([monaco, editor])
+ initEditor(monaco, editor, editorProps, options ?? {}, builtCodeEditorLogic)
+
+ // Override Monaco's suggestion widget styling to prevent truncation
+ const styleId = 'monaco-suggestion-widget-fix'
+ const overrideSuggestionWidgetStyling = (): void => {
+ // Only add style tag if it doesn't already exist
+ if (!document.getElementById(styleId)) {
+ const style = document.createElement('style')
+ style.id = styleId
+ style.textContent = `
+ .monaco-editor .suggest-widget .monaco-list .monaco-list-row.string-label>.contents>.main>.left>.monaco-icon-label {
+ flex-shrink: 0;
+ }
+ `
+ document.head.appendChild(style)
+ }
+ }
+
+ // Apply styling immediately
+ overrideSuggestionWidgetStyling()
+
+ // Monitor for suggestion widget creation and apply styling
+ const observer = new MutationObserver(() => {
+ const suggestWidget = document.querySelector('.monaco-editor .suggest-widget')
+ if (suggestWidget) {
+ overrideSuggestionWidgetStyling()
+ }
+ })
+
+ mutationObserver.current = observer
+ observer.observe(document.body, { childList: true, subtree: true })
+
+ // Clean up observers
+ monacoDisposables.current.push({
+ dispose: () => observer.disconnect(),
+ })
+
+ monacoDisposables.current.push(
+ monaco.editor.registerCommand('posthog.hogql.fixWithAI', (_: unknown, prompt: unknown) => {
+ if (typeof prompt === 'string' && prompt.length > 0) {
+ onFixWithAI?.(prompt)
+ }
+ })
+ )
+
+ if (onPressCmdEnter) {
+ monacoDisposables.current.push(
+ editor.addAction({
+ id: 'saveAndRunPostHog',
+ label: 'Save and run query',
+ keybindings: [monaco.KeyMod.CtrlCmd | monaco.KeyCode.Enter],
+ run: () => {
+ const selection = editor.getSelection()
+ const model = editor.getModel()
+ if (selection && model) {
+ const highlightedText = model.getValueInRange(selection)
+ onPressCmdEnter(highlightedText, 'selection')
+ return
+ }
+
+ onPressCmdEnter(editor.getValue(), 'full')
+ },
+ })
+ )
+ }
+ if (onPressCmdShiftEnter) {
+ monacoDisposables.current.push(
+ editor.addAction({
+ id: 'runSubqueryPostHog',
+ label: 'Run subquery at cursor',
+ keybindings: [monaco.KeyMod.CtrlCmd | monaco.KeyMod.Shift | monaco.KeyCode.Enter],
+ run: () => {
+ onPressCmdShiftEnter()
+ },
+ })
+ )
+ }
+ if (autoFocus) {
+ editor.focus()
+ const model = editor.getModel()
+ if (model) {
+ editor.setPosition({
+ column: model.getLineContent(model.getLineCount()).length + 1,
+ lineNumber: model.getLineCount(),
+ })
+ }
+ }
+
+ onMount?.(editor, monaco)
+ }
+
+ useEffect(() => {
+ if (!mutationObserver.current) {
+ return
+ }
+
+ if (isVisible) {
+ mutationObserver.current.observe(document.body, { childList: true, subtree: true })
+ } else {
+ mutationObserver.current.disconnect()
+ }
+ }, [isVisible])
+
+ if (originalValue) {
+ // If originalValue is provided, we render a diff editor instead
+ const diffEditorOnMount = (diff: importedEditor.IStandaloneDiffEditor, monaco: Monaco): void => {
+ const modifiedEditor = diff.getModifiedEditor()
+ editorRef.current = modifiedEditor
+ diffEditorRef.current = diff
+ trackEditorModels(modifiedEditor, monaco)
+ const original = diff.getOriginalEditor().getModel()
+ if (original) {
+ editorModelsRef.current.add(original)
+ }
+ setMonacoAndEditor([monaco, modifiedEditor])
+
+ if (editorProps.onChange) {
+ const disposable = modifiedEditor.onDidChangeModelContent((event: editor.IModelContentChangedEvent) => {
+ editorProps.onChange?.(modifiedEditor.getValue(), event)
+ })
+ monacoDisposables.current.push(disposable)
+ }
+ onMount?.(modifiedEditor, monaco)
+ }
+
+ return (
+ <div className="CodeEditor relative h-full w-full" data-editor-ready={monacoAndEditor ? 'true' : 'false'}>
+ <MonacoDiffEditor
+ key={queryKey}
+ loading={<Spinner />}
+ theme={isDarkModeOn ? 'vs-dark' : 'vs-light'}
+ original={originalValue}
+ modified={value}
+ options={{
+ ...editorOptions,
+ renderSideBySide: false,
+ acceptSuggestionOnEnter: 'on',
+ renderGutterMenu: false,
+ }}
+ onMount={diffEditorOnMount}
+ {...editorProps}
+ />
+ </div>
+ )
+ }
+
+ return (
+ <div className="CodeEditor relative h-full w-full" data-editor-ready={monacoAndEditor ? 'true' : 'false'}>
+ <MonacoEditor // eslint-disable-line react/forbid-elements
+ key={queryKey}
+ theme={isDarkModeOn ? 'vs-dark' : 'vs-light'}
+ loading={<Spinner />}
+ value={value}
+ options={editorOptions}
+ onMount={editorOnMount}
+ {...editorProps}
+ />
+ {enableVimMode && (
+ <div
+ ref={vimStatusBarRef}
+ className="CodeEditor__vim-status-bar absolute bottom-0 left-0 right-0 font-mono text-xs px-2 py-0.5 bg-bg-light border-t z-10"
+ />
+ )}
+ </div>
+ )
+}
diff --git a/frontend/src/lib/monaco/modelLogicReference.ts b/frontend/src/lib/monaco/modelLogicReference.ts
new file mode 100644
index 000000000000..6042f6be3393
--- /dev/null
+++ b/frontend/src/lib/monaco/modelLogicReference.ts
@@ -0,0 +1,12 @@
+import type { BuiltLogic } from 'kea'
+import type { editor } from 'monaco-editor'
+
+import type { codeEditorLogicType } from 'lib/monaco/codeEditorLogicType'
+
+export function initModel(model: editor.ITextModel, builtCodeEditorLogic: BuiltLogic<codeEditorLogicType>): void {
+ ;(model as any).codeEditorLogic = builtCodeEditorLogic
+}
+
+export function clearLogicReference(model: editor.ITextModel): void {
+ ;(model as any).codeEditorLogic = undefined
+}
diff --git a/frontend/src/scenes/data-warehouse/settings/dataWarehouseSettingsSceneLogic.ts b/frontend/src/scenes/data-warehouse/settings/dataWarehouseSettingsSceneLogic.ts
index 034f8824e368..5d78f2f6c369 100644
--- a/frontend/src/scenes/data-warehouse/settings/dataWarehouseSettingsSceneLogic.ts
+++ b/frontend/src/scenes/data-warehouse/settings/dataWarehouseSettingsSceneLogic.ts
@@ -1,7 +1,7 @@
-import { Monaco } from '@monaco-editor/react'
+import type { Monaco } from '@monaco-editor/react'
import { actions, afterMount, connect, kea, listeners, path, props, reducers, selectors } from 'kea'
import { router, urlToAction } from 'kea-router'
-import { editor } from 'monaco-editor'
+import type { editor } from 'monaco-editor'
import posthog from 'posthog-js'
import { lemonToast } from '@posthog/lemon-ui'
diff --git a/frontend/src/scenes/hog-functions/configuration/hogFunctionTestLogic.tsx b/frontend/src/scenes/hog-functions/configuration/hogFunctionTestLogic.tsx
index fcd0e8330d4d..72c3001a98e1 100644
--- a/frontend/src/scenes/hog-functions/configuration/hogFunctionTestLogic.tsx
+++ b/frontend/src/scenes/hog-functions/configuration/hogFunctionTestLogic.tsx
@@ -1,6 +1,6 @@
import { actions, afterMount, connect, kea, key, listeners, path, props, reducers, selectors } from 'kea'
import { forms } from 'kea-forms'
-import { editor } from 'monaco-editor'
+import type { editor } from 'monaco-editor'
import { lemonToast } from '@posthog/lemon-ui'
diff --git a/frontend/src/scenes/hog-functions/misc/Diff.tsx b/frontend/src/scenes/hog-functions/misc/Diff.tsx
new file mode 100644
index 000000000000..bd3a2fde9a81
--- /dev/null
+++ b/frontend/src/scenes/hog-functions/misc/Diff.tsx
@@ -0,0 +1,41 @@
+import { DiffEditor } from '@monaco-editor/react'
+
+import 'lib/monaco/monacoEnvironment'
+import { initHogLanguage } from 'lib/monaco/languages/hog'
+
+export interface DiffProps {
+ before: string
+ after: string
+ language?: string
+}
+
+export function Diff({ before, after, language }: DiffProps): JSX.Element {
+ return (
+ <DiffEditor
+ height="300px"
+ original={before}
+ modified={after}
+ language={language ?? 'json'}
+ onMount={(_, monaco) => {
+ if (language === 'hog') {
+ initHogLanguage(monaco)
+ }
+ }}
+ options={{
+ lineNumbers: 'off',
+ minimap: { enabled: false },
+ folding: false,
+ wordWrap: 'on',
+ renderLineHighlight: 'none',
+ scrollbar: { vertical: 'auto', horizontal: 'hidden' },
+ overviewRulerBorder: false,
+ hideCursorInOverviewRuler: true,
+ overviewRulerLanes: 0,
+ tabFocusMode: true,
+ enableSplitViewResizing: false,
+ renderSideBySide: false,
+ readOnly: true,
+ }}
+ />
+ )
+}
diff --git a/frontend/src/scenes/hog-functions/misc/activityDescriptions.tsx b/frontend/src/scenes/hog-functions/misc/activityDescriptions.tsx
index b8968cebc942..83246345c6db 100644
--- a/frontend/src/scenes/hog-functions/misc/activityDescriptions.tsx
+++ b/frontend/src/scenes/hog-functions/misc/activityDescriptions.tsx
@@ -1,6 +1,5 @@
-import { DiffEditor } from '@monaco-editor/react'
+import { Suspense, lazy } from 'react'
-import 'lib/monaco/monacoEnvironment'
import {
ActivityLogItem,
HumanizedChange,
@@ -9,53 +8,34 @@ import {
} from 'lib/components/ActivityLog/humanizeActivity'
import { LemonDropdown } from 'lib/lemon-ui/LemonDropdown'
import { Link } from 'lib/lemon-ui/Link'
-import { initHogLanguage } from 'lib/monaco/languages/hog'
+import { Spinner } from 'lib/lemon-ui/Spinner'
import { isObject } from 'lib/utils'
import { urls } from 'scenes/urls'
import { HogFunctionTypeType } from '~/types'
import { humanizeHogFunctionType } from '../hog-function-utils'
+import type { DiffProps } from './Diff'
const nameOrLinkToHogFunction = (id?: string | null, name?: string | null): string | JSX.Element => {
const displayName = name?.trim() ? name : 'Untitled hog function'
return id ? <Link to={urls.hogFunction(id)}>{displayName}</Link> : displayName
}
-export interface DiffProps {
- before: string
- after: string
- language?: string
-}
+const LazyDiff = lazy(() => import('./Diff').then((m) => ({ default: m.Diff })))
-export function Diff({ before, after, language }: DiffProps): JSX.Element {
+/** Lazy so the activity describer registry (imported app-wide) doesn't pull monaco into its chunk. */
+export function Diff(props: DiffProps): JSX.Element {
return (
- <DiffEditor
- height="300px"
- original={before}
- modified={after}
- language={language ?? 'json'}
- onMount={(_, monaco) => {
- if (language === 'hog') {
- initHogLanguage(monaco)
- }
- }}
- options={{
- lineNumbers: 'off',
- minimap: { enabled: false },
- folding: false,
- wordWrap: 'on',
- renderLineHighlight: 'none',
- scrollbar: { vertical: 'auto', horizontal: 'hidden' },
- overviewRulerBorder: false,
- hideCursorInOverviewRuler: true,
- overviewRulerLanes: 0,
- tabFocusMode: true,
- enableSplitViewResizing: false,
- renderSideBySide: false,
- readOnly: true,
- }}
- />
+ <Suspense
+ fallback={
+ <div className="min-h-[300px]">
+ <Spinner />
+ </div>
+ }
+ >
+ <LazyDiff {...props} />
+ </Suspense>
)
}
diff --git a/playwright/e2e/sql-editor-direct-postgres.spec.ts b/playwright/e2e/sql-editor-direct-postgres.spec.ts
index e9ce2aa5a11f..b02ddd128023 100644
--- a/playwright/e2e/sql-editor-direct-postgres.spec.ts
+++ b/playwright/e2e/sql-editor-direct-postgres.spec.ts
@@ -133,6 +133,12 @@ test.describe('SQL Editor direct Postgres queries', () => {
await page.getByTestId('sql-editor-settings-toggle').click()
await page.getByTestId('sql-editor-send-raw-query-toggle').click()
+ // CodeEditor lazy-loads monaco, so the container renders before the editor
+ // mounts — clicking too early focuses nothing and the keystrokes are lost.
+ await page
+ .locator('[data-attr=hogql-query-editor] [data-editor-ready="true"]')
+ .first()
+ .waitFor({ state: 'visible' })
await page.locator('[data-attr=hogql-query-editor]').click()
await page
.locator('[data-attr=hogql-query-editor]')
diff --git a/playwright/page-models/insights/sqlInsight.ts b/playwright/page-models/insights/sqlInsight.ts
index 938188786c05..cac14fc5bfbf 100644
--- a/playwright/page-models/insights/sqlInsight.ts
+++ b/playwright/page-models/insights/sqlInsight.ts
@@ -16,6 +16,9 @@ export class SqlInsight {
async writeQuery(query: string): Promise<void> {
const editorArea = this.page.getByTestId('hogql-query-editor')
await editorArea.waitFor({ state: 'visible' })
+ // CodeEditor lazy-loads monaco, so the container renders before the editor
+ // mounts — clicking too early focuses nothing and insertText is lost.
+ await editorArea.locator('[data-editor-ready="true"]').first().waitFor({ state: 'visible' })
await editorArea.click()
await this.page.keyboard.press('ControlOrMeta+a')
// Use insertText() to avoid Monaco autocomplete intercepting keystrokes