Skip to content

Commit

Permalink
Improve parallelization of loadMeAndConfig action (mattermost#27858)
Browse files Browse the repository at this point in the history
1. Rewrite of loadConfigAndMe function which includes compatible return types along with less blocking redux actions
2. Actions of webapp/channels/src/components/root/actions.ts moved to webapp/channels/src/actions/views/root.ts for consistency
3. Removes 2 redundant network requests for config and license.
  • Loading branch information
M-ZubairAhmed authored Aug 26, 2024
1 parent daff7a3 commit 15c9b15
Show file tree
Hide file tree
Showing 17 changed files with 507 additions and 564 deletions.
36 changes: 0 additions & 36 deletions webapp/channels/src/actions/views/root.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,6 @@ import * as i18nSelectors from 'selectors/i18n';
import mockStore from 'tests/test_store';
import {ActionTypes} from 'utils/constants';

jest.mock('mattermost-redux/actions/general', () => {
const original = jest.requireActual('mattermost-redux/actions/general');
return {
...original,
getClientConfig: () => ({type: 'MOCK_GET_CLIENT_CONFIG'}),
getLicenseConfig: () => ({type: 'MOCK_GET_LICENSE_CONFIG'}),
};
});

jest.mock('mattermost-redux/actions/users', () => {
const original = jest.requireActual('mattermost-redux/actions/users');
return {
...original,
loadMe: () => ({type: 'MOCK_LOAD_ME'}),
};
});

describe('root view actions', () => {
const origCookies = document.cookie;
const origWasLoggedIn = localStorage.getItem('was_logged_in');
Expand All @@ -38,25 +21,6 @@ describe('root view actions', () => {
localStorage.setItem('was_logged_in', origWasLoggedIn || '');
});

describe('loadConfigAndMe', () => {
test('loadConfigAndMe, without user logged in', async () => {
const testStore = mockStore({});

await testStore.dispatch(Actions.loadConfigAndMe());
expect(testStore.getActions()).toEqual([{type: 'MOCK_GET_CLIENT_CONFIG'}, {type: 'MOCK_GET_LICENSE_CONFIG'}]);
});

test('loadConfigAndMe, with user logged in', async () => {
const testStore = mockStore({});

document.cookie = 'MMUSERID=userid';
localStorage.setItem('was_logged_in', 'true');

await testStore.dispatch(Actions.loadConfigAndMe());
expect(testStore.getActions()).toEqual([{type: 'MOCK_GET_CLIENT_CONFIG'}, {type: 'MOCK_GET_LICENSE_CONFIG'}, {type: 'MOCK_LOAD_ME'}]);
});
});

describe('registerPluginTranslationsSource', () => {
test('Should not dispatch action when getTranslation is empty', () => {
const testStore = mockStore({});
Expand Down
40 changes: 0 additions & 40 deletions webapp/channels/src/actions/views/root.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.

import type {ClientConfig} from '@mattermost/types/config';

import {getClientConfig, getLicenseConfig} from 'mattermost-redux/actions/general';
import {loadMe} from 'mattermost-redux/actions/users';
import {Client4} from 'mattermost-redux/client';
import type {ActionFuncAsync, ThunkActionFunc} from 'mattermost-redux/types/actions';

Expand All @@ -20,26 +16,6 @@ const pluginTranslationSources: Record<string, TranslationPluginFunction> = {};

export type TranslationPluginFunction = (locale: string) => Translations

export function loadConfigAndMe(): ThunkActionFunc<Promise<{config?: ClientConfig; isMeLoaded: boolean}>> {
return async (dispatch) => {
const results = await Promise.all([
dispatch(getClientConfig()),
dispatch(getLicenseConfig()),
]);

let isMeLoaded = false;
if (document.cookie.includes('MMUSERID=')) {
const dataFromLoadMe = await dispatch(loadMe());
isMeLoaded = dataFromLoadMe?.data ?? false;
}

return {
config: results[0].data,
isMeLoaded,
};
};
}

export function registerPluginTranslationsSource(pluginId: string, sourceFunction: TranslationPluginFunction): ThunkActionFunc<void, GlobalState> {
pluginTranslationSources[pluginId] = sourceFunction;
return (dispatch, getState) => {
Expand Down Expand Up @@ -91,19 +67,3 @@ export function loadTranslations(locale: string, url: string): ActionFuncAsync {
return {data: true};
};
}

export function registerCustomPostRenderer(type: string, component: any, id: string): ActionFuncAsync {
return async (dispatch) => {
// piggyback on plugins state to register a custom post renderer
dispatch({
type: ActionTypes.RECEIVED_PLUGIN_POST_COMPONENT,
data: {
postTypeId: id,
pluginId: id,
type,
component,
},
});
return {data: true};
};
}
9 changes: 5 additions & 4 deletions webapp/channels/src/components/logged_in/logged_in.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ import Constants from 'utils/constants';
import DesktopApp from 'utils/desktop_api';
import {isKeyPressed} from 'utils/keyboard';
import {getBrowserTimezone} from 'utils/timezone';
import * as UserAgent from 'utils/user_agent';
import {isAndroid, isIos} from 'utils/user_agent';
import {doesCookieContainsMMUserId} from 'utils/utils';

declare global {
interface Window {
Expand Down Expand Up @@ -89,9 +90,9 @@ export default class LoggedIn extends React.PureComponent<Props> {
};

// Device tracking setup
if (UserAgent.isIos()) {
if (isIos()) {
document.body.classList.add('ios');
} else if (UserAgent.isAndroid()) {
} else if (isAndroid()) {
document.body.classList.add('android');
}

Expand Down Expand Up @@ -202,7 +203,7 @@ export default class LoggedIn extends React.PureComponent<Props> {
private handleBeforeUnload = (): void => {
// remove the event listener to prevent getting stuck in a loop
window.removeEventListener('beforeunload', this.handleBeforeUnload);
if (document.cookie.indexOf('MMUSERID=') > -1 && this.props.currentChannelId && !this.props.isCurrentChannelManuallyUnread) {
if (doesCookieContainsMMUserId() && this.props.currentChannelId && !this.props.isCurrentChannelManuallyUnread) {
this.props.actions.updateApproximateViewTime(this.props.currentChannelId);
}
WebSocketActions.close();
Expand Down
139 changes: 0 additions & 139 deletions webapp/channels/src/components/root/__snapshots__/root.test.tsx.snap

This file was deleted.

91 changes: 0 additions & 91 deletions webapp/channels/src/components/root/actions.ts

This file was deleted.

Loading

0 comments on commit 15c9b15

Please sign in to comment.