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.
TypeScript accessibilityalternativecloudflarecloudflare-workersdesignfreefrontendopen-sourceradix-uireactremixweb-developmentwebflowwebsite-builderwebsite-developmentwebsiteswebstudio
refactor: Simplify use-props-logic
Andrey Okonetchnikov3ddef62551ab
diff --git a/app/designer/features/inspector/inspector.tsx b/app/designer/features/inspector/inspector.tsxindex 16dec44c92..4d374c8ecb 100644--- a/app/designer/features/inspector/inspector.tsx+++ b/app/designer/features/inspector/inspector.tsx@@ -55,6 +55,7 @@ export const Inspector = ({ publish }: InspectorProps) => {<TabsContent value="props" css={contentStyle}><PropsPanelpublish={publish}+ key={selectedInstanceData.id /* Re-render when instance changes */}selectedInstanceData={selectedInstanceData}/></TabsContent>diff --git a/app/designer/features/props-panel/props-panel.tsx b/app/designer/features/props-panel/props-panel.tsxindex c37e8db7c6..15134af16f 100644--- a/app/designer/features/props-panel/props-panel.tsx+++ b/app/designer/features/props-panel/props-panel.tsx@@ -223,7 +223,7 @@ const Property = ({type PropsPanelProps = {publish: Publish;- selectedInstanceData?: SelectedInstanceData;+ selectedInstanceData: SelectedInstanceData;};export const PropsPanel = ({@@ -233,8 +233,6 @@ export const PropsPanel = ({const { userProps, addEmptyProp, handleChangeProp, handleDeleteProp } =usePropsLogic({ selectedInstanceData, publish });- if (selectedInstanceData === undefined) return null;-const addButton = (<Buttonghostdiff --git a/app/designer/features/props-panel/use-props-logic.ts b/app/designer/features/props-panel/use-props-logic.tsindex 5fe80661fa..0f1aff0fa4 100644--- a/app/designer/features/props-panel/use-props-logic.ts+++ b/app/designer/features/props-panel/use-props-logic.ts@@ -1,13 +1,13 @@-import produce from "immer";-import { useState, useCallback, useEffect, useRef } from "react";-import debounce from "lodash.debounce";-import ObjectId from "bson-objectid";import type {DeleteProp,+ Publish,UserProp,UserPropsUpdates,- Publish,} from "@webstudio-is/sdk";+import ObjectId from "bson-objectid";+import produce from "immer";+import debounce from "lodash.debounce";+import { useRef, useState } from "react";import type { SelectedInstanceData } from "~/shared/canvas-components";const getInitialProp = () => ({@@ -20,56 +20,40 @@ const initialUserProps = [getInitialProp()];type UsePropsLogic = {publish: Publish;- selectedInstanceData?: SelectedInstanceData;+ selectedInstanceData: SelectedInstanceData;};export const usePropsLogic = ({selectedInstanceData,publish,}: UsePropsLogic) => {- const [userProps, setUserProps] = useState<Array<UserProp>>(initialUserProps);+ const props =+ selectedInstanceData.props === undefined ||+ selectedInstanceData.props.props.length === 0+ ? initialUserProps+ : selectedInstanceData.props.props;+ const [userProps, setUserProps] = useState<Array<UserProp>>(props);const propsToPublishRef = useRef<{[id: UserProp["id"]]: true;}>({});- useEffect(() => {- if (selectedInstanceData === undefined) {- return;- }- const props =- selectedInstanceData.props === undefined ||- selectedInstanceData.props.props.length === 0- ? initialUserProps- : selectedInstanceData.props.props;- setUserProps(props);- }, [selectedInstanceData]);-// @todo this may call the last callback after unmount- // use useDebounce- // eslint-disable-next-line react-hooks/exhaustive-deps- const updateProps = useCallback(- debounce((updates: UserPropsUpdates["updates"]) => {- if (selectedInstanceData === undefined) {- return;- }- publish<"updateProps", UserPropsUpdates>({- type: "updateProps",- payload: {- treeId: selectedInstanceData.props.treeId,- propsId: selectedInstanceData.props.id,- instanceId: selectedInstanceData.id,- updates,- },- });- for (const update of updates) {- delete propsToPublishRef.current[update.id];- }- }, 1000),- [selectedInstanceData?.id]- );+ const updateProps = debounce((updates: UserPropsUpdates["updates"]) => {+ publish<"updateProps", UserPropsUpdates>({+ type: "updateProps",+ payload: {+ treeId: selectedInstanceData.props.treeId,+ propsId: selectedInstanceData.props.id,+ instanceId: selectedInstanceData.id,+ updates,+ },+ });+ for (const update of updates) {+ delete propsToPublishRef.current[update.id];+ }+ }, 1000);const deleteProp = (id: UserProp["id"]) => {- if (selectedInstanceData === undefined) return;publish<"deleteProp", DeleteProp>({type: "deleteProp",payload: {