PromucFlow_constructor/app/client/src/selectors/widgetSelectors.ts
Vemparala Surya Vamsi e5b2a26c65
chore: ce changes related to decoupling webworker (#41033)
## Description
We are improving the LCP by reducing the time to reach the first
evaluation, aiming for a 1.8 to 2.2 second reduction. To achieve this,
we’ve implemented the following changes:

Code Splitting of Widgets: During page load, only the widgets required
for an evaluation are loaded and registered. For every evaluation cycle
we keep discovering widget types and load them as required.

Web Worker Offloading: Macro tasks such as clearCache and JavaScript
library installation have been moved to the web worker setup. These are
now executed in a separate thread, allowing the firstUnevaluatedTree to
be computed in parallel with JS library installation.

Parallel JS Library Loading: All JavaScript libraries are now loaded in
parallel within the web worker, instead of sequentially, improving
efficiency.

Deferred Rendering of AppViewer: We now render the AppViewer and Header
component only after registering the remaining widgets. This ensures
that heavy rendering tasks—such as expensive selector computations and
loading additional chunks related to the AppViewer—can execute in
parallel with the first evaluation, further enhancing performance.

## 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/16202622510>
> Commit: b648036bd7b74ae742f5c5d7f6cfd770867a2828
> <a
href="https://internal.appsmith.com/app/cypress-dashboard/rundetails-65890b3c81d7400d08fa9ee5?branch=master&workflowId=16202622510&attempt=1"
target="_blank">Cypress dashboard</a>.
> Tags: `@tag.All`
> Spec:
> <hr>Thu, 10 Jul 2025 19:22:25 UTC
<!-- end of auto-generated comment: Cypress test results  -->


## Communication
Should the DevRel and Marketing teams inform users about this change?
- [ ] Yes
- [ ] No


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

## Summary by CodeRabbit

* **New Features**
* Widgets are now loaded and registered asynchronously, improving app
startup and modularity.
* Widget registration and configuration changes are now versioned,
ensuring selectors and UI update appropriately.
* Widget initialization and factory cache management are more robust,
with explicit cache clearing after widget registration.
* Added new Redux actions and selectors to manage first page load,
deferred JS library loading, and page rendering state.
* Theme handling and widget initialization in AppViewer are streamlined
for faster evaluation and rendering.
* Deferred loading of JavaScript libraries on first page load improves
performance.
* Conditional rendering gates added to AppViewer and Navigation
components based on evaluation state.

* **Bug Fixes**
* Prevented errors when conditionally rendering widgets and navigation
components before evaluation is complete.
* Improved widget property pane and configuration tests to ensure all
widgets are properly loaded and validated.

* **Refactor**
* Widget import and registration logic was refactored to support
dynamic, on-demand loading.
* Evaluation and initialization sagas were modularized for better
maintainability and performance.
* Widget factory and memoization logic were enhanced to allow explicit
cache clearing and version tracking.
* JavaScript library loading logic was parallelized for faster startup.
* Theme application extracted into a dedicated component for clarity and
reuse.

* **Tests**
* Expanded and updated widget and evaluation saga test suites to cover
asynchronous widget loading, cache management, and first evaluation
scenarios.
* Added tests verifying widget factory cache behavior and first
evaluation integration.

* **Chores**
* Updated internal dependencies and selectors to track widget
configuration version changes, ensuring UI consistency.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2025-07-11 12:24:44 +05:30

288 lines
8.5 KiB
TypeScript

import { createSelector } from "reselect";
import type { DefaultRootState } from "react-redux";
import type {
CanvasWidgetsReduxState,
FlattenedWidgetProps,
} from "ee/reducers/entityReducers/canvasWidgetsReducer";
import { getExistingWidgetNames } from "sagas/selectors";
import { getNextEntityName } from "utils/AppsmithUtils";
import WidgetFactory from "WidgetProvider/factory";
import { getWidgetConfigsVersion } from "WidgetProvider/factory/widgetConfigVersion";
import {
getAltBlockWidgetSelection,
getFocusedWidget,
getLastSelectedWidget,
getSelectedWidgets,
} from "./ui";
import { MAIN_CONTAINER_WIDGET_ID } from "constants/WidgetConstants";
import { get } from "lodash";
import { getAppMode } from "ee/selectors/applicationSelectors";
import { APP_MODE } from "entities/App";
import { getIsTableFilterPaneVisible } from "selectors/tableFilterSelectors";
import { getIsAutoHeightWithLimitsChanging } from "utils/hooks/autoHeightUIHooks";
import { getIsPropertyPaneVisible } from "./propertyPaneSelectors";
import { getIsAnvilLayout } from "layoutSystems/anvil/integrations/selectors";
import { selectCombinedPreviewMode } from "./gitModSelectors";
export const getIsDraggingOrResizing = (state: DefaultRootState) =>
state.ui.widgetDragResize.isResizing || state.ui.widgetDragResize.isDragging;
export const getIsResizing = (state: DefaultRootState) =>
state.ui.widgetDragResize.isResizing;
const getCanvasWidgets = (state: DefaultRootState) =>
state.entities.canvasWidgets;
// A selector that gets the modal widget type based on the feature flag
// This will need to be updated once Anvil and WDS are generally available
export const getModalWidgetType = createSelector(
getIsAnvilLayout,
(isAnvilLayout: boolean) => {
let modalWidgetType = "MODAL_WIDGET";
if (isAnvilLayout) {
modalWidgetType = "WDS_MODAL_WIDGET";
}
return modalWidgetType;
},
);
export const getModalWidgets = createSelector(
getCanvasWidgets,
getModalWidgetType,
(widgets, modalWidgetType) => {
const modalWidgets = Object.values(widgets).filter(
(widget: FlattenedWidgetProps) => widget.type === modalWidgetType,
);
if (modalWidgets.length === 0) return undefined;
return modalWidgets;
},
);
export const getModalDropdownList = createSelector(
getModalWidgets,
(modalWidgets) => {
if (!modalWidgets) return undefined;
return modalWidgets.map((widget: FlattenedWidgetProps) => ({
id: widget.widgetId,
label: widget.widgetName,
value: `${widget.widgetName}.name`,
}));
},
);
export const getNextModalName = createSelector(
getExistingWidgetNames,
getModalWidgetType,
getWidgetConfigsVersion, // Add dependency on widget configs version
(names, modalWidgetType) => {
const prefix =
WidgetFactory.widgetConfigMap.get(modalWidgetType)?.widgetName || "";
return getNextEntityName(prefix, names);
},
);
/**
* Selector to get the parent widget of a particaular widget with id as a prop
*/
export const getParentWidget = createSelector(
getCanvasWidgets,
(state: DefaultRootState, widgetId: string) => widgetId,
(canvasWidgets, widgetId: string): FlattenedWidgetProps | undefined => {
if (canvasWidgets.hasOwnProperty(widgetId)) {
const widget = canvasWidgets[widgetId];
if (widget.parentId && canvasWidgets.hasOwnProperty(widget.parentId)) {
const parent = canvasWidgets[widget.parentId];
return parent;
}
}
return;
},
);
export const getFocusedParentToOpen = createSelector(
getCanvasWidgets,
(state: DefaultRootState) => state.ui.widgetDragResize.focusedWidget,
(canvasWidgets, focusedWidgetId) => {
return getParentToOpenIfAny(focusedWidgetId, canvasWidgets);
},
);
export const getParentToOpenSelector = (widgetId: string) => {
return createSelector(getCanvasWidgets, (canvasWidgets) => {
return getParentToOpenIfAny(widgetId, canvasWidgets);
});
};
// Check if widget is in the list of selected widgets
export const isWidgetSelected = (widgetId?: string) => {
return createSelector(getSelectedWidgets, (widgets): boolean =>
widgetId ? widgets.includes(widgetId) : false,
);
};
export const isWidgetFocused = (widgetId: string) => {
return createSelector(
getFocusedWidget,
(widget): boolean => widget === widgetId,
);
};
// Check if current widget is the last selected widget
export const isCurrentWidgetLastSelected = (widgetId: string) => {
return createSelector(
getLastSelectedWidget,
(widget): boolean => widget === widgetId,
);
};
// Check if current widget is one of multiple selected widgets
export const isMultiSelectedWidget = (widgetId: string) => {
return createSelector(
getSelectedWidgets,
(widgets): boolean => widgets.length > 1 && widgets.includes(widgetId),
);
};
export function getParentToOpenIfAny(
widgetId: string | undefined,
widgets: CanvasWidgetsReduxState,
) {
if (widgetId) {
let widget = get(widgets, widgetId, undefined);
// While this widget has a openParentPropertyPane equal to true
while (widget?.openParentPropertyPane) {
// Get parent widget props
const parent = get(widgets, `${widget.parentId}`, undefined);
// If parent has openParentPropertyPane = false, return the current parent
if (!parent?.openParentPropertyPane) {
return parent;
}
if (parent?.parentId && parent.parentId !== MAIN_CONTAINER_WIDGET_ID) {
widget = get(widgets, `${widget.parentId}`, undefined);
continue;
}
}
}
return;
}
export const shouldWidgetIgnoreClicksSelector = (widgetId: string) => {
return createSelector(
getFocusedWidget,
getIsTableFilterPaneVisible,
(state: DefaultRootState) => state.ui.widgetDragResize.isResizing,
(state: DefaultRootState) => state.ui.widgetDragResize.isDragging,
(state: DefaultRootState) =>
state.ui.canvasSelection.isDraggingForSelection,
getAppMode,
selectCombinedPreviewMode,
getIsAutoHeightWithLimitsChanging,
getAltBlockWidgetSelection,
(
focusedWidgetId,
isTableFilterPaneVisible,
isResizing,
isDragging,
isDraggingForSelection,
appMode,
isPreviewMode,
isAutoHeightWithLimitsChanging,
isWidgetSelectionBlock,
) => {
const isFocused = focusedWidgetId === widgetId;
return (
isDraggingForSelection ||
isResizing ||
isDragging ||
isPreviewMode ||
appMode !== APP_MODE.EDIT ||
!isFocused ||
isTableFilterPaneVisible ||
isAutoHeightWithLimitsChanging ||
isWidgetSelectionBlock
);
},
);
};
export const getSelectedWidgetAncestry = (state: DefaultRootState) =>
state.ui.widgetDragResize.selectedWidgetAncestry;
export const getEntityExplorerWidgetAncestry = (state: DefaultRootState) =>
state.ui.widgetDragResize.entityExplorerAncestry;
export const getEntityExplorerWidgetsToExpand = createSelector(
getEntityExplorerWidgetAncestry,
(selectedWidgetAncestry: string[]) => {
return selectedWidgetAncestry.slice(1);
},
);
export const showWidgetAsSelected = (widgetId: string) => {
return createSelector(
getLastSelectedWidget,
getSelectedWidgets,
(lastSelectedWidgetId, selectedWidgets) => {
return (
lastSelectedWidgetId === widgetId ||
(selectedWidgets.length > 1 && selectedWidgets.includes(widgetId))
);
},
);
};
export const getFirstSelectedWidgetInList = createSelector(
getSelectedWidgets,
(selectedWidgets) => {
return selectedWidgets?.length ? selectedWidgets[0] : undefined;
},
);
export const isCurrentWidgetActiveInPropertyPane = (widgetId: string) => {
return createSelector(
getIsPropertyPaneVisible,
getFirstSelectedWidgetInList,
(isPaneVisible, firstSelectedWidgetId) => {
return isPaneVisible && firstSelectedWidgetId === widgetId;
},
);
};
export const isResizingOrDragging = createSelector(
(state: DefaultRootState) => state.ui.widgetDragResize.isResizing,
(state: DefaultRootState) => state.ui.widgetDragResize.isDragging,
(isResizing, isDragging) => !!isResizing || !!isDragging,
);
// get widgets types associated to a tab
export const getUsedWidgetTypes = createSelector(
getCanvasWidgets,
(canvasWidgets) => {
const widgetTypes = new Set<string>();
// Iterate through all widgets in the state
Object.values(canvasWidgets).forEach((widget) => {
if (widget.type && !widget.type.startsWith("MODULE_WIDGET_")) {
widgetTypes.add(widget.type);
}
});
return Array.from(widgetTypes);
},
);