PromucFlow_constructor/app/client/src/selectors/navigationSelectors.ts
Anand Srinivasan 76f22399e5
fix: performance improvements for js editor (#21492)
## Description

TL;DR performance improvements for js editor

- fix entityNavigationData generation (to prevent unnecessary component
updates)
- in codeEditor/index.ts (`addThisReference` was creating a new object
everytime)
- in navigationSelector.ts (use `getJSCollections` instead of
`getJSCollectionsForCurrentPage`, which created a new object everytime,
even if actions were not updated)
- combine markers for navigation and peek overlay to reduce the total
number of markers
- clear and add marks for only the edited lines instead of the whole
file

Note: once a js object is saved, it's still going to trigger a whole
file clear and marking.
Because, it's an entity update which needs a whole refresh of the
markers.

Fixes #21467


## Media
Case: Adding a blank space in js editor.

### Reduced un-necessary clears and marks:
#### Before: 

![image](https://user-images.githubusercontent.com/66776129/225681826-980e7ea6-5a9f-45ac-9de8-b6b5d73078d7.png)

####After:

![image](https://user-images.githubusercontent.com/66776129/225681997-064db06b-b208-4cdf-8ead-f67de8ea2d34.png)

---

### Reduced entity marker called count:
https://www.loom.com/share/23719f8dfde8457ea0a86f44500ec34a

---

### Reduced markers count:
#### Before:

![image](https://user-images.githubusercontent.com/66776129/225792984-1eb4082e-fbfe-4fa2-bad8-6797d7095673.png)

#### After:

![image](https://user-images.githubusercontent.com/66776129/225793028-04480724-8822-4934-8264-375ba7bd95cd.png)



## Type of change
- Bug fix (non-breaking change which fixes an issue)


## How Has This Been Tested?
- Manual

### 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
- [x] My code follows the style guidelines of this project
- [x] I have performed a self-review of my own code
- [x] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation
- [x] My changes generate no new warnings
- [ ] I have added tests that prove my fix is effective or that my
feature works
- [x] New and existing unit tests pass locally with my changes
- [ ] PR is being merged under a feature flag


### QA activity:
- [ ] Test plan has been approved by relevant developers
- [ ] Test plan has been peer reviewed by QA
- [ ] Cypress test cases have been added and approved by either SDET or
manual QA
- [ ] Organized project review call with relevant stakeholders after
Round 1/2 of QA
- [ ] Added Test Plan Approved label after reveiwing all Cypress test
2023-03-21 10:39:43 +05:30

127 lines
4.1 KiB
TypeScript

import type {
DataTree,
AppsmithEntity,
} from "entities/DataTree/dataTreeFactory";
import { ENTITY_TYPE } from "entities/DataTree/dataTreeFactory";
import { createSelector } from "reselect";
import {
getActionsForCurrentPage,
getJSCollections,
getPlugins,
} from "selectors/entitiesSelector";
import { getWidgets } from "sagas/selectors";
import { getCurrentPageId } from "selectors/editorSelectors";
import { getActionConfig } from "pages/Editor/Explorer/Actions/helpers";
import { jsCollectionIdURL, widgetURL } from "RouteBuilder";
import { getDataTree } from "selectors/dataTreeSelectors";
import { getActionChildrenNavData } from "utils/NavigationSelector/ActionChildren";
import { createNavData } from "utils/NavigationSelector/common";
import { getWidgetChildrenNavData } from "utils/NavigationSelector/WidgetChildren";
import { getJsChildrenNavData } from "utils/NavigationSelector/JsChildren";
import { getAppsmithNavData } from "utils/NavigationSelector/AppsmithNavData";
import { isJSObject } from "ce/workers/Evaluation/evaluationUtils";
export type NavigationData = {
name: string;
id: string;
type: ENTITY_TYPE;
url: string | undefined;
navigable: boolean;
children: EntityNavigationData;
peekable: boolean;
peekData?: unknown;
key?: string;
};
export type EntityNavigationData = Record<string, NavigationData>;
export const getEntitiesForNavigation = createSelector(
getActionsForCurrentPage,
getPlugins,
getJSCollections,
getWidgets,
getCurrentPageId,
getDataTree,
(_: any, entityName: string | undefined) => entityName,
(
actions,
plugins,
jsActions,
widgets,
pageId,
dataTree: DataTree,
entityName: string | undefined,
) => {
// data tree retriggers this
jsActions = jsActions.filter((a) => a.config.pageId === pageId);
const navigationData: EntityNavigationData = {};
if (!dataTree) return navigationData;
actions.forEach((action) => {
const plugin = plugins.find(
(plugin) => plugin.id === action.config.pluginId,
);
const config = getActionConfig(action.config.pluginType);
// dataTree used to get entityDefinitions and peekData
const result = getActionChildrenNavData(action, dataTree);
if (!config) return;
navigationData[action.config.name] = createNavData({
id: action.config.id,
name: action.config.name,
type: ENTITY_TYPE.ACTION,
url: config.getURL(
pageId,
action.config.id,
action.config.pluginType,
plugin,
),
peekable: true,
peekData: result?.peekData,
children: result?.childNavData || {},
});
});
jsActions.forEach((jsAction) => {
// dataTree for null check and peekData
const result = getJsChildrenNavData(jsAction, pageId, dataTree);
navigationData[jsAction.config.name] = createNavData({
id: jsAction.config.id,
name: jsAction.config.name,
type: ENTITY_TYPE.JSACTION,
url: jsCollectionIdURL({ pageId, collectionId: jsAction.config.id }),
peekable: true,
peekData: result?.peekData,
children: result?.childNavData || {},
});
});
Object.values(widgets).forEach((widget) => {
// dataTree to get entityDefinitions, for url (can use getWidgetByName?) and peekData
const result = getWidgetChildrenNavData(widget, dataTree, pageId);
navigationData[widget.widgetName] = createNavData({
id: widget.widgetId,
name: widget.widgetName,
type: ENTITY_TYPE.WIDGET,
url: widgetURL({ pageId, selectedWidgets: [widget.widgetId] }),
peekable: true,
peekData: result?.peekData,
children: result?.childNavData || {},
});
});
// dataTree to get entity definitions and peekData
navigationData["appsmith"] = getAppsmithNavData(
dataTree.appsmith as AppsmithEntity,
);
if (
entityName &&
isJSObject(dataTree[entityName]) &&
entityName in navigationData
) {
return {
...navigationData,
this: navigationData[entityName],
};
}
return navigationData;
},
);