Skip to content

Commit

Permalink
fix: optimize block variables and lexical state updates (#3689)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavel910 committed Nov 10, 2023
1 parent a78f96a commit 1f3e8a1
Show file tree
Hide file tree
Showing 32 changed files with 191 additions and 132 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const VariableSettings = ({ element }: { element: PbEditorElement }) => {
(variable: PbBlockVariable) => variable.id.split(".")[0] === element?.data?.variableId
);

return variables;
return variables ?? [];
}, [block, element]);

const onChange = useCallback(
Expand Down Expand Up @@ -67,7 +67,8 @@ const VariableSettings = ({ element }: { element: PbEditorElement }) => {
() =>
showConfirmation(() => {
if (block && block.id) {
const updatedVariables = block.data.variables.filter(
const variables = block.data.variables ?? [];
const updatedVariables = variables.filter(
(variable: PbBlockVariable) =>
variable.id.split(".")[0] !== element?.data?.variableId
);
Expand Down Expand Up @@ -99,7 +100,7 @@ const VariableSettings = ({ element }: { element: PbEditorElement }) => {
return (
<>
<FormWrapper>
{elementVariables?.map((variable: PbBlockVariable, index: string) => (
{elementVariables.map((variable, index) => (
<TextInput
key={index}
label={`${capitalize(variable.type)} ${capitalize(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@ const VariablesList = ({ block }: { block: PbEditorElement }) => {
showConfirmation(async () => {
if (block && block.id) {
// remove variable from block
const updatedVariables = block.data.variables.filter(
const variables = block.data.variables ?? [];
const updatedVariables = variables.filter(
(variable: PbBlockVariable) => variable.id !== variableId
);
updateElement({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ interface UseMoveVariable {
export const useMoveVariable = (element: PbEditorElement): UseMoveVariable => {
const updateElement = useUpdateElement();
const move = (current: number, next: number) => {
const reorderedVariables = moveInPlace(element?.data?.variables, current, next);
const reorderedVariables = moveInPlace(element?.data?.variables ?? [], current, next);

updateElement({
...element,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export function useElementVariables(element: PbEditorElement | null) {
}
}, [block, element]);

return variableValue;
return variableValue ?? [];
}

export function useElementVariableValue(element: PbEditorElement | null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import { useUpdateElement } from "~/editor/hooks/useUpdateElement";
import { useParentBlock } from "~/editor/hooks/useParentBlock";

const removeVariableFromBlock = (block: PbEditorElement, variableId: string) => {
const updatedVariables = block.data.variables.filter(
const variables = block.data.variables ?? [];

const updatedVariables = variables.filter(
(variable: PbBlockVariable) => variable.id.split(".")[0] !== variableId
);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from "react";
import React, { useCallback, useMemo } from "react";
import { css } from "emotion";
import { plugins } from "@webiny/plugins";
import { useActiveElement } from "~/editor/hooks/useActiveElement";
Expand All @@ -21,21 +21,41 @@ const labelStyle = css({
const VariableSettings: React.FC = () => {
const [element] = useActiveElement();

const elementVariableRendererPlugins =
plugins.byType<PbEditorPageElementVariableRendererPlugin>(
const variableRenderers = useMemo(() => {
return plugins.byType<PbEditorPageElementVariableRendererPlugin>(
"pb-editor-page-element-variable-renderer"
);
}, []);

const renderVariableInput = useCallback(
(variable: PbBlockVariable) => {
const renderer = variableRenderers.find(plugin => plugin.elementType === variable.type);
if (!renderer) {
return null;
}
return renderer.renderVariableInput(variable.id);
},
[variableRenderers]
);

if (!element) {
return null;
}

const variables = element.data.variables;

if (!variables) {
return null;
}

return (
<div className={wrapperStyle}>
{element?.data?.variables?.map((variable: PbBlockVariable, index: number) => (
{variables.map((variable, index) => (
<div key={index}>
<div className={labelStyle}>
<Typography use={"body2"}>{variable.label}</Typography>
</div>
{elementVariableRendererPlugins
.find(plugin => plugin.elementType === variable?.type)
?.renderVariableInput(variable.id)}
{renderVariableInput(variable)}
</div>
))}
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export default {
elementType: "heading",
getVariableValue(element) {
const variables = useElementVariables(element);
return variables?.length > 0 ? variables[0].value : null;
return variables.length > 0 ? variables[0].value : null;
},
renderVariableInput(variableId: string) {
return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default {
elementType: "icon",
getVariableValue(element) {
const variables = useElementVariables(element);
return variables?.length > 0 ? variables[0].value : null;
return variables.length > 0 ? variables[0].value : null;
},
renderVariableInput(variableId: string) {
return <IconVariableInput variableId={variableId} />;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default {
elementType: "image",
getVariableValue(element) {
const variables = useElementVariables(element);
return variables?.length > 0 ? variables[0].value : null;
return variables.length > 0 ? variables[0].value : null;
},
renderVariableInput(variableId: string) {
return <SingleImageVariableInput variableId={variableId} />;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default {
elementType: "images-list",
getVariableValue(element) {
const variables = useElementVariables(element);
return variables?.length > 0 ? variables[0].value : null;
return variables.length > 0 ? variables[0].value : null;
},
renderVariableInput(variableId: string) {
return <MultipleImageVariableInput variableId={variableId} />;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default {
elementType: "list",
getVariableValue(element) {
const variables = useElementVariables(element);
return variables?.length > 0 ? variables[0].value : null;
return variables.length > 0 ? variables[0].value : null;
},
renderVariableInput(variableId: string) {
return <RichVariableInput variableId={variableId} />;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ export default {
elementType: "paragraph",
getVariableValue(element) {
const variables = useElementVariables(element);
return variables?.length > 0 ? variables[0].value : null;
if (!variables) {
return null;
}

return variables.length > 0 ? variables[0].value : null;
},
renderVariableInput(variableId: string) {
return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default {
elementType: "quote",
getVariableValue(element) {
const variables = useElementVariables(element);
return variables?.length > 0 ? variables[0].value : null;
return variables.length > 0 ? variables[0].value : null;
},
renderVariableInput(variableId: string) {
return <RichVariableInput variableId={variableId} />;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default {
elementType: "codesandbox",
getVariableValue(element) {
const variables = useElementVariables(element);
return variables?.length > 0 ? variables[0].value : null;
return variables.length > 0 ? variables[0].value : null;
},
renderVariableInput(variableId: string) {
return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default {
elementType: "iframe",
getVariableValue(element) {
const variables = useElementVariables(element);
return variables?.length > 0 ? variables[0].value : null;
return variables.length > 0 ? variables[0].value : null;
},
renderVariableInput(variableId: string) {
return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default {
elementType: "soundcloud",
getVariableValue(element) {
const variables = useElementVariables(element);
return variables?.length > 0 ? variables[0].value : null;
return variables.length > 0 ? variables[0].value : null;
},
renderVariableInput(variableId: string) {
return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default {
elementType: "vimeo",
getVariableValue(element) {
const variables = useElementVariables(element);
return variables?.length > 0 ? variables[0].value : null;
return variables.length > 0 ? variables[0].value : null;
},
renderVariableInput(variableId: string) {
return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default {
elementType: "youtube",
getVariableValue(element) {
const variables = useElementVariables(element);
return variables?.length > 0 ? variables[0].value : null;
return variables.length > 0 ? variables[0].value : null;
},
renderVariableInput(variableId: string) {
return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default {
elementType: "pinterest",
getVariableValue(element) {
const variables = useElementVariables(element);
return variables?.length > 0 ? variables[0].value : null;
return variables.length > 0 ? variables[0].value : null;
},
renderVariableInput(variableId: string) {
return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default {
elementType: "twitter",
getVariableValue(element) {
const variables = useElementVariables(element);
return variables?.length > 0 ? variables[0].value : null;
return variables.length > 0 ? variables[0].value : null;
},
renderVariableInput(variableId: string) {
return (
Expand Down
59 changes: 34 additions & 25 deletions packages/app-page-builder/src/hooks/useVariable.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,53 @@
import { useCallback } from "react";
import { useCallback, useEffect, useRef } from "react";
import { useUpdateElement } from "~/editor/hooks/useUpdateElement";
import { useActiveElement } from "~/editor/hooks/useActiveElement";
import { PbBlockVariable } from "~/types";

export function useVariable<TValue = any>(variableId: string) {
const [element] = useActiveElement();
const elementRef = useRef(element);
const updateElement = useUpdateElement();
const variable = element?.data?.variables?.find(
(variable: PbBlockVariable) => variable.id === variableId
);

// We're storing a reference to an element, so we don't have to recreate the `onChange` callback on every update.
useEffect(() => {
elementRef.current = element;
}, [element]);

const onChange = useCallback(
(value: TValue, history = false) => {
if (element) {
const newVariables = element?.data?.variables?.map((variable: PbBlockVariable) => {
if (variable?.id === variableId) {
return {
...variable,
value
};
}
const element = elementRef.current;
if (!element) {
return;
}

return variable;
});

updateElement(
{
...element,
data: {
...element.data,
variables: newVariables
}
},
{
history
const newVariables = element.data.variables?.map(variable => {
if (variable?.id === variableId) {
return {
...variable,
value
};
}

return variable;
});

updateElement(
{
...element,
data: {
...element.data,
variables: newVariables
}
);
}
},
{
history
}
);
},
[element, variableId, updateElement]
[variableId, updateElement]
);

const onBlur = useCallback(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@ const syncTemplateVariables = (content: PbElement) => {
const templateVariables = [];

for (const block of content.elements) {
if (block.data.variables?.length > 0) {
const variables = block.data.variables ?? [];
if (variables.length > 0) {
templateVariables.push({
blockId: block.data.templateBlockId,
variables: block.data.variables
variables: variables
});
}
}
Expand All @@ -50,7 +51,7 @@ const removeTemplateBlockIds = (content: PbElement) => {
// we need to drop templateBlockId property, when block is no longer inside template
// we also remove variables from unlinked blocks, since they don't need them outside template
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { templateBlockId, variables, ...blockData } = block.data;
const { templateBlockId, variables = [], ...blockData } = block.data;
if (block.data.blockId) {
return {
...block,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const UnlinkBlockAction: React.FC<UnlinkBlockActionPropsType> = ({ children }) =
if (element) {
// we need to drop blockId and variables properties when unlinking, so they are separated from all other element data
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { blockId, variables, ...newData } = element.data;
const { blockId, variables = [], ...newData } = element.data;
const pbElement = (await getElementTree({
element: { ...element, data: newData }
})) as PbElement;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const UnlinkBlockAction: React.FC<UnlinkBlockActionPropsType> = ({ children }) =
if (element) {
// we need to drop blockId and variables properties when unlinking, so they are separated from all other element data
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { blockId, variables, ...newData } = element.data;
const { blockId, variables = [], ...newData } = element.data;
const pbElement = (await getElementTree({
element: { ...element, data: newData }
})) as PbElement;
Expand Down
1 change: 1 addition & 0 deletions packages/app-page-builder/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ export interface PbElementDataTypeSource {

export type PbElementDataType = {
blockId?: string;
variables?: PbBlockVariable[];
action?: {
href: string;
newTab: boolean;
Expand Down
Loading

0 comments on commit 1f3e8a1

Please sign in to comment.