The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
Make SQL editor typing responsive again (DEV-3545) (#82724)
Romeo Van Snick91f5798258f9
diff --git a/frontend/src/metabase/querying/components/CodeMirrorEditor/extensions.tsx b/frontend/src/metabase/querying/components/CodeMirrorEditor/extensions.tsxindex afc06dfadd0..16f7b527e18 100644--- a/frontend/src/metabase/querying/components/CodeMirrorEditor/extensions.tsx+++ b/frontend/src/metabase/querying/components/CodeMirrorEditor/extensions.tsx@@ -36,14 +36,10 @@ export function useExtensions({diff,onRunQuery,}: Options): Extension[] {- const { databaseId, engine, referencedCardIds } = useMemo(- () => ({- databaseId: Lib.databaseID(query),- engine: Lib.engine(query),- referencedCardIds: getReferencedCardIds(query),- }),- [query],- );+ const databaseId = Lib.databaseID(query);+ const engine = Lib.engine(query);+ const referencedCardIds = getReferencedCardIds(query);+ const originalQueryText = diff ? Lib.rawNativeQuery(query) : null;const schemaCompletion = useSchemaCompletion({ databaseId });const snippetCompletion = useSnippetCompletion();@@ -84,9 +80,9 @@ export function useExtensions({},]),),- diff+ originalQueryText != null? unifiedMergeView({- original: Lib.rawNativeQuery(query),+ original: originalQueryText,mergeControls: false,}): null,@@ -94,8 +90,7 @@ export function useExtensions({.flat().filter(isNotNull);}, [- query,- diff,+ originalQueryText,engine,schemaCompletion,snippetCompletion,diff --git a/frontend/src/metabase/querying/components/NativeQueryEditor/NativeQueryEditor.typing.unit.spec.tsx b/frontend/src/metabase/querying/components/NativeQueryEditor/NativeQueryEditor.typing.unit.spec.tsxnew file mode 100644index 00000000000..6306649d8a7--- /dev/null+++ b/frontend/src/metabase/querying/components/NativeQueryEditor/NativeQueryEditor.typing.unit.spec.tsx@@ -0,0 +1,228 @@+import userEvent from "@testing-library/user-event";+import { useCallback, useState } from "react";++import { createMockMetadata } from "__support__/metadata";+import {+ setupCollectionsEndpoints,+ setupNativeQuerySnippetEndpoints,+} from "__support__/server-mocks";+import {+ fireEvent,+ renderWithProviders,+ screen,+ waitFor,+} from "__support__/ui";+import Question from "metabase-lib/v1/Question";+import type Metadata from "metabase-lib/v1/metadata/Metadata";+import type NativeQuery from "metabase-lib/v1/queries/NativeQuery";+import {+ createMockCard,+ createMockNativeDatasetQuery,+ createMockNativeQuery,+} from "metabase-types/api/mocks";+import {+ SAMPLE_DB_ID,+ createSampleDatabase,+} from "metabase-types/api/mocks/presets";++import { NativeQueryEditor } from "./NativeQueryEditor";++/**+ * `@uiw/react-codemirror` reconfigures the whole editor whenever one of these+ * props changes identity, which rebuilds the language parser and every view+ * plugin, and re-decorates the whole document. Doing that once per keystroke is+ * what made typing slow with large queries (DEV-3545).+ */+const RECONFIGURING_PROPS = [+ "extensions",+ "onChange",+ "onFormat",+ "onUpdate",+] as const;++type ReconfiguringProp = (typeof RECONFIGURING_PROPS)[number];++const receivedProps: Record<ReconfiguringProp, unknown>[] = [];++jest.mock("metabase/common/components/CodeMirror", () => {+ const { forwardRef } = jest.requireActual("react");++ return {+ __esModule: true,+ ...jest.requireActual("metabase/common/components/CodeMirror"),+ // Unjustified type casts. The mock stands in for a component with a large+ // prop surface that this spec does not need to model.+ CodeMirror: forwardRef(function MockCodeMirror(props: any, _ref: unknown) {+ receivedProps.push({+ extensions: props.extensions,+ onChange: props.onChange,+ onFormat: props.onFormat,+ onUpdate: props.onUpdate,+ });++ return (+ <textarea+ data-testid="mock-code-mirror"+ value={props.value}+ onChange={(event) => props.onChange?.(event.target.value)}+ />+ );+ }),+ };+});++const QUERY_TEXT = "SELECT 1";++const setDatasetQuerySpy = jest.fn();+const runQuerySpy = jest.fn();++function TestEditor({+ metadata,+ withRunButton = false,+}: {+ metadata: Metadata;+ withRunButton?: boolean;+}) {+ const [card, setCard] = useState(() =>+ createMockCard({+ id: 1,+ name: "Native",+ dataset_query: createMockNativeDatasetQuery({+ database: SAMPLE_DB_ID,+ native: createMockNativeQuery({ query: QUERY_TEXT }),+ }),+ }),+ );++ const question = new Question(card, metadata);+ // Unjustified type cast. The question is built from a native dataset query.+ const query = question.legacyNativeQuery() as NativeQuery;++ // The query builder passes a dispatch-bound action, whose identity is stable+ const setDatasetQuery = useCallback((updatedQuery: NativeQuery) => {+ setDatasetQuerySpy(updatedQuery.queryText());+ setCard((previousCard) => ({+ ...previousCard,+ dataset_query: updatedQuery.datasetQuery(),+ }));+ }, []);++ return (+ <NativeQueryEditor+ question={question}+ query={query}+ isNativeEditorOpen+ setDatasetQuery={setDatasetQuery}+ isRunnable+ runQuery={runQuerySpy}+ cancelQuery={jest.fn()}+ >+ {withRunButton && <NativeQueryEditor.RunButton />}+ </NativeQueryEditor>+ );+}++async function setup({ withRunButton = false } = {}) {+ setupNativeQuerySnippetEndpoints();+ setupCollectionsEndpoints({ collections: [] });++ const metadata = createMockMetadata({+ databases: [createSampleDatabase()],+ });++ const { unmount } = renderWithProviders(+ <TestEditor metadata={metadata} withRunButton={withRunButton} />,+ );++ const editor = await screen.findByTestId("mock-code-mirror");++ return { editor, unmount };+}++async function typeThreeCharacters() {+ const { editor } = await setup();++ receivedProps.length = 0;+ await userEvent.type(editor, "234");++ return receivedProps;+}++describe("NativeQueryEditor typing", () => {+ beforeEach(() => {+ receivedProps.length = 0;+ setDatasetQuerySpy.mockClear();+ runQuerySpy.mockClear();+ });++ it.each(RECONFIGURING_PROPS)(+ "should keep the %s prop stable while typing",+ async (prop) => {+ const renders = await typeThreeCharacters();++ expect(renders.length).toBeGreaterThan(1);+ expect(new Set(renders.map((render) => render[prop])).size).toBe(1);+ },+ );+});++describe("NativeQueryEditor deferred edits", () => {+ beforeEach(() => {+ receivedProps.length = 0;+ setDatasetQuerySpy.mockClear();+ runQuerySpy.mockClear();+ });++ it("should not put the edit in the store during the keystroke", async () => {+ const { editor } = await setup();++ fireEvent.change(editor, { target: { value: "SELECT 2" } });++ expect(setDatasetQuerySpy).not.toHaveBeenCalled();+ });++ it("should put the edit in the store once the keystroke is over", async () => {+ const { editor } = await setup();++ fireEvent.change(editor, { target: { value: "SELECT 2" } });++ await waitFor(() => {+ expect(setDatasetQuerySpy).toHaveBeenCalledWith("SELECT 2");+ });+ });++ it("should only apply the last of several edits made in one task", async () => {+ const { editor } = await setup();++ fireEvent.change(editor, { target: { value: "SELECT 2" } });+ fireEvent.change(editor, { target: { value: "SELECT 23" } });+ fireEvent.change(editor, { target: { value: "SELECT 234" } });++ await waitFor(() => {+ expect(setDatasetQuerySpy).toHaveBeenCalledWith("SELECT 234");+ });+ expect(setDatasetQuerySpy).toHaveBeenCalledTimes(1);+ });++ it("should not lose a pending edit when the editor unmounts", async () => {+ const { editor, unmount } = await setup();++ fireEvent.change(editor, { target: { value: "SELECT 2" } });+ unmount();++ expect(setDatasetQuerySpy).toHaveBeenCalledWith("SELECT 2");+ });++ it("should apply a pending edit before running the query", async () => {+ const { editor } = await setup({ withRunButton: true });++ fireEvent.change(editor, { target: { value: "SELECT 2" } });+ await userEvent.click(screen.getByTestId("run-button"));++ expect(setDatasetQuerySpy).toHaveBeenCalledWith("SELECT 2");+ expect(runQuerySpy).toHaveBeenCalled();+ expect(setDatasetQuerySpy.mock.invocationCallOrder[0]).toBeLessThan(+ runQuerySpy.mock.invocationCallOrder[0],+ );+ });+});diff --git a/frontend/src/metabase/querying/components/NativeQueryEditor/NativeQueryEditorRoot.tsx b/frontend/src/metabase/querying/components/NativeQueryEditor/NativeQueryEditorRoot.tsxindex 4da136ea191..cc180caa6db 100644--- a/frontend/src/metabase/querying/components/NativeQueryEditor/NativeQueryEditorRoot.tsx+++ b/frontend/src/metabase/querying/components/NativeQueryEditor/NativeQueryEditorRoot.tsx@@ -1,4 +1,4 @@-import { useElementSize } from "@mantine/hooks";+import { useDebouncedCallback, useElementSize } from "@mantine/hooks";import cx from "classnames";import {Children,@@ -11,7 +11,7 @@ import {useRef,useState,} from "react";-import { useMount } from "react-use";+import { useLatest, useMount } from "react-use";import { t } from "ttag";import { useListCollectionsQuery, useListSnippetsQuery } from "metabase/api";@@ -220,16 +220,42 @@ export const NativeQueryEditorRoot = forwardRef<}}, [nativeEditorSelectedText, isSelectedTextPopoverOpen]);- const handleChange = useCallback(+ // The editor reconfigures itself whenever the identity of its callbacks+ // changes, so they must not depend on the query, which changes on every+ // keystroke.+ const queryRef = useLatest(query);+ const questionRef = useLatest(question);++ const applyQueryText = useCallback((queryText: string) => {- if (query.queryText() !== queryText) {- const updatedQuery = query.setQueryText(queryText);- setDatasetQuery(updatedQuery);+ const currentQuery = queryRef.current;+ if (currentQuery.queryText() !== queryText) {+ setDatasetQuery(currentQuery.setQueryText(queryText));}},- [query, setDatasetQuery],+ [queryRef, setDatasetQuery],);+ // Putting the edit in the store rerenders the query builder, which is far+ // more work than the keystroke that caused it. A zero delay runs it in the+ // next task, so the editor paints the character first. That is what keeps+ // typing responsive on large queries (DEV-3545).+ const handleChange = useDebouncedCallback(applyQueryText, {+ delay: 0,+ flushOnUnmount: true,+ });++ // Anything that reads the query from the store needs the pending edit first.+ const handleRunQuery = useCallback(() => {+ handleChange.flush();+ runQuery?.();+ }, [handleChange, runQuery]);++ const handleBlur = useCallback(() => {+ handleChange.flush();+ onBlur?.();+ }, [handleChange, onBlur]);+const handleSnippetUpdate = useCallback((newSnippet: NativeQuerySnippet, oldSnippet: NativeQuerySnippet) => {// get the query instance with the latest Metadata that has the updated snippet@@ -255,7 +281,7 @@ export const NativeQueryEditorRoot = forwardRef<}, [setIsNativeEditorOpen, shouldOpenDataReference, isNativeEditorOpen]);const handleFormatQuery = useCallback(async () => {- const query = question.query();+ const query = questionRef.current.query();const engine = Lib.engine(query);const queryText = Lib.rawNativeQuery(query);const canFormatQuery = engine != null && canFormatForEngine(engine);@@ -270,8 +296,9 @@ export const NativeQueryEditorRoot = forwardRef<const formattedQuery = await formatQuery(queryText, engine);handleChange(formattedQuery);+ handleChange.flush();focusEditor();- }, [question, focusEditor, handleChange]);+ }, [questionRef, focusEditor, handleChange]);const handleResize = useCallback((height: number) => {@@ -306,7 +333,7 @@ export const NativeQueryEditorRoot = forwardRef<isRunnable,isRunning,isResultDirty,- runQuery,+ runQuery: handleRunQuery,cancelQuery,nativeEditorSelectedText,snippets,@@ -364,9 +391,9 @@ export const NativeQueryEditorRoot = forwardRef<hasSqlGenerationAccess={hasSqlGenerationAccess}highlightedLineNumbers={highlightedLineNumbers}extensions={extensions}- onBlur={onBlur}+ onBlur={handleBlur}onChange={handleChange}- onRunQuery={runQuery}+ onRunQuery={handleRunQuery}onSelectionChange={setNativeEditorSelectedRange}onCursorMoveOverCardTag={openDataReferenceAtQuestion}onRightClickSelection={handleRightClickSelection}@@ -396,6 +423,7 @@ export const NativeQueryEditorRoot = forwardRef<proposedQuestion.legacyNativeQuery();if (proposedQuery) {handleChange(proposedQuery.queryText());+ handleChange.flush();onAcceptProposed(proposedQuery.datasetQuery());}}}