Skip to content

Commit

Permalink
feat(plugin-history-sync): pass SearchParams to fallback activity p…
Browse files Browse the repository at this point in the history
…arams (#462)
  • Loading branch information
tonyfromundefined authored May 22, 2024
1 parent 20d1954 commit cea5137
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/hip-dolphins-camp.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@stackflow/plugin-history-sync": minor
---

Pass initial search params to fallback activity as an activity params
31 changes: 31 additions & 0 deletions extensions/plugin-history-sync/src/historySyncPlugin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1293,4 +1293,35 @@ describe("historySyncPlugin", () => {
);
expect(history.index).toEqual(1);
});

test("historySyncPlugin - search param이 붙은 채로 fallback activity로 이동하는 경우 activity params로 만들어줍니다", async () => {
history = createMemoryHistory({
initialEntries: ["/not/found/route/?foo=1&bar=2"],
});

const coreStore = stackflow({
activityNames: ["Home", "Article"],
plugins: [
historySyncPlugin({
history,
routes: {
Home: "/home",
Article: "/articles/:articleId",
},
fallbackActivity: () => "Home",
}),
],
});

actions = makeActionsProxy({
actions: coreStore.actions,
});

const stack = await actions.getStack();

expect(activeActivity(stack)?.name).toEqual("Home");
expect(activeActivity(stack)?.params.foo).toEqual("1");
expect(activeActivity(stack)?.params.bar).toEqual("2");
expect(path(history.location)).toEqual("/home/?foo=1&bar=2");
});
});
9 changes: 7 additions & 2 deletions extensions/plugin-history-sync/src/historySyncPlugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
import { last } from "./last";
import { makeHistoryTaskQueue } from "./makeHistoryTaskQueue";
import type { UrlPatternOptions } from "./makeTemplate";
import { makeTemplate } from "./makeTemplate";
import { makeTemplate, pathToUrl, urlSearchParamsToMap } from "./makeTemplate";
import { normalizeActivityRouteMap } from "./normalizeActivityRouteMap";
import type { RouteLike } from "./RouteLike";
import { RoutesProvider } from "./RoutesContext";
Expand Down Expand Up @@ -142,12 +142,17 @@ export function historySyncPlugin<
(r) => r.activityName === fallbackActivityName,
);
const fallbackActivityPath = fallbackActivityRoute?.path;
const fallbackActivityParams = urlSearchParamsToMap(
pathToUrl(currentPath).searchParams,
);

return [
makeEvent("Pushed", {
activityId: fallbackActivityId,
activityName: fallbackActivityName,
activityParams: {},
activityParams: {
...fallbackActivityParams,
},
eventDate: new Date().getTime() - MINUTE,
activityContext: {
path: fallbackActivityPath,
Expand Down
4 changes: 2 additions & 2 deletions extensions/plugin-history-sync/src/makeTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import UrlPattern from "url-pattern";

import type { Route } from "./RouteLike";

function pathToUrl(path: string) {
export function pathToUrl(path: string) {
return new URL(path, "file://");
}

function urlSearchParamsToMap(urlSearchParams: URLSearchParams) {
export function urlSearchParamsToMap(urlSearchParams: URLSearchParams) {
const map: { [key: string]: any } = {};

urlSearchParams.forEach((value, key) => {
Expand Down

0 comments on commit cea5137

Please sign in to comment.