Skip to content

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

handle new document to new document nav (#62504)

Nick Fitzpatrickf1a484e51488
diff --git a/e2e/support/helpers/e2e-document-helpers.ts b/e2e/support/helpers/e2e-document-helpers.ts
index b96fd1901f9..be06fcbf4f0 100644
--- a/e2e/support/helpers/e2e-document-helpers.ts
+++ b/e2e/support/helpers/e2e-document-helpers.ts
@@ -1,5 +1,8 @@
export const documentContent = () => cy.findByTestId("document-content");
+export const documentSaveButton = () =>
+ cy.findByRole("button", { name: "Save" });
+
export const documentFormattingMenu = () =>
cy.findByTestId("document-formatting-menu");
diff --git a/e2e/support/helpers/e2e-ui-elements-helpers.js b/e2e/support/helpers/e2e-ui-elements-helpers.js
index f90647971f4..d65bcd3be48 100644
--- a/e2e/support/helpers/e2e-ui-elements-helpers.js
+++ b/e2e/support/helpers/e2e-ui-elements-helpers.js
@@ -626,3 +626,7 @@ export function mapPinIcon() {
export function waitForLoaderToBeRemoved() {
cy.findByTestId("loading-indicator").should("not.exist");
}
+
+export function leaveConfirmationModal() {
+ return cy.findByTestId("leave-confirmation");
+}
diff --git a/e2e/test/scenarios/documents/documents.cy.spec.ts b/e2e/test/scenarios/documents/documents.cy.spec.ts
index fbd9c6c448b..42c8376ccd8 100644
--- a/e2e/test/scenarios/documents/documents.cy.spec.ts
+++ b/e2e/test/scenarios/documents/documents.cy.spec.ts
@@ -87,10 +87,36 @@ describe("documents", () => {
H.openNavigationSidebar();
- H.navigationSidebar().findByText("Trash").click();
+ // Force the click since this is hidden behind a toast notification
+ H.navigationSidebar().findByText("Trash").click({ force: true });
H.getUnpinnedSection().findByText("Test Document").should("exist");
});
+ it("should handle navigating from /new to /new gracefully", () => {
+ cy.visit("/");
+ H.newButton("Document").click();
+ cy.title().should("eq", "New document · Metabase");
+ H.documentContent().click();
+
+ H.documentSaveButton().should("not.exist");
+
+ H.addToDocument("This is some content");
+
+ H.documentSaveButton().should("exist");
+
+ H.newButton("Document").click();
+ H.leaveConfirmationModal().findByRole("button", { name: "Cancel" }).click();
+
+ H.documentContent().should("have.text", "This is some content");
+
+ H.newButton("Document").click();
+ H.leaveConfirmationModal()
+ .findByRole("button", { name: "Discard changes" })
+ .click();
+ H.documentContent().should("have.text", "");
+ H.documentSaveButton().should("not.exist");
+ });
+
describe("document editing", () => {
describe("Document with content", () => {
beforeEach(() => {
diff --git a/enterprise/frontend/src/metabase-enterprise/documents/components/DocumentPage.tsx b/enterprise/frontend/src/metabase-enterprise/documents/components/DocumentPage.tsx
index 57531fc0a1e..3be372502e7 100644
--- a/enterprise/frontend/src/metabase-enterprise/documents/components/DocumentPage.tsx
+++ b/enterprise/frontend/src/metabase-enterprise/documents/components/DocumentPage.tsx
@@ -1,6 +1,8 @@
+import { useForceUpdate } from "@mantine/hooks";
import type { JSONContent, Editor as TiptapEditor } from "@tiptap/core";
import type { Node as ProseMirrorNode } from "@tiptap/pm/model";
import dayjs from "dayjs";
+import type { Location } from "history";
import { useCallback, useEffect, useState } from "react";
import type { Route } from "react-router";
import { push, replace } from "react-router-redux";
@@ -16,7 +18,10 @@ import {
useListBookmarksQuery,
} from "metabase/api";
import { canonicalCollectionId } from "metabase/collections/utils";
-import { LeaveRouteConfirmModal } from "metabase/common/components/LeaveConfirmModal";
+import {
+ LeaveConfirmModal,
+ LeaveRouteConfirmModal,
+} from "metabase/common/components/LeaveConfirmModal";
import { CollectionPickerModal } from "metabase/common/components/Pickers/CollectionPicker";
import { useToast } from "metabase/common/hooks";
import { useCallbackEffect } from "metabase/common/hooks/use-callback-effect";
@@ -59,11 +64,14 @@ import { EmbedQuestionSettingsSidebar } from "./EmbedQuestionSettingsSidebar";
export const DocumentPage = ({
params: { entityId },
route,
+ location,
}: {
params: { entityId?: string };
- location?: { query?: { version?: string } };
+ location: Location;
route: Route;
}) => {
+ const previousLocationKey = usePrevious(location.key);
+ const forceUpdate = useForceUpdate();
const dispatch = useDispatch();
const selectedQuestionId = useSelector(getSelectedQuestionId);
const selectedEmbedIndex = useSelector(getSelectedEmbedIndex);
@@ -155,6 +163,16 @@ export const DocumentPage = ({
dispatch,
]);
+ // Reset state when we navigate back to /new
+ const resetDocument = useCallback(() => {
+ setDocumentTitle("");
+ setDocumentContent(null);
+ setHasUnsavedEditorChanges(false);
+ editorInstance?.commands.clearContent();
+ editorInstance?.commands.focus();
+ dispatch(resetDocuments());
+ }, [dispatch, editorInstance, setDocumentContent, setDocumentTitle]);
+
// Reset dirty state when document content loads from API
useEffect(() => {
if (documentContent && !isNewDocument) {
@@ -203,7 +221,8 @@ export const DocumentPage = ({
(content: JSONContent) => {
// For new documents, any content means changes
if (isNewDocument) {
- setHasUnsavedEditorChanges(!editorInstance?.isEmpty);
+ // when navigating to `/new`, handleChange is fired but the editor instance hasn't been set yet
+ setHasUnsavedEditorChanges(!!editorInstance && !editorInstance.isEmpty);
return;
}
@@ -432,10 +451,21 @@ export const DocumentPage = ({
<LeaveRouteConfirmModal
// `key` remounts this modal when navigating between different documents or to a new document.
// The `route` doesn't change in that scenario which prevents the modal from closing when you confirm you want to discard your changes.
- key={documentId}
+ key={location.key}
isEnabled={hasUnsavedChanges() && !isNavigationScheduled}
route={route}
/>
+
+ <LeaveConfirmModal
+ // only applies when going from /new -> /new
+ opened={
+ hasUnsavedChanges() &&
+ isNewDocument &&
+ location.key !== previousLocationKey
+ }
+ onConfirm={resetDocument}
+ onClose={() => forceUpdate()}
+ />
</Box>
</Box>
);
diff --git a/frontend/src/metabase/common/components/LeaveConfirmModal/LeaveConfirmModal.tsx b/frontend/src/metabase/common/components/LeaveConfirmModal/LeaveConfirmModal.tsx
index 4a4476b7429..d91405ddecd 100644
--- a/frontend/src/metabase/common/components/LeaveConfirmModal/LeaveConfirmModal.tsx
+++ b/frontend/src/metabase/common/components/LeaveConfirmModal/LeaveConfirmModal.tsx
@@ -5,7 +5,6 @@ import { ConfirmModal } from "metabase/common/components/ConfirmModal/ConfirmMod
interface Props {
onConfirm?: () => void;
- onCancel?: () => void;
onClose?: () => void;
opened: boolean | undefined;
}