Skip to content

Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.

Simplify props state (#783)

Bogdan Chadkinf9052681a315
diff --git a/apps/designer/app/canvas/canvas.tsx b/apps/designer/app/canvas/canvas.tsx
index 84573b6c39..5336d0346d 100644
--- a/apps/designer/app/canvas/canvas.tsx
+++ b/apps/designer/app/canvas/canvas.tsx
@@ -105,7 +105,7 @@ const useCopyPaste = () => {
const allUserProps = useAllUserProps();
const selectedInstanceData = useMemo(
- () => instance && { instance, props: allUserProps[instance.id]?.props },
+ () => instance && { instance, props: allUserProps[instance.id] },
[allUserProps, instance]
);
@@ -133,9 +133,9 @@ type DesignModeProps = {
const DesignMode = ({ treeId, buildId }: DesignModeProps) => {
useManageDesignModeStyles();
- useManageProps({ treeId });
+ useManageProps();
usePublishSelectedInstanceData();
- useInsertInstance({ treeId });
+ useInsertInstance();
useReparentInstance();
useDeleteInstance();
useTrackSelectedElement();
diff --git a/apps/designer/app/canvas/features/wrapper-component/selected-instance-connector.ts b/apps/designer/app/canvas/features/wrapper-component/selected-instance-connector.ts
index 5fedf1da61..5aaeef8a11 100644
--- a/apps/designer/app/canvas/features/wrapper-component/selected-instance-connector.ts
+++ b/apps/designer/app/canvas/features/wrapper-component/selected-instance-connector.ts
@@ -1,5 +1,5 @@
import { useEffect } from "react";
-import type { Instance, InstanceProps } from "@webstudio-is/react-sdk";
+import type { Instance, UserProp } from "@webstudio-is/react-sdk";
import { getBrowserStyle } from "@webstudio-is/react-sdk";
import { publish, subscribe, subscribeAll } from "~/shared/pubsub";
import {
@@ -53,7 +53,7 @@ export const SelectedInstanceConnector = ({
instanceElementRef: { current: undefined | HTMLElement };
instance: Instance;
instanceStylesKey: unknown;
- instanceProps: undefined | InstanceProps;
+ instanceProps: undefined | UserProp[];
}) => {
useEffect(() => {
const element = instanceElementRef.current;
diff --git a/apps/designer/app/canvas/features/wrapper-component/wrapper-component.tsx b/apps/designer/app/canvas/features/wrapper-component/wrapper-component.tsx
index e1113a3aa8..b0a2d25e7a 100644
--- a/apps/designer/app/canvas/features/wrapper-component/wrapper-component.tsx
+++ b/apps/designer/app/canvas/features/wrapper-component/wrapper-component.tsx
@@ -66,7 +66,7 @@ export const WrapperComponentDev = ({
if (instanceProps === undefined) {
return result;
}
- for (const item of instanceProps.props) {
+ for (const item of instanceProps) {
if (item.type !== "asset") {
result[item.prop] = item.value;
}
diff --git a/apps/designer/app/canvas/shared/instance.ts b/apps/designer/app/canvas/shared/instance.ts
index 2ce2f4d023..d4620411c1 100644
--- a/apps/designer/app/canvas/shared/instance.ts
+++ b/apps/designer/app/canvas/shared/instance.ts
@@ -67,7 +67,7 @@ export const findInsertLocation = (
};
};
-export const useInsertInstance = ({ treeId }: { treeId: string }) => {
+export const useInsertInstance = () => {
const [selectedInstance, setSelectedInstance] = useSelectedInstance();
useSubscribe("insertInstance", ({ instance, dropTarget, props }) => {
store.createTransaction(
@@ -85,11 +85,7 @@ export const useInsertInstance = ({ treeId }: { treeId: string }) => {
setSelectedInstance(instance);
}
if (props !== undefined) {
- allUserProps[instance.id] = utils.props.createInstanceProps({
- instanceId: instance.id,
- treeId,
- props,
- });
+ allUserProps[instance.id] = props;
}
}
);
diff --git a/apps/designer/app/canvas/shared/props.ts b/apps/designer/app/canvas/shared/props.ts
index 6efde3c311..d011a1928d 100644
--- a/apps/designer/app/canvas/shared/props.ts
+++ b/apps/designer/app/canvas/shared/props.ts
@@ -6,10 +6,10 @@ import {
deletePropMutable,
} from "~/shared/props-utils";
-export const useManageProps = ({ treeId }: { treeId: string }) => {
+export const useManageProps = () => {
useSubscribe("updateProps", (userPropsUpdates) => {
store.createTransaction([allUserPropsContainer], (allUserProps) => {
- updateAllUserPropsMutable(treeId, allUserProps, userPropsUpdates);
+ updateAllUserPropsMutable(allUserProps, userPropsUpdates);
});
});
diff --git a/apps/designer/app/designer/designer.tsx b/apps/designer/app/designer/designer.tsx
index 625cfbeb9c..d89bd5d9d3 100644
--- a/apps/designer/app/designer/designer.tsx
+++ b/apps/designer/app/designer/designer.tsx
@@ -111,9 +111,7 @@ const useCopyPaste = (publish: Publish) => {
rootInstance,
selectedInstance.id
);
- return (
- instance && { instance, props: selectedInstance.props?.props ?? [] }
- );
+ return instance && { instance, props: selectedInstance.props ?? [] };
}
}, [rootInstance, selectedInstance]);
diff --git a/apps/designer/app/designer/features/props-panel/props-panel.stories.tsx b/apps/designer/app/designer/features/props-panel/props-panel.stories.tsx
index d078d456ee..ad9988a5b3 100644
--- a/apps/designer/app/designer/features/props-panel/props-panel.stories.tsx
+++ b/apps/designer/app/designer/features/props-panel/props-panel.stories.tsx
@@ -13,19 +13,14 @@ export const NoProps: ComponentStoryObj<typeof PropsPanel> = {
id: "1",
component: "Button",
browserStyle: {},
- props: {
- id: "1",
- props: [
- {
- id: "disabled",
- prop: "disabled",
- type: "boolean",
- value: true,
- },
- ],
- instanceId: "2",
- treeId: "1",
- },
+ props: [
+ {
+ id: "disabled",
+ prop: "disabled",
+ type: "boolean",
+ value: true,
+ },
+ ],
},
},
};
@@ -36,12 +31,7 @@ export const RequiredProps: ComponentStoryObj<typeof PropsPanel> = {
id: "1",
component: "Link",
browserStyle: {},
- props: {
- id: "1",
- props: [],
- instanceId: "2",
- treeId: "1",
- },
+ props: [],
},
},
};
@@ -52,12 +42,7 @@ export const DefaultProps: ComponentStoryObj<typeof PropsPanel> = {
id: "1",
component: "Button",
browserStyle: {},
- props: {
- id: "1",
- props: [],
- instanceId: "2",
- treeId: "1",
- },
+ props: [],
},
},
};
@@ -70,18 +55,13 @@ export const AllProps: ComponentStoryObj<typeof PropsPanel> = {
id: "3",
component: "Heading",
browserStyle: {},
- props: {
- id: "2",
- instanceId: "2",
- treeId: "1",
- props: Object.entries(meta).map(([prop, value]) => {
- return {
- id: prop,
- prop,
- value: value?.defaultValue ?? "",
- } as UserProp;
- }),
- },
+ props: Object.entries(meta).map(([prop, value]) => {
+ return {
+ id: prop,
+ prop,
+ value: value?.defaultValue ?? "",
+ } as UserProp;
+ }),
},
},
};
diff --git a/apps/designer/app/designer/features/props-panel/use-props-logic.test.ts b/apps/designer/app/designer/features/props-panel/use-props-logic.test.ts
index 5265f3ec6e..3f0be5efab 100644
--- a/apps/designer/app/designer/features/props-panel/use-props-logic.test.ts
+++ b/apps/designer/app/designer/features/props-panel/use-props-logic.test.ts
@@ -17,12 +17,7 @@ const getSelectedInstanceData = (
id: nanoid(8),
component: componentName,
browserStyle: {},
- props: {
- id: nanoid(8),
- props,
- instanceId: nanoid(8),
- treeId: nanoid(8),
- },
+ props,
};
};
diff --git a/apps/designer/app/designer/features/props-panel/use-props-logic.ts b/apps/designer/app/designer/features/props-panel/use-props-logic.ts
index e2868ba8ba..3946e495ab 100644
--- a/apps/designer/app/designer/features/props-panel/use-props-logic.ts
+++ b/apps/designer/app/designer/features/props-panel/use-props-logic.ts
@@ -63,11 +63,7 @@ const getRequiredProps = (
const { component } = selectedInstanceData;
const meta = getComponentMetaProps(component);
- const props =
- selectedInstanceData.props === undefined ||
- selectedInstanceData.props.props.length === 0
- ? []
- : selectedInstanceData.props.props;
+ const props = selectedInstanceData.props ?? [];
return Object.entries(meta)
.filter(([_, value]) => value?.required)
@@ -101,11 +97,7 @@ const getPropsWithDefaultValue = (
const { component } = selectedInstanceData;
const meta = getComponentMetaProps(component);
- const props =
- selectedInstanceData.props === undefined ||
- selectedInstanceData.props.props.length === 0
- ? []
- : selectedInstanceData.props.props;
+ const props = selectedInstanceData.props ?? [];
return Object.entries(meta)
.filter(([_, value]) => value?.defaultValue != null)
@@ -129,11 +121,7 @@ const getPropsWithDefaultValue = (
const getInitialProps = (
selectedInstanceData: SelectedInstanceData
): UserProp[] => {
- const props =
- selectedInstanceData.props === undefined ||
- selectedInstanceData.props.props.length === 0
- ? []
- : selectedInstanceData.props.props;
+ const props = selectedInstanceData.props ?? [];
const { component } = selectedInstanceData;
const meta = getComponentMeta(component);
@@ -179,11 +167,7 @@ export const usePropsLogic = ({
selectedInstanceData,
publish,
}: UsePropsLogic) => {
- const props =
- selectedInstanceData.props === undefined ||
- selectedInstanceData.props.props.length === 0
- ? []
- : selectedInstanceData.props.props;
+ const props = selectedInstanceData.props ?? [];
const [requiredProps] = useState<Array<UserProp>>(() =>
uniqBy(
@@ -204,7 +188,6 @@ export const usePropsLogic = ({
publish({
type: "updateProps",
payload: {
- propsId: selectedInstanceData.props?.id,
instanceId: selectedInstanceData.id,
updates,
},
diff --git a/apps/designer/app/shared/props-utils/__snapshots__/update-props.test.ts.snap b/apps/designer/app/shared/props-utils/__snapshots__/update-props.test.ts.snap
index 11272d16ab..0ceac3683b 100644
--- a/apps/designer/app/shared/props-utils/__snapshots__/update-props.test.ts.snap
+++ b/apps/designer/app/shared/props-utils/__snapshots__/update-props.test.ts.snap
@@ -2,67 +2,47 @@
exports[`Update props with added prop 1`] = `
{
- "instanceId": {
- "id": "id",
- "instanceId": "instanceId",
- "props": [
- {
- "id": "propId",
- "prop": "a",
- "type": "string",
- "value": "1",
- },
- ],
- "treeId": "treeId",
- },
+ "instanceId": [
+ {
+ "id": "propId",
+ "prop": "a",
+ "type": "string",
+ "value": "1",
+ },
+ ],
}
`;
exports[`Update props with empty updates 1`] = `
{
- "instanceId": {
- "id": "id",
- "instanceId": "instanceId",
- "props": [],
- "treeId": "treeId",
- },
+ "instanceId": [],
}
`;
exports[`Update props with renamed prop name 1`] = `
{
- "instanceId": {
- "id": "id",
- "instanceId": "instanceId",
- "props": [
- {
- "id": "propId",
- "prop": "b",
- "required": undefined,
- "type": "string",
- "value": "1",
- },
- ],
- "treeId": "treeId",
- },
+ "instanceId": [
+ {
+ "id": "propId",
+ "prop": "b",
+ "required": undefined,
+ "type": "string",
+ "value": "1",
+ },
+ ],
}
`;
exports[`Update props with updated prop value 1`] = `
{
- "instanceId": {
- "id": "id",
- "instanceId": "instanceId",
- "props": [
- {
- "id": "propId",
- "prop": "a",
- "required": undefined,
- "type": "string",
- "value": "2",
- },
- ],
- "treeId": "treeId",
- },
+ "instanceId": [
+ {
+ "id": "propId",
+ "prop": "a",
+ "required": undefined,
+ "type": "string",
+ "value": "2",
+ },
+ ],
}
`;
diff --git a/apps/designer/app/shared/props-utils/delete-prop.test.ts b/apps/designer/app/shared/props-utils/delete-prop.test.ts
index e987f3d482..ebe1756f2c 100644
--- a/apps/designer/app/shared/props-utils/delete-prop.test.ts
+++ b/apps/designer/app/shared/props-utils/delete-prop.test.ts
@@ -5,15 +5,10 @@ import { deletePropMutable } from "./delete-prop";
describe("Delete prop", () => {
test("found and deleted", () => {
const propsMap: AllUserProps = {
- instanceId: {
- id: "id",
- instanceId: "instanceId",
- treeId: "treeId",
- props: [
- { id: "propId", prop: "b", value: "1", type: "string" },
- { id: "propId1", prop: "a", value: "1", type: "string" },
- ],
- },
+ instanceId: [
+ { id: "propId", prop: "b", value: "1", type: "string" },
+ { id: "propId1", prop: "a", value: "1", type: "string" },
+ ],
};
const isDeleted = deletePropMutable(propsMap, {
@@ -22,19 +17,14 @@ describe("Delete prop", () => {
});
expect(propsMap).toMatchInlineSnapshot(`
{
- "instanceId": {
- "id": "id",
- "instanceId": "instanceId",
- "props": [
- {
- "id": "propId1",
- "prop": "a",
- "type": "string",
- "value": "1",
- },
- ],
- "treeId": "treeId",
- },
+ "instanceId": [
+ {
+ "id": "propId1",
+ "prop": "a",
+ "type": "string",
+ "value": "1",
+ },
+ ],
}
`);
expect(isDeleted).toBeTruthy();
@@ -42,15 +32,10 @@ describe("Delete prop", () => {
test("not found and not deleted", () => {
const propsMap: AllUserProps = {
- instanceId: {
- id: "id",
- instanceId: "instanceId",
- treeId: "treeId",
- props: [
- { id: "propId", prop: "b", value: "1", type: "string" },
- { id: "propId1", prop: "a", value: "1", type: "string" },
- ],
- },
+ instanceId: [
+ { id: "propId", prop: "b", value: "1", type: "string" },
+ { id: "propId1", prop: "a", value: "1", type: "string" },
+ ],
};
const isDeleted = deletePropMutable(propsMap, {
@@ -59,25 +44,20 @@ describe("Delete prop", () => {
});
expect(propsMap).toMatchInlineSnapshot(`
{
- "instanceId": {
- "id": "id",
- "instanceId": "instanceId",
- "props": [
- {
- "id": "propId",
- "prop": "b",
- "type": "string",
- "value": "1",
- },
- {
- "id": "propId1",
- "prop": "a",
- "type": "string",
- "value": "1",
- },
- ],
- "treeId": "treeId",
- },
+ "instanceId": [
+ {
+ "id": "propId",
+ "prop": "b",
+ "type": "string",
+ "value": "1",
+ },
+ {
+ "id": "propId1",
+ "prop": "a",
+ "type": "string",
+ "value": "1",
+ },
+ ],
}
`);
expect(isDeleted).toBeFalsy();
diff --git a/apps/designer/app/shared/props-utils/delete-prop.ts b/apps/designer/app/shared/props-utils/delete-prop.ts
index 6febc1d340..7f2861057a 100644
--- a/apps/designer/app/shared/props-utils/delete-prop.ts
+++ b/apps/designer/app/shared/props-utils/delete-prop.ts
@@ -4,14 +4,14 @@ export const deletePropMutable = (
allProps: AllUserProps,
deleteProp: DeleteProp
) => {
- const prop = allProps[deleteProp.instanceId];
- if (prop === undefined) {
+ const props = allProps[deleteProp.instanceId];
+ if (props === undefined) {
return false;
}
- const index = prop.props.findIndex(({ id }) => id === deleteProp.propId);
+ const index = props.findIndex(({ id }) => id === deleteProp.propId);
if (index === -1) {
return false;
}
- prop.props.splice(index, 1);
+ props.splice(index, 1);
return true;
};
diff --git a/apps/designer/app/shared/props-utils/update-props.test.ts b/apps/designer/app/shared/props-utils/update-props.test.ts
index e1aa20f119..8e7b95aa53 100644
--- a/apps/designer/app/shared/props-utils/update-props.test.ts
+++ b/apps/designer/app/shared/props-utils/update-props.test.ts
@@ -9,87 +9,57 @@ import { updateAllUserPropsMutable } from "./update-props";
describe("Update props", () => {
test("with empty updates", () => {
const propsMap: AllUserProps = {
- instanceId: {
- id: "id",
- instanceId: "instanceId",
- treeId: "treeId",
- props: [],
- },
+ instanceId: [],
};
const update: UserPropsUpdates = {
- propsId: "id",
instanceId: "instanceId",
updates: [],
};
- updateAllUserPropsMutable("treeId", propsMap, update);
+ updateAllUserPropsMutable(propsMap, update);
expect(propsMap).toMatchSnapshot();
});
test("with added prop", () => {
const propsMap: AllUserProps = {
- instanceId: {
- id: "id",
- instanceId: "instanceId",
- treeId: "treeId",
- props: [],
- },
+ instanceId: [],
};
const update: UserPropsUpdates = {
- propsId: "id",
instanceId: "instanceId",
updates: [{ id: "propId", prop: "a", value: "1", type: "string" }],
};
- updateAllUserPropsMutable("treeId", propsMap, update);
+ updateAllUserPropsMutable(propsMap, update);
expect(propsMap).toMatchSnapshot();
});
test("with updated prop value", () => {
const propsMap: AllUserProps = {
- instanceId: {
- id: "id",
- instanceId: "instanceId",
- treeId: "treeId",
- props: [{ id: "propId", prop: "a", value: "1", type: "string" }],
- },
+ instanceId: [{ id: "propId", prop: "a", value: "1", type: "string" }],
};
const update: UserPropsUpdates = {
- propsId: "id",
instanceId: "instanceId",
updates: [{ id: "propId", prop: "a", value: "2", type: "string" }],
};
- updateAllUserPropsMutable("treeId", propsMap, update);
+ updateAllUserPropsMutable(propsMap, update);
expect(propsMap).toMatchSnapshot();
});
test("with renamed prop name", () => {
const propsMap: AllUserProps = {
- instanceId: {
- id: "id",
- instanceId: "instanceId",
- treeId: "treeId",
- props: [{ id: "propId", prop: "a", value: "1", type: "string" }],
- },
+ instanceId: [{ id: "propId", prop: "a", value: "1", type: "string" }],
};
const update: UserPropsUpdates = {
- propsId: "id",
instanceId: "instanceId",
updates: [{ id: "propId", prop: "b", value: "1", type: "string" }],
};
- updateAllUserPropsMutable("treeId", propsMap, update);
+ updateAllUserPropsMutable(propsMap, update);
expect(propsMap).toMatchSnapshot();
});
test("with changed value to asset type", () => {
const propsMap: AllUserProps = {
- instanceId: {
- id: "id",
- instanceId: "instanceId",
- treeId: "treeId",
- props: [{ id: "propId", prop: "a", value: "1", type: "string" }],
- },
+ instanceId: [{ id: "propId", prop: "a", value: "1", type: "string" }],
};
const update: UserPropsUpdates = {
- propsId: "id",
instanceId: "instanceId",
updates: [
{
@@ -100,25 +70,20 @@ describe("Update props", () => {
},
],
};
- updateAllUserPropsMutable("treeId", propsMap, update);
+ updateAllUserPropsMutable(propsMap, update);
expect(propsMap).toMatchInlineSnapshot(`
{
- "instanceId": {
- "id": "id",
- "instanceId": "instanceId",
- "props": [
- {
- "id": "propId",
- "prop": "a",
- "required": undefined,
- "type": "asset",
- "value": {
- "id": "assetId",
- },
+ "instanceId": [
+ {
+ "id": "propId",
+ "prop": "a",
+ "required": undefined,
+ "type": "asset",
+ "value": {
+ "id": "assetId",
},
- ],
- "treeId": "treeId",
- },
+ },
+ ],
}
`);
});
diff --git a/apps/designer/app/shared/props-utils/update-props.ts b/apps/designer/app/shared/props-utils/update-props.ts
index 2db47e51f9..b0cbc969b8 100644
--- a/apps/designer/app/shared/props-utils/update-props.ts
+++ b/apps/designer/app/shared/props-utils/update-props.ts
@@ -1,32 +1,23 @@
-import { v4 as uuid } from "uuid";
import {
type AllUserProps,
type UserPropsUpdates,
} from "@webstudio-is/react-sdk";
export const updateAllUserPropsMutable = (
- treeId: string,
allUserProps: AllUserProps,
- { instanceId, propsId, updates }: UserPropsUpdates
+ { instanceId, updates }: UserPropsUpdates
) => {
- if (instanceId in allUserProps === false) {
- allUserProps[instanceId] = {
- id: propsId ?? uuid(),
- instanceId,
- treeId,
- props: [],
- };
+ if (allUserProps[instanceId] === undefined) {
+ allUserProps[instanceId] = [];
}
const instanceProps = allUserProps[instanceId];
for (const update of updates) {
- const propIndex = instanceProps.props.findIndex(
- ({ id }) => id === update.id
- );
+ const propIndex = instanceProps.findIndex(({ id }) => id === update.id);
if (propIndex === -1) {
- instanceProps.props.push(update);
+ instanceProps.push(update);
} else {
- const prop = instanceProps.props[propIndex];
+ const prop = instanceProps[propIndex];
prop.prop = update.prop;
prop.type = update.type;
diff --git a/packages/project/src/db/props.ts b/packages/project/src/db/props.ts
index b0965a81fb..e79667a7e3 100644
--- a/packages/project/src/db/props.ts
+++ b/packages/project/src/db/props.ts
@@ -1,3 +1,4 @@
+import { v4 as uuid } from "uuid";
import { z } from "zod";
import warnOnce from "warn-once";
@@ -151,7 +152,7 @@ export const patch = async (
// model instead of the instanceId map.
// The map is handy for accessing props, but probably should be just cached interface, not the main data structure.
const allPropsMapByInstanceId = allProps.reduce((acc, prop) => {
- acc[prop.instanceId] = prop;
+ acc[prop.instanceId] = prop.props;
return acc;
}, {} as AllUserProps);
const nextProps = applyPatches<AllUserProps>(
@@ -160,7 +161,7 @@ export const patch = async (
);
await Promise.all(
- Object.values(nextProps).map(({ id, instanceId, treeId, props }) => {
+ Object.entries(nextProps).map(async ([instanceId, props]) => {
const propsDb: UserDbProp[] = UserProps.parse(props).map((prop) => {
if (prop.type === "asset") {
return {
@@ -174,13 +175,22 @@ export const patch = async (
const propsString = JSON.stringify(propsDb);
- return prisma.instanceProps.upsert({
- where: { id: id },
- create: { id: id, instanceId, treeId, props: propsString },
- update: {
+ const { count } = await prisma.instanceProps.updateMany({
+ where: { instanceId, treeId },
+ data: {
props: propsString,
},
});
+ if (count === 0) {
+ await prisma.instanceProps.create({
+ data: {
+ id: uuid(),
+ instanceId,
+ treeId,
+ props: propsString,
+ },
+ });
+ }
})
);
};
diff --git a/packages/project/src/shared/canvas-components/instance-data.ts b/packages/project/src/shared/canvas-components/instance-data.ts
index cf22164419..d1d7939604 100644
--- a/packages/project/src/shared/canvas-components/instance-data.ts
+++ b/packages/project/src/shared/canvas-components/instance-data.ts
@@ -1,4 +1,4 @@
-import type { Instance, InstanceProps } from "@webstudio-is/react-sdk";
+import type { Instance, UserProp } from "@webstudio-is/react-sdk";
import type {
Style,
StyleProperty,
@@ -10,7 +10,7 @@ export type SelectedInstanceData = {
id: Instance["id"];
component: Instance["component"];
browserStyle: Style;
- props?: InstanceProps;
+ props?: UserProp[];
};
export type HoveredInstanceData = {
diff --git a/packages/project/src/shared/props/create-instance-props.ts b/packages/project/src/shared/props/create-instance-props.ts
deleted file mode 100644
index 3ee4d722fe..0000000000
--- a/packages/project/src/shared/props/create-instance-props.ts
+++ /dev/null
@@ -1,17 +0,0 @@
-import type { InstanceProps, UserProp } from "@webstudio-is/react-sdk";
-import { v4 as uuid } from "uuid";
-
-export const createInstanceProps = ({
- treeId,
- instanceId,
- props,
-}: {
- treeId: string;
- instanceId: string;
- props?: Array<UserProp>;
-}): InstanceProps => ({
- id: uuid(),
- treeId,
- instanceId,
- props: props ?? [],
-});
diff --git a/packages/project/src/shared/props/index.ts b/packages/project/src/shared/props/index.ts
index 16c0a79c7c..8dfba76240 100644
--- a/packages/project/src/shared/props/index.ts
+++ b/packages/project/src/shared/props/index.ts
@@ -1,2 +1 @@
-export * from "./create-instance-props";
export * from "./clone-user-props";
diff --git a/packages/react-sdk/src/user-props/all-user-props.ts b/packages/react-sdk/src/user-props/all-user-props.ts
index ee33d40d80..9b714cd89d 100644
--- a/packages/react-sdk/src/user-props/all-user-props.ts
+++ b/packages/react-sdk/src/user-props/all-user-props.ts
@@ -2,8 +2,11 @@ import { useRef } from "react";
import { atom } from "nanostores";
import { useStore } from "@nanostores/react";
import type { InstanceProps, Instance } from "../db";
+import { UserProp } from "./schema";
-export type AllUserProps = { [id: Instance["id"]]: InstanceProps };
+export type AllUserProps = {
+ [id: Instance["id"]]: UserProp[];
+};
export const allUserPropsContainer = atom<AllUserProps>({});
@@ -12,8 +15,8 @@ export const useAllUserProps = (initialUserProps?: Array<InstanceProps>) => {
const ref = useRef(false);
if (ref.current === false && initialUserProps !== undefined) {
const propsMap: AllUserProps = {};
- for (const props of initialUserProps) {
- propsMap[props.instanceId] = props;
+ for (const item of initialUserProps) {
+ propsMap[item.instanceId] = item.props;
}
//We don't need to trigger rerender when setting the initial value
allUserPropsContainer.set(propsMap);
diff --git a/packages/react-sdk/src/user-props/types.ts b/packages/react-sdk/src/user-props/types.ts
index 6a6cf0b8a3..eae803c420 100644
--- a/packages/react-sdk/src/user-props/types.ts
+++ b/packages/react-sdk/src/user-props/types.ts
@@ -1,9 +1,8 @@
-import type { InstanceProps, Instance } from "../db";
+import type { Instance } from "../db";
import { UserProp } from "./schema";
export type UserPropsUpdates = {
instanceId: Instance["id"];
- propsId?: InstanceProps["id"];
updates: Array<UserProp>;
};
diff --git a/packages/react-sdk/src/user-props/use-user-props-asset.ts b/packages/react-sdk/src/user-props/use-user-props-asset.ts
index 205b8f430c..9a2e48deab 100644
--- a/packages/react-sdk/src/user-props/use-user-props-asset.ts
+++ b/packages/react-sdk/src/user-props/use-user-props-asset.ts
@@ -18,7 +18,7 @@ export const useUserPropsAsset = (
if (propsData == null) {
return undefined;
}
- const prop = propsData.props.find((prop) => prop.prop === propName);
+ const prop = propsData.find((prop) => prop.prop === propName);
if (prop == null) {
return undefined;
diff --git a/packages/react-sdk/src/user-props/use-user-props.ts b/packages/react-sdk/src/user-props/use-user-props.ts
index 60b5e262d3..02f4716b5b 100644
--- a/packages/react-sdk/src/user-props/use-user-props.ts
+++ b/packages/react-sdk/src/user-props/use-user-props.ts
@@ -19,7 +19,7 @@ export const useUserProps = (instanceId: Instance["id"]) => {
}
const result: UserProps = {};
- for (const userProp of propsData.props) {
+ for (const userProp of propsData) {
if (userProp.type !== "asset") {
result[userProp.prop] = userProp.value;
}