Skip to content

Commit b798809

Browse files
authored
refactor: decouple system store from variables (#4888)
We are moving away from binding system to variables and instead gonna have one global variable. Here moved system values into separate store coupled to pages instead of variables. Also fixed suggestions in address bar, probably broke while design system refactorings. Testing - submit form with one named input - check system value is rendered - check system value can be inspected
1 parent 34e18b3 commit b798809

File tree

27 files changed

+182
-262
lines changed

27 files changed

+182
-262
lines changed

apps/builder/app/builder/features/address-bar.stories.tsx

Lines changed: 6 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,15 @@
1-
import { computed } from "nanostores";
21
import { useStore } from "@nanostores/react";
32
import type { Meta, StoryFn } from "@storybook/react";
43
import { Box, Text, theme } from "@webstudio-is/design-system";
54
import { AddressBarPopover } from "./address-bar";
6-
import {
7-
$dataSourceVariables,
8-
$dataSources,
9-
$pages,
10-
} from "~/shared/nano-states";
5+
import { $dataSources, $pages } from "~/shared/nano-states";
116
import { registerContainers } from "~/shared/sync";
127
import { $awareness, $selectedPage } from "~/shared/awareness";
8+
import { $currentSystem } from "~/shared/system";
139

1410
registerContainers();
1511

16-
$dataSources.set(
17-
new Map([
18-
[
19-
"systemId",
20-
{
21-
id: "systemId",
22-
scopeInstanceId: "rootInstanceId",
23-
name: "system",
24-
type: "parameter",
25-
},
26-
],
27-
])
28-
);
12+
$dataSources.set(new Map());
2913

3014
$pages.set({
3115
folders: [
@@ -43,7 +27,6 @@ $pages.set({
4327
title: "",
4428
meta: {},
4529
rootInstanceId: "",
46-
systemDataSourceId: "",
4730
},
4831
pages: [
4932
{
@@ -53,40 +36,24 @@ $pages.set({
5336
title: "",
5437
meta: {},
5538
rootInstanceId: "rootInstanceId",
56-
systemDataSourceId: "systemId",
5739
},
5840
],
5941
});
6042

61-
const $selectedPageSystem = computed(
62-
[$selectedPage, $dataSourceVariables],
63-
(selectedPage, dataSourceVariables) => {
64-
if (selectedPage?.systemDataSourceId === undefined) {
65-
return {};
66-
}
67-
return dataSourceVariables.get(selectedPage.systemDataSourceId);
68-
}
69-
);
70-
7143
const SystemInspect = () => {
72-
const system = useStore($selectedPageSystem);
44+
const system = useStore($currentSystem);
7345
return (
7446
<Text variant="mono" css={{ whiteSpace: "pre" }}>
7547
{JSON.stringify(system, null, 2)}
7648
</Text>
7749
);
7850
};
7951

80-
const $selectedPageHistory = computed(
81-
$selectedPage,
82-
(page) => page?.history ?? []
83-
);
84-
8552
const HistoryInspect = () => {
86-
const history = useStore($selectedPageHistory);
53+
const page = useStore($selectedPage);
8754
return (
8855
<Text variant="mono" css={{ whiteSpace: "pre" }}>
89-
{JSON.stringify(history, null, 2)}
56+
{JSON.stringify(page?.history, null, 2)}
9057
</Text>
9158
);
9259
};

apps/builder/app/builder/features/address-bar.tsx

Lines changed: 6 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,16 @@ import {
3131
findParentFolderByChildId,
3232
ROOT_FOLDER_ID,
3333
getPagePath,
34-
type System,
3534
} from "@webstudio-is/sdk";
36-
import {
37-
$dataSourceVariables,
38-
$pages,
39-
$publishedOrigin,
40-
$selectedPageDefaultSystem,
41-
updateSystem,
42-
} from "~/shared/nano-states";
35+
import { $pages, $publishedOrigin } from "~/shared/nano-states";
4336
import {
4437
compilePathnamePattern,
4538
isPathnamePattern,
4639
matchPathnamePattern,
4740
tokenizePathnamePattern,
4841
} from "~/builder/shared/url-pattern";
49-
import { savePathInHistory } from "~/shared/pages";
5042
import { $selectedPage } from "~/shared/awareness";
43+
import { $currentSystem, updateCurrentSystem } from "~/shared/system";
5144

5245
const $selectedPagePath = computed([$selectedPage, $pages], (page, pages) => {
5346
if (pages === undefined || page === undefined) {
@@ -62,19 +55,6 @@ const $selectedPagePath = computed([$selectedPage, $pages], (page, pages) => {
6255
.replace(/\/+/g, "/");
6356
});
6457

65-
const $selectedPagePathParams = computed(
66-
[$selectedPageDefaultSystem, $selectedPage, $dataSourceVariables],
67-
(defaultSystem, selectedPage, dataSourceVariables) => {
68-
if (selectedPage?.systemDataSourceId === undefined) {
69-
return defaultSystem.params;
70-
}
71-
const system = dataSourceVariables.get(selectedPage.systemDataSourceId) as
72-
| undefined
73-
| System;
74-
return system?.params ?? defaultSystem.params;
75-
}
76-
);
77-
7858
const $selectedPageHistory = computed(
7959
$selectedPage,
8060
(page) => page?.history ?? []
@@ -278,7 +258,7 @@ const AddressBar = forwardRef<
278258
return history.filter((item) => matchPathnamePattern(path, item));
279259
}, [history, path]);
280260
const [pathParams, setPathParams] = useState(
281-
() => $selectedPagePathParams.get() ?? {}
261+
() => $currentSystem.get().params
282262
);
283263
const tokens = tokenizePathnamePattern(path);
284264
const compiledPath = compilePathnamePattern(tokens, pathParams);
@@ -307,26 +287,11 @@ const AddressBar = forwardRef<
307287
return (
308288
<form
309289
ref={mergeRefs(ref, containerRef)}
310-
style={{ position: "relative" }}
311290
onSubmit={(event) => {
312291
event.preventDefault();
313292
const formData = new FormData(event.currentTarget);
314-
const path = $selectedPagePath.get();
315-
const tokens = tokenizePathnamePattern(path);
316-
// delete stale fields
317-
const newParams: Record<string, string> = {};
318-
for (const token of tokens) {
319-
if (token.type === "param") {
320-
newParams[token.name] = String(formData.get(token.name) ?? "");
321-
}
322-
}
323-
const page = $selectedPage.get();
324-
if (page === undefined) {
325-
return;
326-
}
327-
updateSystem(page, { params: newParams });
328-
const compiledPath = compilePathnamePattern(tokens, newParams);
329-
savePathInHistory(page.id, compiledPath);
293+
const params = Object.fromEntries(formData) as Record<string, string>;
294+
updateCurrentSystem({ params });
330295
if (errors.size === 0) {
331296
onSubmit();
332297
}
@@ -357,6 +322,7 @@ const AddressBar = forwardRef<
357322
key={index}
358323
name={token.name}
359324
fieldSizing="content"
325+
autoComplete="off"
360326
css={{ minWidth: theme.spacing[15] }}
361327
color={errors.has(token.name) ? "error" : undefined}
362328
placeholder={token.name}

apps/builder/app/builder/features/command-panel/command-panel.stories.tsx

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,12 @@ $breakpoints.set(
4040
)
4141
);
4242

43-
const pages = createDefaultPages({
44-
rootInstanceId: "",
45-
systemDataSourceId: "",
46-
});
43+
const pages = createDefaultPages({ rootInstanceId: "" });
4744
pages.pages.push({
4845
id: "page2",
4946
path: "",
5047
name: "Second Page",
5148
rootInstanceId: "",
52-
systemDataSourceId: "",
5349
title: "",
5450
meta: {},
5551
});
@@ -58,7 +54,6 @@ pages.pages.push({
5854
path: "",
5955
name: "Thrid Page",
6056
rootInstanceId: "",
61-
systemDataSourceId: "",
6257
title: "",
6358
meta: {},
6459
});

apps/builder/app/builder/features/pages/page-settings.stories.tsx

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,7 @@ $assets.set(
3737
])
3838
);
3939

40-
const pages = createDefaultPages({
41-
rootInstanceId: "root-instance-id",
42-
systemDataSourceId: "systemDataSourceId",
43-
});
40+
const pages = createDefaultPages({ rootInstanceId: "root-instance-id" });
4441
pages.meta = {
4542
siteName: "Project name",
4643
faviconAssetId: "imageId",
@@ -53,7 +50,6 @@ pages.pages.push({
5350
name: "page-name",
5451
meta: {},
5552
rootInstanceId: "root-instance-id",
56-
systemDataSourceId: "systemDataSourceId",
5753
});
5854
const rootFolder = pages.folders.find(isRootFolder);
5955
rootFolder?.children.push("pageId");

apps/builder/app/builder/features/pages/page-utils.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
} from "~/shared/nano-states";
2727
import { registerContainers } from "~/shared/sync";
2828
import { $awareness } from "~/shared/awareness";
29+
import { updateCurrentSystem } from "~/shared/system";
2930

3031
setEnv("*");
3132
registerContainers();
@@ -525,10 +526,9 @@ test("page root scope should prefill default system variable value", () => {
525526
],
526527
]),
527528
});
528-
529-
$dataSourceVariables.set(
530-
new Map([["systemId", { params: { slug: "my-post" }, search: {} }]])
531-
);
529+
updateCurrentSystem({
530+
params: { slug: "my-post" },
531+
});
532532
expect($pageRootScope.get()).toEqual({
533533
aliases: new Map([["$ws$dataSource$systemId", "system"]]),
534534
scope: {

apps/builder/app/builder/features/project-settings/project-settings.stories.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ export const Redirects = () => {
3333
title: `"My Title"`,
3434
meta: {},
3535
rootInstanceId: "body",
36-
systemDataSourceId: "",
3736
},
3837
pages: [],
3938
folders: [],

apps/builder/app/builder/features/project-settings/utils.test.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ describe("getExistingRoutePaths", () => {
66
test("gets all the route paths that exists in the project", () => {
77
const pages = createDefaultPages({
88
rootInstanceId: "rootInstanceId",
9-
systemDataSourceId: "systemDataSourceId",
109
homePageId: "homePageId",
1110
});
1211

@@ -16,7 +15,6 @@ describe("getExistingRoutePaths", () => {
1615
name: "Page",
1716
path: "/page",
1817
rootInstanceId: "rootInstanceId",
19-
systemDataSourceId: "systemDataSourceId",
2018
title: `"Page"`,
2119
});
2220

@@ -26,7 +24,6 @@ describe("getExistingRoutePaths", () => {
2624
name: "Blog",
2725
path: "/blog/:id",
2826
rootInstanceId: "rootInstanceId",
29-
systemDataSourceId: "systemDataSourceId",
3027
title: `"Blog"`,
3128
});
3229

apps/builder/app/builder/features/settings-panel/props-section/props-section.stories.tsx

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,10 @@ const page = (name: string, path: string): Page => ({
2626
path,
2727
meta: {},
2828
rootInstanceId: unique(),
29-
systemDataSourceId: unique(),
3029
});
3130

3231
$pages.set({
33-
...createDefaultPages({
34-
rootInstanceId: unique(),
35-
systemDataSourceId: unique(),
36-
}),
37-
32+
...createDefaultPages({ rootInstanceId: unique() }),
3833
homePage: page("Home", "") as Page & { path: "" },
3934
pages: [
4035
page("About", "/about"),

apps/builder/app/builder/features/settings-panel/variables-section.stories.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@ $instances.set(
2222
["box", { id: "box", type: "instance", component: "Box", children: [] }],
2323
])
2424
);
25-
$pages.set(
26-
createDefaultPages({ rootInstanceId: "box", systemDataSourceId: "system" })
27-
);
25+
$pages.set(createDefaultPages({ rootInstanceId: "box" }));
2826
$awareness.set({ pageId: "home", instanceSelector: ["box"] });
2927

3028
export const VariablesSection: StoryObj = {

apps/builder/app/builder/features/style-panel/sections/backgrounds/background-content.stories.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ $pages.set(
3737
createDefaultPages({
3838
homePageId: "homePageId",
3939
rootInstanceId: "box",
40-
systemDataSourceId: "systemId",
4140
})
4241
);
4342
$awareness.set({

0 commit comments

Comments
 (0)