Skip to content

Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.

fix: navigate to home page when undo page creation (#4995)

Bogdan Chadkina273abadb447
diff --git a/apps/builder/app/shared/awareness.ts b/apps/builder/app/shared/awareness.ts
index ea5fee5cd1..4dd991c45d 100644
--- a/apps/builder/app/shared/awareness.ts
+++ b/apps/builder/app/shared/awareness.ts
@@ -99,7 +99,7 @@ export const getInstancePath = (
instances: Instances,
virtualInstances?: Instances,
temporaryInstances?: Instances
-): InstancePath => {
+): undefined | InstancePath => {
const instancePath: InstancePath = [];
for (let index = 0; index < instanceSelector.length; index += 1) {
const instanceId = instanceSelector[index];
@@ -116,6 +116,11 @@ export const getInstancePath = (
instanceSelector: instanceSelector.slice(index),
});
}
+ // all consuming code expect at least one instance to be selected
+ // though it is possible to get empty array when undo created page
+ if (instancePath.length === 0) {
+ return undefined;
+ }
return instancePath;
};
diff --git a/apps/builder/app/shared/instance-utils.test.tsx b/apps/builder/app/shared/instance-utils.test.tsx
index 8313d804c9..8059bc6a0c 100644
--- a/apps/builder/app/shared/instance-utils.test.tsx
+++ b/apps/builder/app/shared/instance-utils.test.tsx
@@ -1505,3 +1505,7 @@ describe("find closest insertable", () => {
expect(findClosestInsertable(newListItemFragment)).toEqual(undefined);
});
});
+
+test("get undefined instead of instance path when no instances found", () => {
+ expect(getInstancePath(["boxId"], new Map())).toEqual(undefined);
+});
diff --git a/apps/builder/app/shared/instance-utils.ts b/apps/builder/app/shared/instance-utils.ts
index 37a5404e2e..45ae284a78 100644
--- a/apps/builder/app/shared/instance-utils.ts
+++ b/apps/builder/app/shared/instance-utils.ts
@@ -315,6 +315,9 @@ export const insertWebstudioFragmentAt = (
insertable.parentSelector,
data.instances
);
+ if (instancePath === undefined) {
+ return;
+ }
const { newInstanceIds } = insertWebstudioFragmentCopy({
data,
fragment,
@@ -432,8 +435,11 @@ export const reparentInstance = (
export const deleteInstanceMutable = (
data: Omit<WebstudioData, "pages">,
- instancePath: InstancePath
+ instancePath: undefined | InstancePath
) => {
+ if (instancePath === undefined) {
+ return false;
+ }
const {
instances,
props,
diff --git a/apps/builder/app/shared/pages/use-switch-page.ts b/apps/builder/app/shared/pages/use-switch-page.ts
index d0591b9962..96dc7e0074 100644
--- a/apps/builder/app/shared/pages/use-switch-page.ts
+++ b/apps/builder/app/shared/pages/use-switch-page.ts
@@ -109,6 +109,19 @@ export const useSyncPageUrl = () => {
})
);
}, [builderMode, navigate, page, pageHash]);
+
+ useEffect(() => {
+ return $selectedPage.subscribe((page) => {
+ // switch to home page when current one does not exist
+ // possible when undo creating page
+ if (page === undefined) {
+ const pages = $pages.get();
+ if (pages) {
+ selectPage(pages.homePage.id);
+ }
+ }
+ });
+ });
};
/**