Skip to content

Commit 8427428

Browse files
authored
refactor: add instance key store (#4389)
Here added $instanceKey store which can be used to access props and variables from any part of the tree. This replaces manual JSON.stringify(selectedInstanceSelector) in a few places.
1 parent a1c05ba commit 8427428

File tree

6 files changed

+41
-30
lines changed

6 files changed

+41
-30
lines changed

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { computed } from "nanostores";
12
import { useState } from "react";
23
import { useStore } from "@nanostores/react";
34
import { matchSorter } from "match-sorter";
@@ -17,13 +18,13 @@ import {
1718
$propValuesByInstanceSelector,
1819
$propsIndex,
1920
$props,
20-
$selectedInstanceSelector,
2121
} from "~/shared/nano-states";
2222
import { CollapsibleSectionWithAddButton } from "~/builder/shared/collapsible-section";
2323
import { renderControl } from "../controls/combined";
2424
import { usePropsLogic, type PropAndMeta } from "./use-props-logic";
2525
import { Row } from "../shared";
2626
import { serverSyncStore } from "~/shared/sync";
27+
import { $selectedInstanceKey } from "~/shared/awareness";
2728

2829
type Item = {
2930
name: string;
@@ -193,17 +194,19 @@ export const PropsSection = (props: PropsSectionProps) => {
193194
);
194195
};
195196

197+
const $propValues = computed(
198+
[$propValuesByInstanceSelector, $selectedInstanceKey],
199+
(propValuesByInstanceSelector, instanceKey) =>
200+
propValuesByInstanceSelector.get(instanceKey ?? "")
201+
);
202+
196203
export const PropsSectionContainer = ({
197204
selectedInstance: instance,
198205
}: {
199206
selectedInstance: Instance;
200207
}) => {
201208
const { propsByInstanceId } = useStore($propsIndex);
202-
const propValuesByInstanceSelector = useStore($propValuesByInstanceSelector);
203-
const instanceSelector = useStore($selectedInstanceSelector);
204-
const propValues = propValuesByInstanceSelector.get(
205-
JSON.stringify(instanceSelector)
206-
);
209+
const propValues = useStore($propValues);
207210

208211
const logic = usePropsLogic({
209212
instance,

apps/builder/app/builder/features/settings-panel/resource-panel.tsx

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ import { serverSyncStore } from "~/shared/sync";
4040
import {
4141
$dataSources,
4242
$resources,
43-
$selectedInstanceSelector,
4443
$variableValuesByInstanceSelector,
4544
} from "~/shared/nano-states";
4645
import {
@@ -55,7 +54,11 @@ import {
5554
EditorDialogControl,
5655
} from "~/builder/shared/code-editor-base";
5756
import { parseCurl, type CurlRequest } from "./curl";
58-
import { $selectedInstance, $selectedPage } from "~/shared/awareness";
57+
import {
58+
$selectedInstance,
59+
$selectedInstanceKey,
60+
$selectedPage,
61+
} from "~/shared/awareness";
5962

6063
const validateUrl = (value: string, scope: Record<string, unknown>) => {
6164
const evaluatedValue = evaluateExpressionWithinScope(value, scope);
@@ -380,25 +383,23 @@ const $hiddenDataSourceIds = computed(
380383

381384
const $selectedInstanceScope = computed(
382385
[
383-
$selectedInstanceSelector,
386+
$selectedInstanceKey,
384387
$variableValuesByInstanceSelector,
385388
$dataSources,
386389
$hiddenDataSourceIds,
387390
],
388391
(
389-
instanceSelector,
392+
instanceKey,
390393
variableValuesByInstanceSelector,
391394
dataSources,
392395
hiddenDataSourceIds
393396
) => {
394397
const scope: Record<string, unknown> = {};
395398
const aliases = new Map<string, string>();
396-
if (instanceSelector === undefined) {
399+
if (instanceKey === undefined) {
397400
return { scope, aliases };
398401
}
399-
const values = variableValuesByInstanceSelector.get(
400-
JSON.stringify(instanceSelector)
401-
);
402+
const values = variableValuesByInstanceSelector.get(instanceKey);
402403
if (values) {
403404
for (const [dataSourceId, value] of values) {
404405
if (hiddenDataSourceIds.has(dataSourceId)) {

apps/builder/app/builder/features/settings-panel/shared.tsx

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ import {
3333
import {
3434
$dataSourceVariables,
3535
$dataSources,
36-
$selectedInstanceSelector,
3736
$variableValuesByInstanceSelector,
3837
} from "~/shared/nano-states";
3938
import type { BindingVariant } from "~/builder/shared/binding-popover";
4039
import { humanizeString } from "~/shared/string-utils";
40+
import { $selectedInstanceKey } from "~/shared/awareness";
4141

4242
export type PropValue =
4343
| { type: "number"; value: number }
@@ -312,16 +312,14 @@ export const Row = ({
312312
);
313313

314314
export const $selectedInstanceScope = computed(
315-
[$selectedInstanceSelector, $variableValuesByInstanceSelector, $dataSources],
316-
(instanceSelector, variableValuesByInstanceSelector, dataSources) => {
315+
[$selectedInstanceKey, $variableValuesByInstanceSelector, $dataSources],
316+
(instanceKey, variableValuesByInstanceSelector, dataSources) => {
317317
const scope: Record<string, unknown> = {};
318318
const aliases = new Map<string, string>();
319-
if (instanceSelector === undefined) {
319+
if (instanceKey === undefined) {
320320
return { scope, aliases };
321321
}
322-
const values = variableValuesByInstanceSelector.get(
323-
JSON.stringify(instanceSelector)
324-
);
322+
const values = variableValuesByInstanceSelector.get(instanceKey);
325323
if (values) {
326324
for (const [dataSourceId, value] of values) {
327325
const dataSource = dataSources.get(dataSourceId);

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import {
3030
$instances,
3131
$props,
3232
$resources,
33-
$selectedInstanceSelector,
3433
$variableValuesByInstanceSelector,
3534
} from "~/shared/nano-states";
3635
import { serverSyncStore } from "~/shared/sync";
@@ -46,7 +45,11 @@ import {
4645
VariablePopoverProvider,
4746
VariablePopoverTrigger,
4847
} from "./variable-popover";
49-
import { $selectedInstance, $selectedPage } from "~/shared/awareness";
48+
import {
49+
$selectedInstance,
50+
$selectedInstanceKey,
51+
$selectedPage,
52+
} from "~/shared/awareness";
5053

5154
/**
5255
* find variables defined specifically on this selected instance
@@ -68,11 +71,10 @@ const $instanceVariables = computed(
6871
);
6972

7073
const $instanceVariableValues = computed(
71-
[$selectedInstanceSelector, $variableValuesByInstanceSelector],
72-
(instanceSelector, variableValuesByInstanceSelector) => {
73-
const key = JSON.stringify(instanceSelector);
74-
return variableValuesByInstanceSelector.get(key) ?? new Map();
75-
}
74+
[$selectedInstanceKey, $variableValuesByInstanceSelector],
75+
(instanceKey, variableValuesByInstanceSelector) =>
76+
variableValuesByInstanceSelector.get(instanceKey ?? "") ??
77+
new Map<string, unknown>()
7678
);
7779

7880
/**

apps/builder/app/shared/awareness.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,13 @@ export const $selectedInstance = computed(
5858
}
5959
);
6060

61+
export const $selectedInstanceKey = computed($awareness, (awareness) => {
62+
if (awareness === undefined) {
63+
return;
64+
}
65+
return JSON.stringify(awareness.instanceSelector);
66+
});
67+
6168
export const selectPage = (pageId: Page["id"]) => {
6269
const pages = $pages.get();
6370
if (pages === undefined) {

apps/builder/app/shared/nano-states/components.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ const createHookContext = (): HookContext => {
5454
return;
5555
}
5656

57-
const props = $memoryProps.get();
57+
const props = new Map($memoryProps.get());
5858

5959
const newProps = props.get(JSON.stringify(instanceSelector)) ?? new Map();
6060

@@ -96,7 +96,7 @@ const createHookContext = (): HookContext => {
9696

9797
props.set(JSON.stringify(instanceSelector), newProps);
9898

99-
$memoryProps.set(new Map(props));
99+
$memoryProps.set(props);
100100
},
101101

102102
setPropVariable: (instanceId, propName, value) => {

0 commit comments

Comments
 (0)