PromucFlow_constructor/app/client/src/widgets/withWidgetProps.tsx
Ashok Kumar M 7b527c9024
chore: Merge wds and anvil feature flags (#32609)
[![workerB](https://img.shields.io/endpoint?url=https%3A%2F%2Fworkerb.linearb.io%2Fv2%2Fbadge%2Fprivate%2FU2FsdGVkX1LNwrMHgs05enX0VDk8QxZH7uP7Ii4HE%2Fcollaboration.svg%3FcacheSeconds%3D60)](https://workerb.linearb.io/v2/badge/collaboration-page?magicLinkId=M7zehz4)
## Description

Cleaning up three patterns of checks to enable wds and anvil into two.
wds and anvil had to have different flags coz anvil had to play catch up
with wds, now that's not the case so it does not make sense to have two
flags.

Old patterns
- checking if the wds feature flag is enabled
- checking if the anvil feature flag is enabled
- checking if the layout system of the app is anvil

New Pattern
- checking if anvil feature flag is enabled (used only for creating an
anvil app)
- checking if layout system of the app is anvil

Fixes #32590
_or_  
Fixes `Issue URL`
> [!WARNING]  
> _If no issue exists, please create an issue first, and check with the
maintainers if the issue is valid._

## Automation

/ok-to-test tags="@tag.All"

### 🔍 Cypress test results
<!-- This is an auto-generated comment: Cypress test results  -->
> [!TIP]
> 🟢 🟢 🟢 All cypress tests have passed! 🎉 🎉 🎉
> Workflow run:
<https://github.com/appsmithorg/appsmith/actions/runs/8663918496>
> Commit: e10cc2a84ed680b29c49c5b2e8175df4c18da2f8
> Cypress dashboard url: <a
href="https://internal.appsmith.com/app/cypress-dashboard/rundetails-65890b3c81d7400d08fa9ee5?branch=master&workflowId=8663918496&attempt=1"
target="_blank">Click here!</a>

<!-- end of auto-generated comment: Cypress test results  -->

















<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

- **Refactor**
- Consolidated the usage of layout system checks across the application
to use a unified Anvil layout selector, enhancing consistency in
layout-related conditional logic.
- **Bug Fixes**
- Removed outdated feature flags related to the Anvil + WDS integration,
ensuring the application's feature toggling aligns with the current
development strategy.
- **Tests**
- Updated unit tests to align with the new method of layout system
determination, ensuring test environments accurately reflect production
behavior.
- **Chores**
- Cleaned up redundant code and feature flags that are no longer in use,
simplifying the codebase and reducing potential for errors.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2024-04-12 22:54:04 +05:30

362 lines
13 KiB
TypeScript

import equal from "fast-deep-equal/es6";
import React from "react";
import { ReduxActionTypes } from "@appsmith/constants/ReduxActionConstants";
import type { AppState } from "@appsmith/reducers";
import { checkContainersForAutoHeightAction } from "actions/autoHeightActions";
import {
GridDefaults,
MAIN_CONTAINER_WIDGET_ID,
RenderModes,
WIDGET_PADDING,
} from "constants/WidgetConstants";
import { useDispatch, useSelector } from "react-redux";
import { getWidget } from "sagas/selectors";
import {
getIsWidgetLoading,
getWidgetEvalValues,
} from "selectors/dataTreeSelectors";
import {
computeMainContainerWidget,
getChildWidgets,
getMainCanvasProps,
getRenderMode,
getMetaWidgetChildrenStructure,
getMetaWidget,
getIsAutoLayoutMobileBreakPoint,
getCanvasWidth,
combinedPreviewModeSelector,
} from "selectors/editorSelectors";
import {
createCanvasWidget,
createLoadingWidget,
widgetErrorsFromStaticProps,
} from "utils/widgetRenderUtils";
import type { WidgetProps } from "./BaseWidget";
import type BaseWidget from "./BaseWidget";
import type { WidgetEntityConfig } from "@appsmith/entities/DataTree/types";
import { Positioning } from "layoutSystems/common/utils/constants";
import { isAutoHeightEnabledForWidget } from "./WidgetUtils";
import { CANVAS_DEFAULT_MIN_HEIGHT_PX } from "constants/AppConstants";
import { getGoogleMapsApiKey } from "@appsmith/selectors/tenantSelectors";
import ConfigTreeActions from "utils/configTree";
import { getSelectedWidgetAncestry } from "../selectors/widgetSelectors";
import { getWidgetMinMaxDimensionsInPixel } from "layoutSystems/autolayout/utils/flexWidgetUtils";
import { defaultAutoLayoutWidgets } from "layoutSystems/autolayout/utils/constants";
import { getFlattenedChildCanvasWidgets } from "selectors/flattenedChildCanvasSelector";
import { LayoutSystemTypes } from "layoutSystems/types";
import { getLayoutSystemType } from "selectors/layoutSystemSelectors";
import { isWidgetSelectedForPropertyPane } from "selectors/propertyPaneSelectors";
import WidgetFactory from "WidgetProvider/factory";
import { getIsAnvilLayout } from "layoutSystems/anvil/integrations/selectors";
const WIDGETS_WITH_CHILD_WIDGETS = ["LIST_WIDGET", "FORM_WIDGET"];
const WIDGETS_REQUIRING_SELECTED_ANCESTRY = ["MODAL_WIDGET", "TABS_WIDGET"];
function withWidgetProps(WrappedWidget: typeof BaseWidget) {
function WrappedPropsComponent(
props: WidgetProps & { skipWidgetPropsHydration?: boolean },
) {
const {
children,
hasMetaWidgets,
referencedWidgetId,
requiresFlatWidgetChildren,
skipWidgetPropsHydration,
type,
widgetId,
} = props;
const isPreviewMode = useSelector(combinedPreviewModeSelector);
const canvasWidget = useSelector((state: AppState) =>
getWidget(state, widgetId),
);
const mainCanvasWidth = useSelector(getCanvasWidth);
const metaWidget = useSelector(getMetaWidget(widgetId));
const mainCanvasProps = useSelector((state: AppState) =>
getMainCanvasProps(state),
);
const googleMapsApiKey = useSelector(getGoogleMapsApiKey);
const renderMode = useSelector(getRenderMode);
const widgetName = canvasWidget?.widgetName || metaWidget?.widgetName;
const evaluatedWidget = useSelector((state: AppState) =>
getWidgetEvalValues(state, widgetName),
);
const isLoading = useSelector((state: AppState) =>
getIsWidgetLoading(state, widgetName),
);
const metaWidgetChildrenStructure = useSelector(
getMetaWidgetChildrenStructure(widgetId, type, hasMetaWidgets),
equal,
);
const isWidgetSelected = useSelector((state: AppState) =>
isWidgetSelectedForPropertyPane(state, widgetId),
);
const isMobile = useSelector(getIsAutoLayoutMobileBreakPoint);
const layoutSystemType = useSelector(getLayoutSystemType);
const isAnvilLayout = useSelector(getIsAnvilLayout);
const isAutoLayout = layoutSystemType === LayoutSystemTypes.AUTO;
const configTree = ConfigTreeActions.getConfigTree();
const evaluatedWidgetConfig = configTree[
canvasWidget?.widgetName
] as WidgetEntityConfig;
const dispatch = useDispatch();
const childWidgets = useSelector((state: AppState) => {
if (!WIDGETS_WITH_CHILD_WIDGETS.includes(type)) return undefined;
return getChildWidgets(state, widgetId);
}, equal);
const flattenedChildCanvasWidgets = useSelector((state: AppState) => {
if (requiresFlatWidgetChildren) {
return getFlattenedChildCanvasWidgets(
state,
referencedWidgetId || widgetId,
);
}
}, equal);
const selectedWidgetAncestry: string[] = useSelector((state: AppState) => {
if (!WIDGETS_REQUIRING_SELECTED_ANCESTRY.includes(type)) {
return [];
}
return getSelectedWidgetAncestry(state);
}, equal);
let widgetProps: WidgetProps = {} as WidgetProps;
const widget = metaWidget || canvasWidget;
if (!skipWidgetPropsHydration) {
const canvasWidgetProps = (() => {
if (widgetId === MAIN_CONTAINER_WIDGET_ID) {
const computed = computeMainContainerWidget(
canvasWidget,
mainCanvasProps,
);
if (renderMode === RenderModes.CANVAS) {
return {
...computed,
bottomRow: Math.max(
computed.minHeight,
computed.bottomRow +
GridDefaults.MAIN_CANVAS_EXTENSION_OFFSET *
GridDefaults.DEFAULT_GRID_ROW_HEIGHT,
),
};
} else {
return {
...computed,
bottomRow: Math.max(
CANVAS_DEFAULT_MIN_HEIGHT_PX,
computed.bottomRow +
GridDefaults.VIEW_MODE_MAIN_CANVAS_EXTENSION_OFFSET *
GridDefaults.DEFAULT_GRID_ROW_HEIGHT,
),
};
}
}
return evaluatedWidget
? createCanvasWidget(widget, evaluatedWidget, evaluatedWidgetConfig)
: createLoadingWidget(widget);
})();
widgetProps = { ...canvasWidgetProps };
widgetProps.isMobile = !!isMobile;
widgetProps.selectedWidgetAncestry = selectedWidgetAncestry || [];
widgetProps.isWidgetSelected = isWidgetSelected;
/**
* MODAL_WIDGET by default is to be hidden unless the isVisible property is found.
* If the isVisible property is undefined and the widget is MODAL_WIDGET then isVisible
* is set to false
* If the isVisible property is undefined and the widget is not MODAL_WIDGET then isVisible
* is set to true
*/
widgetProps.isVisible =
canvasWidgetProps.isVisible ??
canvasWidgetProps.type !== "MODAL_WIDGET";
if (
props.type === "CANVAS_WIDGET" &&
widgetId !== MAIN_CONTAINER_WIDGET_ID
) {
const isListWidgetCanvas =
props.noPad && props.dropDisabled && props.openParentPropertyPane;
widgetProps.rightColumn = props.rightColumn;
if (isListWidgetCanvas) {
widgetProps.bottomRow = props.bottomRow;
widgetProps.minHeight = props.minHeight;
}
widgetProps.shouldScrollContents = props.shouldScrollContents;
widgetProps.canExtend = props.canExtend;
widgetProps.parentId = props.parentId;
} else if (widgetId !== MAIN_CONTAINER_WIDGET_ID) {
widgetProps.parentColumnSpace = props.parentColumnSpace;
widgetProps.parentRowSpace = props.parentRowSpace;
widgetProps.parentId = props.parentId;
// Form Widget Props
widgetProps.onReset = props.onReset;
if ("isFormValid" in props) widgetProps.isFormValid = props.isFormValid;
}
if (defaultAutoLayoutWidgets.includes(props.type)) {
widgetProps.positioning = isAutoLayout
? Positioning.Vertical
: Positioning.Fixed;
}
widgetProps.children = children;
widgetProps.metaWidgetChildrenStructure = metaWidgetChildrenStructure;
widgetProps.isLoading = isLoading;
widgetProps.childWidgets = childWidgets;
widgetProps.flattenedChildCanvasWidgets = flattenedChildCanvasWidgets;
/*
* In Editor, Widgets can ask for error info to be passed to them
* so they can show them on the UI
*/
const needsErrorInfo =
!isPreviewMode &&
renderMode === RenderModes.CANVAS &&
!!WidgetFactory.getConfig(evaluatedWidget?.type)?.needsErrorInfo;
widgetProps.errors = needsErrorInfo
? widgetErrorsFromStaticProps(evaluatedWidget)
: [];
}
//merging with original props
widgetProps = {
...props,
...widgetProps,
layoutSystemType,
renderMode,
isPreviewMode,
};
// adding google maps api key to widget props (although meant for map widget only)
widgetProps.googleMapsApiKey = googleMapsApiKey;
// isVisible prop defines whether to render a detached widget
if (
widgetProps.detachFromLayout &&
!widgetProps.isVisible &&
!selectedWidgetAncestry.includes(widgetProps.widgetId)
) {
return null;
}
const shouldCollapseWidgetInViewOrPreviewMode =
!widgetProps.isVisible &&
!selectedWidgetAncestry.includes(widgetProps.widgetId) &&
(renderMode === RenderModes.PAGE || isPreviewMode);
const shouldResetCollapsedContainerHeightInViewOrPreviewMode =
widgetProps.isVisible && widgetProps.topRow === widgetProps.bottomRow;
const shouldResetCollapsedContainerHeightInCanvasMode =
widgetProps.topRow === widgetProps.bottomRow &&
renderMode === RenderModes.CANVAS &&
!isPreviewMode;
widgetProps.mainCanvasWidth = mainCanvasWidth;
if (isAnvilLayout) {
if (shouldCollapseWidgetInViewOrPreviewMode) {
return null;
}
} else {
// We don't render invisible widgets in view mode
if (shouldCollapseWidgetInViewOrPreviewMode) {
// This flag (isMetaWidget) is used to prevent the Auto height saga from updating
// the List widget Child Widgets. Auto height is disabled in the List widget and
// this flag serves as a way to avoid any unintended changes to the child widget's height.
if (
widgetProps.bottomRow !== widgetProps.topRow &&
!widgetProps.isMetaWidget
) {
dispatch({
type: ReduxActionTypes.UPDATE_WIDGET_AUTO_HEIGHT,
payload: {
widgetId: props.widgetId,
height: 0,
},
});
}
return null;
} else if (
shouldResetCollapsedContainerHeightInViewOrPreviewMode ||
shouldResetCollapsedContainerHeightInCanvasMode
) {
// We also need to check if a non-auto height widget has collapsed earlier
// We can figure this out if the widget height is zero and the beforeCollapse
// topRow and bottomRow are available.
// If the above is true, we call an auto height update call
// so that the widget can be reset correctly.
if (
widgetProps.topRow === widgetProps.bottomRow &&
widgetProps.topRowBeforeCollapse !== undefined &&
widgetProps.bottomRowBeforeCollapse !== undefined &&
!isAutoHeightEnabledForWidget(widgetProps)
) {
const heightBeforeCollapse =
(widgetProps.bottomRowBeforeCollapse -
widgetProps.topRowBeforeCollapse) *
GridDefaults.DEFAULT_GRID_ROW_HEIGHT;
dispatch({
type: ReduxActionTypes.UPDATE_WIDGET_AUTO_HEIGHT,
payload: {
widgetId: props.widgetId,
height: heightBeforeCollapse,
},
});
} else {
dispatch(checkContainersForAutoHeightAction());
}
}
// Sets the min/max height/width of the widget
if (isAutoLayout) {
const minMaxDimensions = getWidgetMinMaxDimensionsInPixel(
widgetProps,
mainCanvasWidth,
);
widgetProps = {
...widgetProps,
minWidth: minMaxDimensions.minWidth
? minMaxDimensions.minWidth - 2 * WIDGET_PADDING
: undefined,
minHeight: minMaxDimensions.minHeight
? minMaxDimensions.minHeight - 2 * WIDGET_PADDING
: undefined,
maxWidth: minMaxDimensions.maxWidth
? minMaxDimensions.maxWidth - 2 * WIDGET_PADDING
: undefined,
maxHeight: minMaxDimensions.maxHeight
? minMaxDimensions.maxHeight - 2 * WIDGET_PADDING
: undefined,
};
}
}
return <WrappedWidget {...widgetProps} />;
}
return WrappedPropsComponent;
}
export default withWidgetProps;