Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

experimental: save path in history when navigate with link #3381

Merged
merged 2 commits into from
May 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 2 additions & 22 deletions apps/builder/app/builder/features/address-bar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import {
ROOT_FOLDER_ID,
getPagePath,
type System,
findPageByIdOrPath,
} from "@webstudio-is/sdk";
import {
$dataSourceVariables,
Expand All @@ -47,7 +46,7 @@ import {
matchPathnamePattern,
tokenizePathnamePattern,
} from "~/builder/shared/url-pattern";
import { serverSyncStore } from "~/shared/sync";
import { savePathInHistory } from "~/shared/pages";

const $selectedPagePath = computed([$selectedPage, $pages], (page, pages) => {
if (pages === undefined || page === undefined) {
Expand Down Expand Up @@ -80,25 +79,6 @@ const $selectedPageHistory = computed(
(page) => page?.history ?? []
);

/**
* put new path into the beginning of history
* and drop paths in the end when exceeded 20
*/
const savePathInHistory = (path: string, pageId: 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);
});
};

const useCopyUrl = (pageUrl: string) => {
const [copyState, setCopyState] = useState<"copy" | "copied">("copy");
// reset copied state after 2 seconds
Expand Down Expand Up @@ -342,7 +322,7 @@ const AddressBar = forwardRef<
}
updateSystem(page, { params: newParams });
const compiledPath = compilePathnamePattern(tokens, newParams);
savePathInHistory(compiledPath, page.id);
savePathInHistory(page.id, compiledPath);
if (errors.size === 0) {
onSubmit();
}
Expand Down
3 changes: 2 additions & 1 deletion apps/builder/app/canvas/interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
$selectedPage,
updateSystem,
} from "~/shared/nano-states";
import { switchPage } from "~/shared/pages";
import { savePathInHistory, switchPage } from "~/shared/pages";

const isAbsoluteUrl = (href: string) => {
try {
Expand Down Expand Up @@ -59,6 +59,7 @@ const switchPageAndUpdateSystem = (href: string, formData?: FormData) => {
const search = Object.fromEntries(pageHref.searchParams);
switchPage(page.id, pageHref.hash);
updateSystem(page, { params, search });
savePathInHistory(page.id, pageHref.pathname);
break;
}
}
Expand Down
1 change: 1 addition & 0 deletions apps/builder/app/shared/pages/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./use-switch-page";
export * from "./pages";
65 changes: 65 additions & 0 deletions apps/builder/app/shared/pages/pages.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { describe, expect, test } from "@jest/globals";
import type { Page, Pages } from "@webstudio-is/sdk";
import { $pages } from "~/shared/nano-states";
import { registerContainers } from "~/shared/sync";
import { savePathInHistory } from "./pages";

registerContainers();

const getInitialPages = (page: Page): Pages => ({
folders: [
{
id: "rootId",
name: "",
slug: "",
children: ["homeId", "dynamicId"],
},
],
homePage: {
id: "homeId",
path: "",
name: "",
title: "",
meta: {},
rootInstanceId: "",
systemDataSourceId: "",
},
pages: [page],
});

describe("history", () => {
test("add new path at the start", () => {
$pages.set(
getInitialPages({
id: "dynamicId",
path: "/blog/:date/post/:slug",
name: "",
title: "",
meta: {},
rootInstanceId: "",
systemDataSourceId: "",
})
);
savePathInHistory("dynamicId", "/path1");
expect($pages.get()?.pages[0].history).toEqual(["/path1"]);
savePathInHistory("dynamicId", "/path2");
expect($pages.get()?.pages[0].history).toEqual(["/path2", "/path1"]);
});

test("move existing path to the start", () => {
$pages.set(
getInitialPages({
id: "dynamicId",
path: "/blog/:date/post/:slug",
name: "",
title: "",
meta: {},
rootInstanceId: "",
systemDataSourceId: "",
history: ["/path2", "/path1"],
})
);
savePathInHistory("dynamicId", "/path1");
expect($pages.get()?.pages[0].history).toEqual(["/path1", "/path2"]);
});
});
22 changes: 22 additions & 0 deletions apps/builder/app/shared/pages/pages.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
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);
});
};
Loading