Skip to content

Commit a273aba

Browse files
authored
fix: navigate to home page when undo page creation (#4995)
Fixes #4993
1 parent d6f37ef commit a273aba

File tree

4 files changed

+30
-2
lines changed

4 files changed

+30
-2
lines changed

apps/builder/app/shared/awareness.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ export const getInstancePath = (
9999
instances: Instances,
100100
virtualInstances?: Instances,
101101
temporaryInstances?: Instances
102-
): InstancePath => {
102+
): undefined | InstancePath => {
103103
const instancePath: InstancePath = [];
104104
for (let index = 0; index < instanceSelector.length; index += 1) {
105105
const instanceId = instanceSelector[index];
@@ -116,6 +116,11 @@ export const getInstancePath = (
116116
instanceSelector: instanceSelector.slice(index),
117117
});
118118
}
119+
// all consuming code expect at least one instance to be selected
120+
// though it is possible to get empty array when undo created page
121+
if (instancePath.length === 0) {
122+
return undefined;
123+
}
119124
return instancePath;
120125
};
121126

apps/builder/app/shared/instance-utils.test.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1505,3 +1505,7 @@ describe("find closest insertable", () => {
15051505
expect(findClosestInsertable(newListItemFragment)).toEqual(undefined);
15061506
});
15071507
});
1508+
1509+
test("get undefined instead of instance path when no instances found", () => {
1510+
expect(getInstancePath(["boxId"], new Map())).toEqual(undefined);
1511+
});

apps/builder/app/shared/instance-utils.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,9 @@ export const insertWebstudioFragmentAt = (
315315
insertable.parentSelector,
316316
data.instances
317317
);
318+
if (instancePath === undefined) {
319+
return;
320+
}
318321
const { newInstanceIds } = insertWebstudioFragmentCopy({
319322
data,
320323
fragment,
@@ -432,8 +435,11 @@ export const reparentInstance = (
432435

433436
export const deleteInstanceMutable = (
434437
data: Omit<WebstudioData, "pages">,
435-
instancePath: InstancePath
438+
instancePath: undefined | InstancePath
436439
) => {
440+
if (instancePath === undefined) {
441+
return false;
442+
}
437443
const {
438444
instances,
439445
props,

apps/builder/app/shared/pages/use-switch-page.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,19 @@ export const useSyncPageUrl = () => {
109109
})
110110
);
111111
}, [builderMode, navigate, page, pageHash]);
112+
113+
useEffect(() => {
114+
return $selectedPage.subscribe((page) => {
115+
// switch to home page when current one does not exist
116+
// possible when undo creating page
117+
if (page === undefined) {
118+
const pages = $pages.get();
119+
if (pages) {
120+
selectPage(pages.homePage.id);
121+
}
122+
}
123+
});
124+
});
112125
};
113126

114127
/**

0 commit comments

Comments
 (0)