PromucFlow_constructor/app/client/src/sagas/TernSaga.ts
Favour Ohanekwu 4b3ef8ebd6
fix: improve autocompletion hints discovery (#28222)
## Description

This PR improves autocompletion hints discovery by

- Taking entities' recency of usage into consideration when sorting
hints
- Showing entity names at the top, before supported functions and
properties
- Deprioritizing the Function constructor and the MainContainer entity

#### PR fixes following issue(s)
Fixes #27870 
Fixes #17684 
Fixes https://github.com/appsmithorg/appsmith/issues/24975


#### Type of change
- New feature (non-breaking change which adds functionality)
 
## 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
- [ ] Manual
- [ ] JUnit
- [ ] Jest
- [ ] Cypress
>
>
#### Test Plan
- [x] check for autocomplete results when multiple widgets of the same
type are used
- [x] check for entity renaming behavior
- [x] verify function and mainContainer have been removed
- [x] verify that recent entity changes affects autocomplete ranking
>
>
#### Issues raised during DP testing

https://github.com/appsmithorg/appsmith/pull/28222#issuecomment-1777487126
>
>
>
## 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:
- [x] [Speedbreak
features](https://github.com/appsmithorg/TestSmith/wiki/Guidelines-for-test-plans#speedbreakers-)
have been covered
- [x] 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
- [x] 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
2023-10-25 18:18:45 +01:00

77 lines
2.6 KiB
TypeScript

import type { ReduxAction } from "@appsmith/constants/ReduxActionConstants";
import { ReduxActionTypes } from "@appsmith/constants/ReduxActionConstants";
import {
getActions,
getJSCollections,
} from "@appsmith/selectors/entitiesSelector";
import type { AppState } from "@appsmith/reducers";
import type { RecentEntity } from "components/editorComponents/GlobalSearch/utils";
import type { Datasource } from "entities/Datasource";
import { get } from "lodash";
import { FocusEntity } from "navigation/FocusEntity";
import { select, takeLatest } from "redux-saga/effects";
import { getWidgets } from "./selectors";
import CodemirrorTernService from "utils/autocomplete/CodemirrorTernService";
function* handleSetTernRecentEntities(action: ReduxAction<RecentEntity[]>) {
const recentEntities = action.payload || [];
const actions: ReturnType<typeof getActions> = yield select(getActions);
const jsActions: ReturnType<typeof getJSCollections> =
yield select(getJSCollections);
const reducerDatasources: Datasource[] = yield select((state: AppState) => {
return state.entities.datasources.list;
});
const widgetsMap: ReturnType<typeof getWidgets> = yield select(getWidgets);
const recentEntityNames = new Set<string>();
for (const recentEntity of recentEntities) {
const { id, type } = recentEntity;
switch (type) {
case FocusEntity.DATASOURCE: {
const datasource = reducerDatasources.find(
(reducerDatasource) => reducerDatasource.id === id,
);
if (!datasource) break;
recentEntityNames.add(datasource.name);
break;
}
case FocusEntity.API:
case FocusEntity.QUERY: {
const action = actions.find((action) => action?.config?.id === id);
if (!action) break;
recentEntityNames.add(action.config.name);
break;
}
case FocusEntity.JS_OBJECT: {
const action = jsActions.find((action) => action?.config?.id === id);
if (!action) break;
recentEntityNames.add(action.config.name);
break;
}
case FocusEntity.PROPERTY_PANE: {
const widget = get(widgetsMap, id, null);
if (!widget) break;
recentEntityNames.add(widget.widgetName);
}
}
}
CodemirrorTernService.updateRecentEntities(Array.from(recentEntityNames));
}
function* handleResetTernRecentEntities() {
CodemirrorTernService.updateRecentEntities([]);
}
export default function* ternSagas() {
yield takeLatest(
ReduxActionTypes.SET_RECENT_ENTITIES,
handleSetTernRecentEntities,
);
yield takeLatest(
ReduxActionTypes.RESET_RECENT_ENTITIES,
handleResetTernRecentEntities,
);
}