PromucFlow_constructor/app/client/src/widgets/withWidgetProps.tsx
Ashok Kumar M 0cbec4283c
chore: BaseWidget Restructuring (#26562)
## Description

Create a Basewidget wrapper that supplies Widget Onion as per the layout
system.

involves extracting widget layers presently in the BaseWidget into HOCs
and hooks and make sure layout systems can be scaled.
Make sure Modal widget is handled as a overlay widget whose wrappers are
supplied by basewidget instead of modal widget implementing its own
editing blocks.
This PR also separates the drag n drop logic for both auto layout and
fixed layout. They are moved into respective Layout system folders to
have clear sepsration of concern

#### PR fixes following issue(s)
Fixes #26674
Fixes #26675
Fixes #26676
Fixes #26570
Fixes #26590 
Fixes #26591 
Fixes #26592 
<img width="931" alt="BaseWidgetHOC"
src="https://github.com/appsmithorg/appsmith/assets/35134347/22f4cf1e-e4c5-4475-83a8-6818e7cebe70">

[Miro Link to view the new
system](https://miro.com/app/board/uXjVM6vRgf8=/?moveToWidget=3458764560239189204&cot=14)


> if no issue exists, please create an issue and ask the maintainers
about this first
>
>
#### Media
> A video or a GIF is preferred. when using Loom, don’t embed because it
looks like it’s a GIF. instead, just link to the video
>
>
#### Type of change
> Please delete options that are not relevant.
- Breaking change (fix or feature that would cause existing
functionality to not work as expected)
- Chore (housekeeping or task changes that don't impact user perception)
>
>
>
## Testing
>
#### How Has This Been Tested?
> Please describe the tests that you ran to verify your changes. Also
list any relevant details for your test configuration.
> Delete anything that is not relevant
- [x] Manual
- [ ] JUnit
- [ ] Jest
- [ ] Cypress
>
>
#### Test Plan
> Add Testsmith test cases links that relate to this PR
>
>
#### Issues raised during DP testing
> Link issues raised during DP testing for better visiblity and tracking
(copy link from comments dropped on this PR)
>
>
>
## Checklist:
#### Dev activity
- [ ] My code follows the style guidelines of this project
- [ ] I have performed a self-review of my own code
- [ ] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation
- [ ] My changes generate no new warnings
- [ ] I have added tests that prove my fix is effective or that my
feature works
- [ ] New and existing unit tests pass locally with my changes
- [ ] PR is being merged under a feature flag


#### QA activity:
- [ ] [Speedbreak
features](https://github.com/appsmithorg/TestSmith/wiki/Guidelines-for-test-plans#speedbreakers-)
have been covered
- [ ] Test plan covers all impacted features and [areas of
interest](https://github.com/appsmithorg/TestSmith/wiki/Guidelines-for-test-plans#areas-of-interest-)
- [ ] Test plan has been peer reviewed by project stakeholders and other
QA members
- [ ] Manually tested functionality on DP
- [ ] We had an implementation alignment call with stakeholders post QA
Round 2
- [ ] Cypress test cases have been added and approved by SDET/manual QA
- [ ] Added `Test Plan Approved` label after Cypress tests were reviewed
- [ ] Added `Test Plan Approved` label after JUnit tests were reviewed

---------

Co-authored-by: rahulramesha <rahul@appsmith.com>
Co-authored-by: rahulramesha <71900764+rahulramesha@users.noreply.github.com>
Co-authored-by: Preet Sidhu <preetsidhu.bits@gmail.com>
Co-authored-by: Aswath K <aswath.sana@gmail.com>
2023-09-11 21:25:11 +05:30

328 lines
11 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,
getCurrentAppPositioningType,
getMainCanvasProps,
getRenderMode,
getMetaWidgetChildrenStructure,
getMetaWidget,
getFlattenedChildCanvasWidgets,
previewModeSelector,
getIsAutoLayoutMobileBreakPoint,
getCanvasWidth,
} from "selectors/editorSelectors";
import {
createCanvasWidget,
createLoadingWidget,
} from "utils/widgetRenderUtils";
import type { WidgetProps } from "./BaseWidget";
import type BaseWidget from "./BaseWidget";
import type { WidgetEntityConfig } from "entities/DataTree/dataTreeFactory";
import { AppPositioningTypes } from "reducers/entityReducers/pageListReducer";
import {
defaultAutoLayoutWidgets,
Positioning,
} from "layoutSystems/autolayout/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";
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(previewModeSelector);
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 isMobile = useSelector(getIsAutoLayoutMobileBreakPoint);
const appPositioningType = useSelector(getCurrentAppPositioningType);
const isAutoLayout = appPositioningType === AppPositioningTypes.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.appPositioningType = appPositioningType;
widgetProps.selectedWidgetAncestry = selectedWidgetAncestry || [];
/**
* 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;
}
//merging with original props
widgetProps = { ...props, ...widgetProps, renderMode };
// 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;
// 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;