Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Fix UXW-2759 - Snippet edits in data studio are not saved (#68565)

Brad Anderson86f3e524ba1f
diff --git a/e2e/test/scenarios/data-studio/snippets.cy.spec.ts b/e2e/test/scenarios/data-studio/snippets.cy.spec.ts
index f9ef50e1a20..55edd6db756 100644
--- a/e2e/test/scenarios/data-studio/snippets.cy.spec.ts
+++ b/e2e/test/scenarios/data-studio/snippets.cy.spec.ts
@@ -121,6 +121,42 @@ describe("scenarios > data studio > snippets", () => {
H.DataStudio.Snippets.editPage().should("be.visible");
});
+
+ it("should preserve unsaved content changes when description or name is edited", () => {
+ cy.log("Navigate to a snippet and edit its content");
+ H.createSnippet({
+ name: "Test snippet",
+ content: "SELECT * FROM orders",
+ });
+ H.DataStudio.Library.visit();
+ H.DataStudio.Library.libraryPage().findByText("Test snippet").click();
+ H.DataStudio.Snippets.editor.type("1");
+
+ cy.log("Edit its name");
+ cy.findByPlaceholderText("Name").type("1").blur();
+ H.undoToast().findByText("Snippet name updated").should("be.visible");
+ H.undoToast().icon("close").click();
+
+ cy.log("Edit its description");
+ H.DataStudio.Snippets.descriptionInput().type("desc").blur();
+ H.undoToast()
+ .findByText("Snippet description updated")
+ .should("be.visible");
+ H.undoToast().icon("close").click();
+
+ cy.log("Verify unsaved changes are preserved");
+ H.DataStudio.Snippets.editor
+ .value()
+ .should("eq", "SELECT * FROM orders1");
+
+ cy.log(
+ "Verify Save button saves the content without reverting the name and description changes",
+ );
+ H.DataStudio.Snippets.saveButton().click();
+ H.undoToast().findByText("Snippet content updated").should("be.visible");
+ cy.findByPlaceholderText("Name").should("have.value", "Test snippet1");
+ H.DataStudio.Snippets.editPage().findByText("desc").should("be.visible");
+ });
});
describe("description", () => {
diff --git a/enterprise/frontend/src/metabase-enterprise/data-studio/snippets/components/SnippetDescriptionSection/SnippetDescriptionSection.tsx b/enterprise/frontend/src/metabase-enterprise/data-studio/snippets/components/SnippetDescriptionSection/SnippetDescriptionSection.tsx
index 617b87fb128..e96baaa5ba7 100644
--- a/enterprise/frontend/src/metabase-enterprise/data-studio/snippets/components/SnippetDescriptionSection/SnippetDescriptionSection.tsx
+++ b/enterprise/frontend/src/metabase-enterprise/data-studio/snippets/components/SnippetDescriptionSection/SnippetDescriptionSection.tsx
@@ -43,6 +43,7 @@ export function SnippetDescriptionSection({
isMarkdown
onChange={handleChange}
isDisabled={isDisabled}
+ isOptional
/>
);
}
diff --git a/enterprise/frontend/src/metabase-enterprise/data-studio/snippets/pages/EditSnippetPage/EditSnippetPage.tsx b/enterprise/frontend/src/metabase-enterprise/data-studio/snippets/pages/EditSnippetPage/EditSnippetPage.tsx
index 812f9349645..ba9a8b3e450 100644
--- a/enterprise/frontend/src/metabase-enterprise/data-studio/snippets/pages/EditSnippetPage/EditSnippetPage.tsx
+++ b/enterprise/frontend/src/metabase-enterprise/data-studio/snippets/pages/EditSnippetPage/EditSnippetPage.tsx
@@ -1,6 +1,7 @@
import { sql } from "@codemirror/lang-sql";
import { useLayoutEffect, useMemo, useState } from "react";
import type { Route } from "react-router";
+import { usePreviousDistinct } from "react-use";
import { t } from "ttag";
import {
@@ -54,11 +55,12 @@ export function EditSnippetPage({ params, route }: EditSnippetPageProps) {
[content, snippet],
);
+ const previousSnippet = usePreviousDistinct(snippet);
useLayoutEffect(() => {
- if (snippet) {
+ if (snippet && previousSnippet?.id !== snippet.id) {
setContent(snippet.content);
}
- }, [snippet]);
+ }, [snippet, previousSnippet]);
const handleSave = async () => {
if (!snippet) {