## Description
This PR upgrades Prettier to v2 + enforces TypeScript’s [`import
type`](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-8.html#type-only-imports-and-export)
syntax where applicable. It’s submitted as a separate PR so we can merge
it easily.
As a part of this PR, we reformat the codebase heavily:
- add `import type` everywhere where it’s required, and
- re-format the code to account for Prettier 2’s breaking changes:
https://prettier.io/blog/2020/03/21/2.0.0.html#breaking-changes
This PR is submitted against `release` to make sure all new code by team
members will adhere to new formatting standards, and we’ll have fewer
conflicts when merging `bundle-optimizations` into `release`. (I’ll
merge `release` back into `bundle-optimizations` once this PR is
merged.)
### Why is this needed?
This PR is needed because, for the Lodash optimization from
7cbb12af88,
we need to use `import type`. Otherwise, `babel-plugin-lodash` complains
that `LoDashStatic` is not a lodash function.
However, just using `import type` in the current codebase will give you
this:
<img width="962" alt="Screenshot 2023-03-08 at 17 45 59"
src="https://user-images.githubusercontent.com/2953267/223775744-407afa0c-e8b9-44a1-90f9-b879348da57f.png">
That’s because Prettier 1 can’t parse `import type` at all. To parse it,
we need to upgrade to Prettier 2.
### Why enforce `import type`?
Apart from just enabling `import type` support, this PR enforces
specifying `import type` everywhere it’s needed. (Developers will get
immediate TypeScript and ESLint errors when they forget to do so.)
I’m doing this because I believe `import type` improves DX and makes
refactorings easier.
Let’s say you had a few imports like below. Can you tell which of these
imports will increase the bundle size? (Tip: it’s not all of them!)
```ts
// app/client/src/workers/Linting/utils.ts
import { Position } from "codemirror";
import { LintError as JSHintError, LintOptions } from "jshint";
import { get, isEmpty, isNumber, keys, last, set } from "lodash";
```
It’s pretty hard, right?
What about now?
```ts
// app/client/src/workers/Linting/utils.ts
import type { Position } from "codemirror";
import type { LintError as JSHintError, LintOptions } from "jshint";
import { get, isEmpty, isNumber, keys, last, set } from "lodash";
```
Now, it’s clear that only `lodash` will be bundled.
This helps developers to see which imports are problematic, but it
_also_ helps with refactorings. Now, if you want to see where
`codemirror` is bundled, you can just grep for `import \{.*\} from
"codemirror"` – and you won’t get any type-only imports.
This also helps (some) bundlers. Upon transpiling, TypeScript erases
type-only imports completely. In some environment (not ours), this makes
the bundle smaller, as the bundler doesn’t need to bundle type-only
imports anymore.
## Type of change
- Chore (housekeeping or task changes that don't impact user perception)
## How Has This Been Tested?
This was tested to not break the build.
### 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
- [ ] 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
- [x] 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:
- [ ] 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
---------
Co-authored-by: Satish Gandham <hello@satishgandham.com>
Co-authored-by: Satish Gandham <satish.iitg@gmail.com>
426 lines
12 KiB
TypeScript
426 lines
12 KiB
TypeScript
import {
|
|
all,
|
|
call,
|
|
delay,
|
|
put,
|
|
select,
|
|
takeEvery,
|
|
takeLatest,
|
|
} from "redux-saga/effects";
|
|
|
|
import * as Sentry from "@sentry/react";
|
|
import log from "loglevel";
|
|
|
|
import {
|
|
getCurrentWidgetId,
|
|
getIsPropertyPaneVisible,
|
|
} from "selectors/propertyPaneSelectors";
|
|
import { closePropertyPane } from "actions/widgetActions";
|
|
import { selectWidgetInitAction } from "actions/widgetSelectionActions";
|
|
import type {
|
|
ReduxAction,
|
|
ReplayReduxActionTypes,
|
|
} from "@appsmith/constants/ReduxActionConstants";
|
|
import { ReduxActionTypes } from "@appsmith/constants/ReduxActionConstants";
|
|
import { flashElementsById } from "utils/helpers";
|
|
import {
|
|
expandAccordion,
|
|
highlightReplayElement,
|
|
processUndoRedoToasts,
|
|
scrollWidgetIntoView,
|
|
switchTab,
|
|
} from "utils/replayHelpers";
|
|
import { updateAndSaveLayout } from "actions/pageActions";
|
|
import AnalyticsUtil from "utils/AnalyticsUtil";
|
|
import {
|
|
getCurrentApplicationId,
|
|
snipingModeSelector,
|
|
} from "selectors/editorSelectors";
|
|
import { findFieldInfo, REPLAY_FOCUS_DELAY } from "entities/Replay/replayUtils";
|
|
import { setActionProperty, updateAction } from "actions/pluginActionActions";
|
|
import { getEntityInCurrentPath } from "./RecentEntitiesSagas";
|
|
import { updateJSCollectionBody } from "actions/jsPaneActions";
|
|
import {
|
|
updateReplayEntitySaga,
|
|
workerComputeUndoRedo,
|
|
} from "./EvaluationsSaga";
|
|
import { createBrowserHistory } from "history";
|
|
import {
|
|
getEditorConfig,
|
|
getPluginForm,
|
|
getSettingConfig,
|
|
} from "selectors/entitiesSelector";
|
|
import type { Action } from "entities/Action";
|
|
import { isAPIAction, isQueryAction, isSaaSAction } from "entities/Action";
|
|
import { API_EDITOR_TABS } from "constants/ApiEditorConstants/CommonApiConstants";
|
|
import { EDITOR_TABS } from "constants/QueryEditorConstants";
|
|
import _, { isEmpty } from "lodash";
|
|
import type { ReplayEditorUpdate } from "entities/Replay/ReplayEntity/ReplayEditor";
|
|
import { ENTITY_TYPE } from "entities/AppsmithConsole";
|
|
import type { Datasource } from "entities/Datasource";
|
|
import { initialize } from "redux-form";
|
|
import {
|
|
API_EDITOR_FORM_NAME,
|
|
DATASOURCE_DB_FORM,
|
|
DATASOURCE_REST_API_FORM,
|
|
QUERY_EDITOR_FORM_NAME,
|
|
} from "@appsmith/constants/forms";
|
|
import type { Canvas } from "entities/Replay/ReplayEntity/ReplayCanvas";
|
|
import {
|
|
setAppThemingModeStackAction,
|
|
updateSelectedAppThemeAction,
|
|
} from "actions/appThemingActions";
|
|
import { AppThemingMode } from "selectors/appThemingSelectors";
|
|
import { generateAutoHeightLayoutTreeAction } from "actions/autoHeightActions";
|
|
import { SelectionRequestType } from "sagas/WidgetSelectUtils";
|
|
|
|
export type UndoRedoPayload = {
|
|
operation: ReplayReduxActionTypes;
|
|
};
|
|
|
|
export default function* undoRedoListenerSaga() {
|
|
yield all([
|
|
takeEvery(ReduxActionTypes.UNDO_REDO_OPERATION, undoRedoSaga),
|
|
takeLatest(ReduxActionTypes.UPDATE_REPLAY_ENTITY, updateReplayEntitySaga),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* This Saga is called if the type of update is a property change
|
|
* @param replay
|
|
* @returns
|
|
*/
|
|
export function* openPropertyPaneSaga(replay: any) {
|
|
try {
|
|
const replayWidgetId = Object.keys(replay.widgets)[0];
|
|
|
|
if (!replayWidgetId || !replay.widgets[replayWidgetId].propertyUpdates)
|
|
return;
|
|
|
|
scrollWidgetIntoView(replayWidgetId);
|
|
|
|
const isPropertyPaneVisible: boolean = yield select(
|
|
getIsPropertyPaneVisible,
|
|
);
|
|
const selectedWidgetId: string = yield select(getCurrentWidgetId);
|
|
|
|
//if property pane is not visible, select the widget and force open property pane
|
|
if (selectedWidgetId !== replayWidgetId || !isPropertyPaneVisible) {
|
|
yield put(
|
|
selectWidgetInitAction(SelectionRequestType.One, [replayWidgetId]),
|
|
);
|
|
}
|
|
|
|
flashElementsById(
|
|
btoa(
|
|
replay.widgets[replayWidgetId].propertyUpdates.slice(0, 2).join("."),
|
|
),
|
|
0,
|
|
1000,
|
|
);
|
|
} catch (e) {
|
|
log.error(e);
|
|
Sentry.captureException(e);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* This saga is called when type of chenge is not a property Change
|
|
* @param replay
|
|
* @returns
|
|
*/
|
|
export function* postUndoRedoSaga(replay: any) {
|
|
try {
|
|
const isPropertyPaneVisible: boolean = yield select(
|
|
getIsPropertyPaneVisible,
|
|
);
|
|
|
|
if (isPropertyPaneVisible) yield put(closePropertyPane());
|
|
|
|
// display toasts if it is a destructive operation
|
|
if (replay.toasts && replay.toasts.length > 0) {
|
|
processUndoRedoToasts(replay.toasts);
|
|
}
|
|
|
|
if (!replay.widgets || Object.keys(replay.widgets).length <= 0) return;
|
|
|
|
const widgetIds = Object.keys(replay.widgets);
|
|
|
|
yield put(selectWidgetInitAction(SelectionRequestType.Multiple, widgetIds));
|
|
scrollWidgetIntoView(widgetIds[0]);
|
|
} catch (e) {
|
|
log.error(e);
|
|
Sentry.captureException(e);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* saga that listen for UNDO_REDO_OPERATION
|
|
* it won't do anything in case of sniping/comment mode
|
|
*
|
|
* @param action
|
|
* @returns
|
|
*/
|
|
export function* undoRedoSaga(action: ReduxAction<UndoRedoPayload>) {
|
|
const isSnipingMode: boolean = yield select(snipingModeSelector);
|
|
|
|
// if the app is in snipping or comments mode, don't do anything
|
|
if (isSnipingMode) return;
|
|
try {
|
|
const history = createBrowserHistory();
|
|
const pathname = history.location.pathname;
|
|
const { id, type } = getEntityInCurrentPath(pathname);
|
|
const entityId = type === "page" ? "canvas" : id;
|
|
// @ts-expect-error: workerResponse is of type unknown
|
|
const workerResponse = yield call(
|
|
workerComputeUndoRedo,
|
|
action.payload.operation,
|
|
entityId,
|
|
);
|
|
|
|
// if there is no change, then don't do anythingÎ
|
|
if (!workerResponse) return;
|
|
|
|
const {
|
|
event,
|
|
logs,
|
|
paths,
|
|
replay,
|
|
replayEntity,
|
|
replayEntityType,
|
|
timeTaken,
|
|
} = workerResponse;
|
|
|
|
logs && logs.forEach((evalLog: any) => log.debug(evalLog));
|
|
|
|
if (replay.theme) {
|
|
yield call(replayThemeSaga, replayEntity, replay);
|
|
|
|
return;
|
|
}
|
|
switch (replayEntityType) {
|
|
case ENTITY_TYPE.WIDGET: {
|
|
const isPropertyUpdate = replay.widgets && replay.propertyUpdates;
|
|
AnalyticsUtil.logEvent(event, { paths, timeTaken });
|
|
|
|
yield put(
|
|
updateAndSaveLayout(replayEntity.widgets, {
|
|
isRetry: false,
|
|
shouldReplay: false,
|
|
}),
|
|
);
|
|
|
|
if (isPropertyUpdate) {
|
|
yield call(openPropertyPaneSaga, replay);
|
|
}
|
|
if (!isPropertyUpdate) {
|
|
yield call(postUndoRedoSaga, replay);
|
|
}
|
|
yield put(generateAutoHeightLayoutTreeAction(true, false));
|
|
break;
|
|
}
|
|
case ENTITY_TYPE.ACTION:
|
|
yield call(replayActionSaga, replayEntity, replay);
|
|
break;
|
|
case ENTITY_TYPE.DATASOURCE: {
|
|
yield call(replayDatasourceSaga, replayEntity, replay);
|
|
break;
|
|
}
|
|
case ENTITY_TYPE.JSACTION:
|
|
yield put(
|
|
updateJSCollectionBody(replayEntity.body, replayEntity.id, true),
|
|
);
|
|
break;
|
|
}
|
|
} catch (e) {
|
|
log.error(e);
|
|
Sentry.captureException(e);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* replay theme actions
|
|
*
|
|
* @param replayEntity
|
|
* @param replay
|
|
*/
|
|
function* replayThemeSaga(replayEntity: Canvas, replay: any) {
|
|
const applicationId: string = yield select(getCurrentApplicationId);
|
|
|
|
// if theme is changed, open the theme selector
|
|
if (replay.themeChanged) {
|
|
yield put(
|
|
setAppThemingModeStackAction([AppThemingMode.APP_THEME_SELECTION]),
|
|
);
|
|
} else {
|
|
yield put(setAppThemingModeStackAction([]));
|
|
}
|
|
|
|
yield put(selectWidgetInitAction(SelectionRequestType.Empty));
|
|
|
|
// todo(pawan): check with arun/rahul on how we can get rid of this check
|
|
// better way to do is set shouldreplay = false when evaluating tree
|
|
if (replayEntity.theme.id) {
|
|
yield put(
|
|
updateSelectedAppThemeAction({
|
|
theme: replayEntity.theme,
|
|
shouldReplay: false,
|
|
applicationId,
|
|
}),
|
|
);
|
|
}
|
|
}
|
|
|
|
function* replayActionSaga(
|
|
replayEntity: Action,
|
|
replay: { updates: ReplayEditorUpdate[] },
|
|
) {
|
|
const { updates = [] } = replay;
|
|
/**
|
|
* Pick one diff to determine tab switching.
|
|
* Diffs across multiple tabs are unlikely
|
|
*/
|
|
const { modifiedProperty } = updates[updates.length - 1] || {};
|
|
const { currentTab } = yield call(
|
|
getEditorFieldConfig,
|
|
replayEntity,
|
|
modifiedProperty,
|
|
);
|
|
|
|
/**
|
|
* Check if tab needs to be switched and switch is necessary
|
|
* Delay change if tab needs to be switched
|
|
*/
|
|
const didSwitch: boolean = yield call(switchTab, currentTab);
|
|
if (didSwitch) yield delay(REPLAY_FOCUS_DELAY);
|
|
|
|
//Reinitialize form
|
|
const currentFormName =
|
|
isQueryAction(replayEntity) || isSaaSAction(replayEntity)
|
|
? QUERY_EDITOR_FORM_NAME
|
|
: API_EDITOR_FORM_NAME;
|
|
yield put(initialize(currentFormName, replayEntity));
|
|
|
|
//Begin modified field highlighting
|
|
highlightReplayElement(
|
|
updates.map((u: ReplayEditorUpdate) => u.modifiedProperty),
|
|
);
|
|
|
|
/**
|
|
* Update all the diffs in the action object.
|
|
* We need this for debugger logs, dynamicBindingPathList and to call relevant APIs */
|
|
yield all(
|
|
updates.map((u) =>
|
|
put(
|
|
setActionProperty({
|
|
actionId: replayEntity.id,
|
|
propertyName: u.modifiedProperty,
|
|
value:
|
|
u.kind === "A" ? _.get(replayEntity, u.modifiedProperty) : u.update,
|
|
skipSave: true,
|
|
}),
|
|
),
|
|
),
|
|
);
|
|
|
|
//Save the updated action object
|
|
yield put(updateAction({ id: replayEntity.id }));
|
|
}
|
|
|
|
function* replayDatasourceSaga(
|
|
replayEntity: Datasource,
|
|
replay: { updates: ReplayEditorUpdate[] },
|
|
) {
|
|
const { updates = [] } = replay;
|
|
const { modifiedProperty } = updates[updates.length - 1] || {};
|
|
const { fieldInfo: { parentSection = "" } = {} } = yield call(
|
|
getDatasourceFieldConfig,
|
|
replayEntity,
|
|
modifiedProperty,
|
|
);
|
|
/**
|
|
* Check if the modified field is under a collapsed section and expand if necessary
|
|
* Delay change if accordion needs to be expanded
|
|
*/
|
|
const didExpand: boolean = yield call(expandAccordion, parentSection);
|
|
if (didExpand) yield delay(REPLAY_FOCUS_DELAY);
|
|
|
|
/**
|
|
* Reinitialize redux form
|
|
* Attribute "datasourceId" is used as a differentiator between rest-api datasources and the others */
|
|
if (replayEntity.hasOwnProperty("datasourceId")) {
|
|
yield put(initialize(DATASOURCE_REST_API_FORM, replayEntity));
|
|
} else {
|
|
yield put(initialize(DATASOURCE_DB_FORM, _.omit(replayEntity, ["name"])));
|
|
}
|
|
|
|
// Highlight modified fields
|
|
highlightReplayElement(
|
|
updates.map((u: ReplayEditorUpdate) => u.modifiedProperty),
|
|
);
|
|
}
|
|
|
|
/*
|
|
Figure out the field config of the last modified field in datasource forms
|
|
*/
|
|
function* getDatasourceFieldConfig(
|
|
replayEntity: Datasource,
|
|
modifiedProperty: string,
|
|
) {
|
|
const formConfig: [Record<any, any>] = yield select(
|
|
getPluginForm,
|
|
replayEntity.pluginId,
|
|
);
|
|
const fieldInfo = findFieldInfo(formConfig, modifiedProperty);
|
|
return { fieldInfo };
|
|
}
|
|
|
|
/*
|
|
Figure out the tab in which the last modified field is present and the
|
|
field config of the last modified field.
|
|
*/
|
|
function* getEditorFieldConfig(replayEntity: Action, modifiedProperty: string) {
|
|
let currentTab = "";
|
|
let fieldInfo = {};
|
|
if (!modifiedProperty) return { currentTab, fieldInfo };
|
|
if (isAPIAction(replayEntity)) {
|
|
if (modifiedProperty.includes("headers"))
|
|
currentTab = API_EDITOR_TABS.HEADERS;
|
|
else if (modifiedProperty.includes("queryParameters"))
|
|
currentTab = API_EDITOR_TABS.PARAMS;
|
|
else if (modifiedProperty.includes("body"))
|
|
currentTab = API_EDITOR_TABS.BODY;
|
|
else if (
|
|
modifiedProperty.includes("pagination") ||
|
|
modifiedProperty.includes("next") ||
|
|
modifiedProperty.includes("previous")
|
|
)
|
|
currentTab = API_EDITOR_TABS.PAGINATION;
|
|
if (!currentTab) {
|
|
const settingsConfig: [Record<any, any>] = yield select(
|
|
getSettingConfig,
|
|
replayEntity.pluginId,
|
|
);
|
|
fieldInfo = findFieldInfo(settingsConfig, modifiedProperty);
|
|
if (!isEmpty(fieldInfo)) currentTab = API_EDITOR_TABS.SETTINGS;
|
|
}
|
|
} else {
|
|
const editorConfig: [Record<any, any>] = yield select(
|
|
getEditorConfig,
|
|
replayEntity.pluginId,
|
|
);
|
|
fieldInfo = findFieldInfo(editorConfig, modifiedProperty);
|
|
if (!isEmpty(fieldInfo)) {
|
|
currentTab = EDITOR_TABS.QUERY;
|
|
} else {
|
|
const settingsConfig: [Record<any, any>] = yield select(
|
|
getSettingConfig,
|
|
replayEntity.pluginId,
|
|
);
|
|
fieldInfo = findFieldInfo(settingsConfig, modifiedProperty);
|
|
if (!isEmpty(fieldInfo)) currentTab = EDITOR_TABS.SETTINGS;
|
|
}
|
|
}
|
|
return { currentTab, fieldInfo };
|
|
}
|