## 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>
377 lines
10 KiB
TypeScript
377 lines
10 KiB
TypeScript
import React from "react";
|
|
import type {
|
|
ChangeSelectedAppThemeAction,
|
|
DeleteAppThemeAction,
|
|
FetchAppThemesAction,
|
|
FetchSelectedAppThemeAction,
|
|
SaveAppThemeAction,
|
|
UpdateSelectedAppThemeAction,
|
|
} from "actions/appThemingActions";
|
|
import { updateisBetaCardShownAction } from "actions/appThemingActions";
|
|
import type { ReduxAction } from "@appsmith/constants/ReduxActionConstants";
|
|
import {
|
|
ReduxActionErrorTypes,
|
|
ReduxActionTypes,
|
|
} from "@appsmith/constants/ReduxActionConstants";
|
|
import ThemingApi from "api/AppThemingApi";
|
|
import { all, takeLatest, put, select } from "redux-saga/effects";
|
|
import { Toaster, Variant } from "design-system-old";
|
|
import {
|
|
CHANGE_APP_THEME,
|
|
createMessage,
|
|
DELETE_APP_THEME,
|
|
SAVE_APP_THEME,
|
|
SET_DEFAULT_SELECTED_THEME,
|
|
} from "@appsmith/constants/messages";
|
|
import { ENTITY_TYPE } from "entities/AppsmithConsole";
|
|
import { undoAction, updateReplayEntity } from "actions/pageActions";
|
|
import { getCanvasWidgets } from "selectors/entitiesSelector";
|
|
import store from "store";
|
|
import { getAppMode } from "selectors/applicationSelectors";
|
|
import type { APP_MODE } from "entities/App";
|
|
import { getCurrentUser } from "selectors/usersSelectors";
|
|
import type { User } from "constants/userConstants";
|
|
import { getBetaFlag, setBetaFlag, STORAGE_KEYS } from "utils/storage";
|
|
import type { UpdateWidgetPropertyPayload } from "actions/controlActions";
|
|
import { batchUpdateMultipleWidgetProperties } from "actions/controlActions";
|
|
import { getPropertiesToUpdateForReset } from "entities/AppTheming/utils";
|
|
import type { ApiResponse } from "api/ApiResponses";
|
|
import type { AppTheme } from "entities/AppTheming";
|
|
import type { CanvasWidgetsReduxState } from "reducers/entityReducers/canvasWidgetsReducer";
|
|
import {
|
|
getCurrentApplicationId,
|
|
selectApplicationVersion,
|
|
} from "selectors/editorSelectors";
|
|
import { find } from "lodash";
|
|
import * as Sentry from "@sentry/react";
|
|
import { Severity } from "@sentry/react";
|
|
import { getAllPageIds } from "./selectors";
|
|
import type { SagaIterator } from "@redux-saga/types";
|
|
import type { AxiosPromise } from "axios";
|
|
|
|
/**
|
|
* init app theming
|
|
*/
|
|
export function* initAppTheming() {
|
|
try {
|
|
const user: User = yield select(getCurrentUser);
|
|
const { email } = user;
|
|
if (email) {
|
|
const appThemingBetaFlag: boolean = yield getBetaFlag(
|
|
email,
|
|
STORAGE_KEYS.APP_THEMING_BETA_SHOWN,
|
|
);
|
|
|
|
yield put(updateisBetaCardShownAction(appThemingBetaFlag));
|
|
}
|
|
} catch (error) {}
|
|
}
|
|
|
|
/**
|
|
* fetches all themes of the application
|
|
*
|
|
* @param action
|
|
*/
|
|
// eslint-disable-next-line
|
|
export function* fetchAppThemes(action: ReduxAction<FetchAppThemesAction>) {
|
|
try {
|
|
const { applicationId } = action.payload;
|
|
const response: ApiResponse<AppTheme> = yield ThemingApi.fetchThemes(
|
|
applicationId,
|
|
);
|
|
|
|
yield put({
|
|
type: ReduxActionTypes.FETCH_APP_THEMES_SUCCESS,
|
|
payload: response.data,
|
|
});
|
|
} catch (error) {
|
|
yield put({
|
|
type: ReduxActionErrorTypes.FETCH_APP_THEMES_ERROR,
|
|
payload: { error },
|
|
});
|
|
}
|
|
}
|
|
|
|
/**
|
|
* fetches the selected theme of the application
|
|
*
|
|
* @param action
|
|
*/
|
|
|
|
export function* fetchAppSelectedTheme(
|
|
// eslint-disable-next-line
|
|
action: ReduxAction<FetchSelectedAppThemeAction>,
|
|
): SagaIterator | AxiosPromise {
|
|
const { applicationId } = action.payload;
|
|
const mode: APP_MODE = yield select(getAppMode);
|
|
|
|
const pageIds = yield select(getAllPageIds);
|
|
const userDetails = yield select(getCurrentUser);
|
|
const applicationVersion = yield select(selectApplicationVersion);
|
|
try {
|
|
// eslint-disable-next-line
|
|
const response: ApiResponse<AppTheme[]> = yield ThemingApi.fetchSelected(
|
|
applicationId,
|
|
mode,
|
|
);
|
|
if (response?.data) {
|
|
yield put({
|
|
type: ReduxActionTypes.FETCH_SELECTED_APP_THEME_SUCCESS,
|
|
payload: response.data,
|
|
});
|
|
} else {
|
|
Sentry.captureException("Unable to fetch the selected theme", {
|
|
level: Severity.Critical,
|
|
extra: {
|
|
pageIds,
|
|
applicationId,
|
|
applicationVersion,
|
|
userDetails,
|
|
themeResponse: response,
|
|
},
|
|
});
|
|
|
|
// If the response.data is undefined then we set selectedTheme to default Theme
|
|
yield put({
|
|
type: ReduxActionTypes.SET_DEFAULT_SELECTED_THEME_INIT,
|
|
});
|
|
}
|
|
} catch (error) {
|
|
yield put({
|
|
type: ReduxActionErrorTypes.FETCH_SELECTED_APP_THEME_ERROR,
|
|
payload: { error },
|
|
});
|
|
}
|
|
}
|
|
|
|
/**
|
|
* updates the selected theme of the application
|
|
*
|
|
* @param action
|
|
*/
|
|
export function* updateSelectedTheme(
|
|
action: ReduxAction<UpdateSelectedAppThemeAction>,
|
|
) {
|
|
// eslint-disable-next-line
|
|
const { shouldReplay = true, theme, applicationId } = action.payload;
|
|
const canvasWidgets: CanvasWidgetsReduxState = yield select(getCanvasWidgets);
|
|
|
|
try {
|
|
yield ThemingApi.updateTheme(applicationId, theme);
|
|
|
|
yield put({
|
|
type: ReduxActionTypes.UPDATE_SELECTED_APP_THEME_SUCCESS,
|
|
payload: theme,
|
|
});
|
|
|
|
if (shouldReplay) {
|
|
yield put(
|
|
updateReplayEntity(
|
|
"canvas",
|
|
{ widgets: canvasWidgets, theme },
|
|
ENTITY_TYPE.WIDGET,
|
|
),
|
|
);
|
|
}
|
|
} catch (error) {
|
|
yield put({
|
|
type: ReduxActionErrorTypes.UPDATE_SELECTED_APP_THEME_ERROR,
|
|
payload: { error },
|
|
});
|
|
}
|
|
}
|
|
|
|
/**
|
|
* changes eelcted theme
|
|
*
|
|
* @param action
|
|
*/
|
|
export function* changeSelectedTheme(
|
|
action: ReduxAction<ChangeSelectedAppThemeAction>,
|
|
) {
|
|
const { applicationId, shouldReplay = true, theme } = action.payload;
|
|
const canvasWidgets: CanvasWidgetsReduxState = yield select(getCanvasWidgets);
|
|
|
|
try {
|
|
yield ThemingApi.changeTheme(applicationId, theme);
|
|
|
|
yield put({
|
|
type: ReduxActionTypes.CHANGE_SELECTED_APP_THEME_SUCCESS,
|
|
payload: theme,
|
|
});
|
|
|
|
// shows toast
|
|
Toaster.show({
|
|
text: createMessage(CHANGE_APP_THEME, theme.displayName),
|
|
variant: Variant.success,
|
|
actionElement: (
|
|
<span onClick={() => store.dispatch(undoAction())}>Undo</span>
|
|
),
|
|
});
|
|
|
|
if (shouldReplay) {
|
|
yield put(
|
|
updateReplayEntity(
|
|
"canvas",
|
|
{ widgets: canvasWidgets, theme },
|
|
ENTITY_TYPE.WIDGET,
|
|
),
|
|
);
|
|
}
|
|
} catch (error) {
|
|
yield put({
|
|
type: ReduxActionErrorTypes.UPDATE_SELECTED_APP_THEME_ERROR,
|
|
payload: { error },
|
|
});
|
|
}
|
|
}
|
|
|
|
/**
|
|
* save and create new theme from selected theme
|
|
*
|
|
* @param action
|
|
*/
|
|
export function* saveSelectedTheme(action: ReduxAction<SaveAppThemeAction>) {
|
|
const { applicationId, name } = action.payload;
|
|
|
|
try {
|
|
const response: ApiResponse<AppTheme[]> = yield ThemingApi.saveTheme(
|
|
applicationId,
|
|
{ name },
|
|
);
|
|
|
|
yield put({
|
|
type: ReduxActionTypes.SAVE_APP_THEME_SUCCESS,
|
|
payload: response.data,
|
|
});
|
|
|
|
// shows toast
|
|
Toaster.show({
|
|
text: createMessage(SAVE_APP_THEME, name),
|
|
variant: Variant.success,
|
|
});
|
|
} catch (error) {
|
|
yield put({
|
|
type: ReduxActionErrorTypes.SAVE_APP_THEME_ERROR,
|
|
payload: { error },
|
|
});
|
|
}
|
|
}
|
|
|
|
/**
|
|
* deletes custom saved theme
|
|
*
|
|
* @param action
|
|
*/
|
|
export function* deleteTheme(action: ReduxAction<DeleteAppThemeAction>) {
|
|
const { name, themeId } = action.payload;
|
|
|
|
try {
|
|
yield ThemingApi.deleteTheme(themeId);
|
|
|
|
yield put({
|
|
type: ReduxActionTypes.DELETE_APP_THEME_SUCCESS,
|
|
payload: { themeId },
|
|
});
|
|
|
|
// shows toast
|
|
Toaster.show({
|
|
text: createMessage(DELETE_APP_THEME, name),
|
|
variant: Variant.success,
|
|
});
|
|
} catch (error) {
|
|
yield put({
|
|
type: ReduxActionErrorTypes.DELETE_APP_THEME_ERROR,
|
|
payload: { error },
|
|
});
|
|
}
|
|
}
|
|
|
|
function* closeisBetaCardShown() {
|
|
try {
|
|
const user: User = yield select(getCurrentUser);
|
|
const { email } = user;
|
|
if (email) {
|
|
yield setBetaFlag(email, STORAGE_KEYS.APP_THEMING_BETA_SHOWN, true);
|
|
}
|
|
} catch (error) {}
|
|
}
|
|
|
|
/**
|
|
* resets widget styles
|
|
*/
|
|
function* resetTheme() {
|
|
try {
|
|
const canvasWidgets: CanvasWidgetsReduxState = yield select(
|
|
getCanvasWidgets,
|
|
);
|
|
const propertiesToUpdate: UpdateWidgetPropertyPayload[] =
|
|
getPropertiesToUpdateForReset(canvasWidgets);
|
|
|
|
if (propertiesToUpdate.length) {
|
|
yield put(batchUpdateMultipleWidgetProperties(propertiesToUpdate));
|
|
}
|
|
} catch (error) {}
|
|
}
|
|
|
|
/**
|
|
* sets the selectedTheme to default theme when Selected Theme API fails
|
|
*/
|
|
function* setDefaultSelectedThemeOnError() {
|
|
const applicationId: string = yield select(getCurrentApplicationId);
|
|
try {
|
|
// Fetch all system themes
|
|
const response: ApiResponse<AppTheme[]> = yield ThemingApi.fetchThemes(
|
|
applicationId,
|
|
);
|
|
|
|
// Gets default theme
|
|
const theme = find(response.data, { name: "Default" });
|
|
|
|
if (theme) {
|
|
// Update API call to set current theme to default
|
|
yield ThemingApi.changeTheme(applicationId, theme);
|
|
yield put({
|
|
type: ReduxActionTypes.FETCH_SELECTED_APP_THEME_SUCCESS,
|
|
payload: theme,
|
|
});
|
|
// shows toast
|
|
Toaster.show({
|
|
text: createMessage(SET_DEFAULT_SELECTED_THEME, theme.displayName),
|
|
variant: Variant.warning,
|
|
});
|
|
}
|
|
} catch (error) {
|
|
yield put({
|
|
type: ReduxActionErrorTypes.SET_DEFAULT_SELECTED_THEME_ERROR,
|
|
payload: { error },
|
|
});
|
|
}
|
|
}
|
|
export default function* appThemingSaga() {
|
|
yield all([takeLatest(ReduxActionTypes.INITIALIZE_EDITOR, initAppTheming)]);
|
|
yield all([
|
|
takeLatest(ReduxActionTypes.FETCH_APP_THEMES_INIT, fetchAppThemes),
|
|
takeLatest(ReduxActionTypes.RESET_APP_THEME_INIT, resetTheme),
|
|
takeLatest(
|
|
ReduxActionTypes.FETCH_SELECTED_APP_THEME_INIT,
|
|
fetchAppSelectedTheme,
|
|
),
|
|
takeLatest(
|
|
ReduxActionTypes.UPDATE_SELECTED_APP_THEME_INIT,
|
|
updateSelectedTheme,
|
|
),
|
|
takeLatest(
|
|
ReduxActionTypes.CHANGE_SELECTED_APP_THEME_INIT,
|
|
changeSelectedTheme,
|
|
),
|
|
takeLatest(ReduxActionTypes.SAVE_APP_THEME_INIT, saveSelectedTheme),
|
|
takeLatest(ReduxActionTypes.DELETE_APP_THEME_INIT, deleteTheme),
|
|
takeLatest(ReduxActionTypes.CLOSE_BETA_CARD_SHOWN, closeisBetaCardShown),
|
|
takeLatest(
|
|
ReduxActionTypes.SET_DEFAULT_SELECTED_THEME_INIT,
|
|
setDefaultSelectedThemeOnError,
|
|
),
|
|
]);
|
|
}
|