Skip to content

Commit a1c05ba

Browse files
authored
refactor: move selected instance store into awareness (#4388)
We need to avoid accessing instance selector where possible to make logic hookable. Here moved `$selectedInstance` into awareness and switch to it in many places instance selector is accessed directly.
1 parent 4c26097 commit a1c05ba

File tree

21 files changed

+172
-190
lines changed

21 files changed

+172
-190
lines changed

apps/builder/app/builder/features/ai/ai-fetch-result.ts

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import {
2222
$project,
2323
$props,
2424
$registeredComponentMetas,
25-
$selectedInstanceSelector,
2625
$styleSourceSelections,
2726
$styles,
2827
} from "~/shared/nano-states";
@@ -37,6 +36,7 @@ import {
3736
} from "./api-exceptions";
3837
import { isFeatureEnabled } from "@webstudio-is/feature-flags";
3938
import { fetch } from "~/shared/fetch.client";
39+
import { $selectedInstance } from "~/shared/awareness";
4040

4141
const unknownArray = z.array(z.unknown());
4242

@@ -253,7 +253,7 @@ const restoreComponentsNamespace = (operations: operations.WsOperations) => {
253253

254254
const $jsx = computed(
255255
[
256-
$selectedInstanceSelector,
256+
$selectedInstance,
257257
$instances,
258258
$props,
259259
$dataSources,
@@ -262,25 +262,20 @@ const $jsx = computed(
262262
$styleSourceSelections,
263263
],
264264
(
265-
selectedInstanceSelector,
265+
instance,
266266
instances,
267267
props,
268268
dataSources,
269269
metas,
270270
styles,
271271
styleSourceSelections
272272
) => {
273-
if (selectedInstanceSelector === undefined) {
274-
return;
275-
}
276-
277-
const [rootInstanceId] = selectedInstanceSelector;
278-
const instance = instances.get(rootInstanceId);
279273
if (instance === undefined) {
280274
return;
281275
}
276+
282277
const indexesWithinAncestors = getIndexesWithinAncestors(metas, instances, [
283-
rootInstanceId,
278+
instance.id,
284279
]);
285280
const scope = createScope();
286281

@@ -303,7 +298,7 @@ const $jsx = computed(
303298
}),
304299
});
305300

306-
const treeInstanceIds = findTreeInstanceIds(instances, rootInstanceId);
301+
const treeInstanceIds = findTreeInstanceIds(instances, instance.id);
307302

308303
const sheet = createRegularStyleSheet({ name: "ssr" });
309304
for (const styleDecl of styles.values()) {

apps/builder/app/builder/features/ai/apply-operations.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
import { serverSyncStore } from "~/shared/sync";
1+
import { nanoid } from "nanoid";
2+
import { getStyleDeclKey, Instance, type StyleSource } from "@webstudio-is/sdk";
23
import { generateDataFromEmbedTemplate } from "@webstudio-is/react-sdk";
34
import type { copywriter, operations } from "@webstudio-is/ai";
5+
import { serverSyncStore } from "~/shared/sync";
46
import { isBaseBreakpoint } from "~/shared/breakpoints";
57
import {
68
deleteInstanceMutable,
@@ -12,14 +14,12 @@ import {
1214
$instances,
1315
$registeredComponentMetas,
1416
$selectedInstanceSelector,
15-
$selectedInstance,
1617
$styleSourceSelections,
1718
$styleSources,
1819
$styles,
1920
} from "~/shared/nano-states";
2021
import type { DroppableTarget, InstanceSelector } from "~/shared/tree-utils";
21-
import { getStyleDeclKey, Instance, type StyleSource } from "@webstudio-is/sdk";
22-
import { nanoid } from "nanoid";
22+
import { $selectedInstance } from "~/shared/awareness";
2323

2424
export const applyOperations = (operations: operations.WsOperations) => {
2525
for (const operation of operations) {

apps/builder/app/builder/features/inspector/inspector.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import { StylePanel } from "~/builder/features/style-panel";
2222
import { SettingsPanelContainer } from "~/builder/features/settings-panel";
2323
import { FloatingPanelProvider } from "~/builder/shared/floating-panel";
2424
import {
25-
$selectedInstance,
2625
$registeredComponentMetas,
2726
$dragAndDropState,
2827
} from "~/shared/nano-states";
@@ -32,7 +31,7 @@ import { MetaIcon } from "~/builder/shared/meta-icon";
3231
import { getInstanceLabel } from "~/shared/instance-utils";
3332
import { BindingPopoverProvider } from "~/builder/shared/binding-popover";
3433
import { $activeInspectorPanel } from "~/builder/shared/nano-states";
35-
import { $selectedPage } from "~/shared/awareness";
34+
import { $selectedInstance, $selectedPage } from "~/shared/awareness";
3635

3736
const InstanceInfo = ({ instance }: { instance: Instance }) => {
3837
const metas = useStore($registeredComponentMetas);

apps/builder/app/builder/features/navigator/css-preview.tsx

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ import type { StyleMap, StyleProperty } from "@webstudio-is/css-engine";
1515
import { CollapsibleSection } from "~/builder/shared/collapsible-section";
1616
import { highlightCss } from "~/builder/shared/code-highlight";
1717
import type { ComputedStyleDecl } from "~/shared/style-object-model";
18-
import { $selectedInstanceSelector } from "~/shared/nano-states";
1918
import { $definedComputedStyles } from "~/builder/features/style-panel/shared/model";
19+
import { $selectedInstance } from "~/shared/awareness";
2020

2121
const preStyle = css(textVariants.mono, {
2222
margin: 0,
@@ -80,13 +80,12 @@ const getCssText = (
8080
};
8181

8282
const $highlightedCss = computed(
83-
[$selectedInstanceSelector, $definedComputedStyles],
84-
(instanceSelector, computedStyles) => {
85-
if (instanceSelector === undefined) {
83+
[$selectedInstance, $definedComputedStyles],
84+
(instance, computedStyles) => {
85+
if (instance === undefined) {
8686
return;
8787
}
88-
const [instanceId] = instanceSelector;
89-
const cssText = getCssText(computedStyles, instanceId);
88+
const cssText = getCssText(computedStyles, instance.id);
9089
return highlightCss(cssText);
9190
}
9291
);

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

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ import {
5555
EditorDialogControl,
5656
} from "~/builder/shared/code-editor-base";
5757
import { parseCurl, type CurlRequest } from "./curl";
58-
import { $selectedPage } from "~/shared/awareness";
58+
import { $selectedInstance, $selectedPage } from "~/shared/awareness";
5959

6060
const validateUrl = (value: string, scope: Record<string, unknown>) => {
6161
const evaluatedValue = evaluateExpressionWithinScope(value, scope);
@@ -581,12 +581,11 @@ export const ResourceForm = forwardRef<
581581

582582
useImperativeHandle(ref, () => ({
583583
save: (formData) => {
584-
const instanceSelector = $selectedInstanceSelector.get();
585-
if (instanceSelector === undefined) {
584+
const instanceId = $selectedInstance.get()?.id;
585+
if (instanceId === undefined) {
586586
return;
587587
}
588588
const name = z.string().parse(formData.get("name"));
589-
const [instanceId] = instanceSelector;
590589
const newResource: Resource = {
591590
id: resource?.id ?? nanoid(),
592591
name,
@@ -715,12 +714,11 @@ export const SystemResourceForm = forwardRef<
715714

716715
useImperativeHandle(ref, () => ({
717716
save: (formData) => {
718-
const instanceSelector = $selectedInstanceSelector.get();
719-
if (instanceSelector === undefined) {
717+
const instanceId = $selectedInstance.get()?.id;
718+
if (instanceId === undefined) {
720719
return;
721720
}
722721
const name = z.string().parse(formData.get("name"));
723-
const [instanceId] = instanceSelector;
724722
const newResource: Resource = {
725723
id: resource?.id ?? nanoid(),
726724
name,
@@ -826,8 +824,8 @@ export const GraphqlResourceForm = forwardRef<
826824

827825
useImperativeHandle(ref, () => ({
828826
save: (formData) => {
829-
const instanceSelector = $selectedInstanceSelector.get();
830-
if (instanceSelector === undefined) {
827+
const instanceId = $selectedInstance.get()?.id;
828+
if (instanceId === undefined) {
831829
return;
832830
}
833831
const name = z.string().parse(formData.get("name"));
@@ -837,7 +835,6 @@ export const GraphqlResourceForm = forwardRef<
837835
["variables", variables],
838836
])
839837
);
840-
const [instanceId] = instanceSelector;
841838
const newResource: Resource = {
842839
id: resource?.id ?? nanoid(),
843840
name,

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
import { useStore } from "@nanostores/react";
2+
import type { Instance } from "@webstudio-is/sdk";
23
import { InputField, useId } from "@webstudio-is/design-system";
3-
import {
4-
$instances,
5-
$registeredComponentMetas,
6-
$selectedInstance,
7-
} from "~/shared/nano-states";
4+
import { $instances, $registeredComponentMetas } from "~/shared/nano-states";
85
import { HorizontalLayout, Label, Row, useLocalValue } from "./shared";
96
import { getInstanceLabel } from "~/shared/instance-utils";
107
import { serverSyncStore } from "~/shared/sync";
11-
import type { Instance } from "@webstudio-is/sdk";
8+
import { $selectedInstance } from "~/shared/awareness";
129

1310
const saveLabel = (label: string, selectedInstance: Instance) => {
1411
serverSyncStore.createTransaction([$instances], (instances) => {

apps/builder/app/builder/features/settings-panel/variable-popover.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ import {
5252
$dataSources,
5353
$resources,
5454
$areResourcesLoading,
55-
$selectedInstanceSelector,
5655
invalidateResource,
5756
getComputedResource,
5857
} from "~/shared/nano-states";
@@ -71,6 +70,7 @@ import {
7170
SystemResourceForm,
7271
} from "./resource-panel";
7372
import { generateCurl } from "./curl";
73+
import { $selectedInstance } from "~/shared/awareness";
7474

7575
const validateName = (value: string) =>
7676
value.trim().length === 0 ? "Name is required" : "";
@@ -241,11 +241,10 @@ const useValuePanelRef = ({
241241
}) => {
242242
useImperativeHandle(ref, () => ({
243243
save: (formData) => {
244-
const instanceSelector = $selectedInstanceSelector.get();
245-
if (instanceSelector === undefined) {
244+
const instanceId = $selectedInstance.get()?.id;
245+
if (instanceId === undefined) {
246246
return;
247247
}
248-
const [instanceId] = instanceSelector;
249248
const dataSourceId = variable?.id ?? nanoid();
250249
// preserve existing instance scope when edit
251250
const scopeInstanceId = variable?.scopeInstanceId ?? instanceId;

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,21 +46,20 @@ import {
4646
VariablePopoverProvider,
4747
VariablePopoverTrigger,
4848
} from "./variable-popover";
49-
import { $selectedPage } from "~/shared/awareness";
49+
import { $selectedInstance, $selectedPage } from "~/shared/awareness";
5050

5151
/**
5252
* find variables defined specifically on this selected instance
5353
*/
5454
const $instanceVariables = computed(
55-
[$selectedInstanceSelector, $dataSources],
56-
(instanceSelector, dataSources) => {
55+
[$selectedInstance, $dataSources],
56+
(instance, dataSources) => {
5757
const matchedVariables: DataSource[] = [];
58-
if (instanceSelector === undefined) {
58+
if (instance === undefined) {
5959
return matchedVariables;
6060
}
61-
const [instanceId] = instanceSelector;
6261
for (const dataSource of dataSources.values()) {
63-
if (instanceId === dataSource.scopeInstanceId) {
62+
if (instance.id === dataSource.scopeInstanceId) {
6463
matchedVariables.push(dataSource);
6564
}
6665
}

apps/builder/app/builder/features/style-panel/property-label.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import {
2424
$instances,
2525
$registeredComponentMetas,
2626
$styleSources,
27-
$virtualInstances,
2827
} from "~/shared/nano-states";
2928
import { getInstanceLabel } from "~/shared/instance-utils";
3029
import type {
@@ -34,6 +33,7 @@ import type {
3433
import { useComputedStyles } from "./shared/model";
3534
import { StyleSourceBadge } from "./style-source";
3635
import { createBatchUpdate } from "./shared/use-style-data";
36+
import { $virtualInstances } from "~/shared/awareness";
3737

3838
const $isAltPressed = atom(false);
3939
if (typeof window !== "undefined") {

apps/builder/app/builder/features/style-panel/sections/advanced/advanced.stories.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { registerContainers } from "~/shared/sync";
33
import { Section } from "./advanced";
44
import {
55
$breakpoints,
6+
$instances,
67
$selectedBreakpointId,
78
$styles,
89
$styleSourceSelections,
@@ -27,6 +28,11 @@ $styles.set(new Map([[getStyleDeclKey(backgroundImage), backgroundImage]]));
2728
$styleSourceSelections.set(
2829
new Map([["box", { instanceId: "box", values: ["local"] }]])
2930
);
31+
$instances.set(
32+
new Map([
33+
["box", { type: "instance", id: "box", component: "Box", children: [] }],
34+
])
35+
);
3036
$awareness.set({
3137
pageId: "",
3238
instanceSelector: ["box"],

0 commit comments

Comments
 (0)