Files
webstudio/apps/builder/app/shared/pages/pages.ts
T
Bogdan Chadkin 45fdd6404c experimental: save path in history when navigate with link (#3381)
Here improved history support. Now navigating with links from preview
adds paths into page history. This will let to reach same page without
switching to the list of posts.
2024-05-19 14:35:25 +03:00

23 lines
672 B
TypeScript

import { findPageByIdOrPath } from "@webstudio-is/sdk";
import { $pages } from "../nano-states";
import { serverSyncStore } from "../sync";
/**
* put new path into the beginning of history
* and drop paths in the end when exceeded 20
*/
export const savePathInHistory = (pageId: string, path: string) => {
serverSyncStore.createTransaction([$pages], (pages) => {
if (pages === undefined) {
return;
}
const page = findPageByIdOrPath(pageId, pages);
if (page === undefined) {
return;
}
const history = Array.from(page.history ?? []);
history.unshift(path);
page.history = Array.from(new Set(history)).slice(0, 20);
});
};