Skip to content

Commit

Permalink
[MM-56902] Minor selector improvements (#26372)
Browse files Browse the repository at this point in the history
* minor selector improvements
  • Loading branch information
BenCookie95 committed May 8, 2024
1 parent 5d4ad44 commit f111db5
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 30 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.

import React from 'react';
import React, {useMemo} from 'react';
import {FormattedMessage} from 'react-intl';
import {useSelector, useDispatch} from 'react-redux';

Expand Down Expand Up @@ -34,7 +34,7 @@ import AnnouncementBar from '../default_announcement_bar';
const CloudTrialEndAnnouncementBar: React.FC = () => {
const limits = useGetLimits();
const subscription = useGetSubscription();
const getCategory = makeGetCategory();
const getCategory = useMemo(makeGetCategory, []);
const dispatch = useDispatch();
const preferences = useSelector((state: GlobalState) =>
getCategory(state, Preferences.CLOUD_TRIAL_END_BANNER),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.

import {useEffect} from 'react';
import {useEffect, useMemo} from 'react';
import {useSelector, useDispatch} from 'react-redux';

import type {PreferenceType} from '@mattermost/types/preferences';
Expand Down Expand Up @@ -29,9 +29,9 @@ import type {GlobalState} from 'types/store';
const ShowStartTrialModal = () => {
const isUserAdmin = useSelector((state: GlobalState) => isCurrentUserSystemAdmin(state));
const openStartTrialFormModal = useOpenStartTrialFormModal();
const getCategory = useMemo(makeGetCategory, []);

const dispatch = useDispatch();
const getCategory = makeGetCategory();

const userThreshold = 10;
const TRUE = 'true';
Expand Down
4 changes: 2 additions & 2 deletions webapp/channels/src/components/forward_post_modal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// See LICENSE.txt for license information.

import classNames from 'classnames';
import React, {useCallback, useRef, useState} from 'react';
import React, {useCallback, useRef, useState, useMemo} from 'react';
import {FormattedList, FormattedMessage, useIntl} from 'react-intl';
import {useDispatch, useSelector} from 'react-redux';
import type {ValueType} from 'react-select';
Expand Down Expand Up @@ -50,7 +50,7 @@ const ForwardPostModal = ({onExited, post}: Props) => {
const {formatMessage} = useIntl();
const dispatch = useDispatch();

const getChannel = makeGetChannel();
const getChannel = useMemo(makeGetChannel, []);

const channel = useSelector((state: GlobalState) => getChannel(state, {id: post.channel_id}));
const currentTeam = useSelector(getCurrentTeam);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.

import React from 'react';
import React, {useMemo} from 'react';
import {FormattedMessage} from 'react-intl';
import {useDispatch, useSelector} from 'react-redux';

Expand Down Expand Up @@ -42,7 +42,7 @@ const OverageUsersBannerNotice = () => {
const isGovSku = getIsGovSku(license);
const seatsPurchased = parseInt(license.Users, 10);
const isCloud = useSelector(isCurrentLicenseCloud);
const getPreferencesCategory = makeGetCategory();
const getPreferencesCategory = useMemo(makeGetCategory, []);
const currentUser = useSelector((state: GlobalState) => getCurrentUser(state));
const overagePreferences = useSelector((state: GlobalState) => getPreferencesCategory(state, Preferences.OVERAGE_USERS_BANNER));
const activeUsers = ((stats || {})[StatTypes.TOTAL_USERS]) as number || 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ function makeMapStateToProps() {
return (state: GlobalState, ownProps: OwnProps) => {
const member = getMyChannelMemberships(state)[ownProps.channel.id];
const unreadCount = getUnreadCount(state, ownProps.channel.id);
const firstChannelName = getFirstChannelName(state);
const config = getConfig(state);
const enableTutorial = config.EnableTutorial === 'true';
const currentUserId = getCurrentUserId(state);
Expand All @@ -58,7 +57,7 @@ function makeMapStateToProps() {
isMuted: isChannelMuted(member),
hasUrgent: unreadCount.hasUrgent,
isChannelSelected: isChannelSelected(state, ownProps.channel.id),
firstChannelName: showChannelsTutorialStep ? firstChannelName : '',
firstChannelName: showChannelsTutorialStep ? getFirstChannelName(state) : '',
showChannelsTutorialStep,
rhsState: getRhsState(state),
rhsOpen: getIsRhsOpen(state),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -684,16 +684,12 @@ export const getTeamsUnreadStatuses: (state: GlobalState) => [Set<Team['id']>, M
getAllChannels,
getMyChannelMemberships,
getChannelMessageCounts,
getUsers,
getCurrentUserId,
isCollapsedThreadsEnabled,
getThreadCounts,
(
channels,
channelMemberships,
channelMessageCounts,
users,
currentUserId,
collapsedThreadsEnabled,
teamThreadCounts,
) => {
Expand Down
8 changes: 3 additions & 5 deletions webapp/channels/src/selectors/drafts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,14 @@ export function makeGetDraftsByPrefix(prefix: string): DraftSelector {
);
}

const getChannelDrafts = makeGetDraftsByPrefix(StoragePrefixes.DRAFT);
const getRHSDrafts = makeGetDraftsByPrefix(StoragePrefixes.COMMENT_DRAFT);

/**
* Gets all local drafts in storage.
* @param excludeInactive determines if we filter drafts based on active channels.
*/
export function makeGetDrafts(excludeInactive = true): DraftSelector {
const getChannelDrafts = makeGetDraftsByPrefix(StoragePrefixes.DRAFT);
const getRHSDrafts = makeGetDraftsByPrefix(StoragePrefixes.COMMENT_DRAFT);

return createSelector(
'makeGetDrafts',
getChannelDrafts,
Expand All @@ -93,8 +93,6 @@ export function makeGetDrafts(excludeInactive = true): DraftSelector {
}

export function makeGetDraftsCount(): DraftCountSelector {
const getChannelDrafts = makeGetDraftsByPrefix(StoragePrefixes.DRAFT);
const getRHSDrafts = makeGetDraftsByPrefix(StoragePrefixes.COMMENT_DRAFT);
return createSelector(
'makeGetDraftsCount',
getChannelDrafts,
Expand Down
23 changes: 12 additions & 11 deletions webapp/channels/src/selectors/onboarding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import {RecommendedNextStepsLegacy, Preferences} from 'utils/constants';

import type {GlobalState} from 'types/store';

const getCategory = makeGetCategory();
const getABTestCategory = makeGetCategory();
export const getABTestPreferences = (() => {
return (state: GlobalState) => getCategory(state, Preferences.AB_TEST_PREFERENCE_VALUE);
return (state: GlobalState) => getABTestCategory(state, Preferences.AB_TEST_PREFERENCE_VALUE);
})();

const getFirstChannelNamePref = createSelector(
Expand Down Expand Up @@ -91,10 +91,13 @@ const getSteps = createSelector(
},
);

const getNextStepsCategory = makeGetCategory();
const getOnboardingTaskCategory = makeGetCategory();

// Loop through all Steps. For each step, check that
export const legacyNextStepsNotFinished = createSelector(
'legacyNextStepsNotFinished',
(state: GlobalState) => getCategory(state, Preferences.RECOMMENDED_NEXT_STEPS),
(state: GlobalState) => getNextStepsCategory(state, Preferences.RECOMMENDED_NEXT_STEPS),
(state: GlobalState) => getCurrentUser(state),
(state: GlobalState) => isFirstAdmin(state),
(state: GlobalState) => getSteps(state),
Expand All @@ -108,7 +111,7 @@ export const legacyNextStepsNotFinished = createSelector(
// Loop through all Steps. For each step, check that
export const hasLegacyNextStepsPreferences = createSelector(
'hasLegacyNextStepsPreferences',
(state: GlobalState) => getCategory(state, Preferences.RECOMMENDED_NEXT_STEPS),
(state: GlobalState) => getNextStepsCategory(state, Preferences.RECOMMENDED_NEXT_STEPS),
(state: GlobalState) => getSteps(state),
(stepPreferences, mySteps) => {
const checkPref = (step: StepType) => stepPreferences.some((pref) => (pref.name === step.id));
Expand All @@ -118,18 +121,17 @@ export const hasLegacyNextStepsPreferences = createSelector(

export const getShowTaskListBool = createSelector(
'getShowTaskListBool',
(state: GlobalState) => state,
(state: GlobalState) => getCategory(state, OnboardingTaskCategory),
(state: GlobalState) => getCategory(state, Preferences.RECOMMENDED_NEXT_STEPS),
(state: GlobalState) => getOnboardingTaskCategory(state, OnboardingTaskCategory),
(state: GlobalState) => getNextStepsCategory(state, Preferences.RECOMMENDED_NEXT_STEPS),
getIsMobileView,
(state, onboardingPreferences, legacyStepsPreferences, isMobileView) => {
(state: GlobalState) => getBool(state, OnboardingTaskCategory, OnboardingTaskList.ONBOARDING_TASK_LIST_SHOW),
(state: GlobalState) => hasLegacyNextStepsPreferences(state),
(onboardingPreferences, legacyStepsPreferences, isMobileView, taskListStatus, hasAnyOfTheLegacyStepsPreferences) => {
// conditions to validate scenario where users (initially first_admins) had already set any of the onboarding task list preferences values.
// We check wether the preference value exists meaning the onboarding tasks list already started no matter what the state of the process is
const hasUserStartedOnboardingTaskListProcess = onboardingPreferences?.some((pref) =>
pref.name === OnboardingTaskList.ONBOARDING_TASK_LIST_SHOW || pref.name === OnboardingTaskList.ONBOARDING_TASK_LIST_OPEN);

const taskListStatus = getBool(state, OnboardingTaskCategory, OnboardingTaskList.ONBOARDING_TASK_LIST_SHOW);

if (hasUserStartedOnboardingTaskListProcess) {
return [(taskListStatus && !isMobileView), false];
}
Expand All @@ -143,7 +145,6 @@ export const getShowTaskListBool = createSelector(
// This condition verifies existing users hasn't finished nor skipped legacy next steps or there are still steps not completed
const hasSkipLegacyStepsPreference = legacyStepsPreferences.some((pref) => (pref.name === RecommendedNextStepsLegacy.SKIP));
const hideLegacyStepsSetToFalse = legacyStepsPreferences.some((pref) => (pref.name === RecommendedNextStepsLegacy.HIDE && pref.value === 'false'));
const hasAnyOfTheLegacyStepsPreferences = hasLegacyNextStepsPreferences(state);
const areFirstUserPrefs = !hasSkipLegacyStepsPreference && hideLegacyStepsSetToFalse && !hasAnyOfTheLegacyStepsPreferences;

const completelyNewUserForOnboarding = !hasUserStartedOnboardingTaskListProcess && areFirstUserPrefs;
Expand Down

0 comments on commit f111db5

Please sign in to comment.