UNPKG

mattermost-redux

Version:

Common code (API client, Redux stores, logic, utility functions) for building a Mattermost client

191 lines 10 kB
"use strict"; // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. // See LICENSE.txt for license information. Object.defineProperty(exports, "__esModule", { value: true }); exports.shouldAutocloseDMs = exports.shouldShowUnreadsCategory = exports.getSidebarPreferences = exports.makeGetStyleFromTheme = exports.getTheme = exports.getTeammateNameDisplaySetting = exports.getVisibleGroupIds = exports.getVisibleTeammate = exports.getFavoritesPreferences = exports.getGroupShowPreferences = exports.getDirectShowPreferences = exports.makeGetCategory = exports.getInt = exports.getBool = exports.get = exports.getMyPreferences = void 0; var tslib_1 = require("tslib"); var reselect_1 = require("reselect"); var constants_1 = require("../../constants"); var general_1 = require("./general"); var teams_1 = require("./teams"); var helpers_1 = require("../../utils/helpers"); var preference_utils_1 = require("../../utils/preference_utils"); function getMyPreferences(state) { return state.entities.preferences.myPreferences; } exports.getMyPreferences = getMyPreferences; function get(state, category, name, defaultValue) { if (defaultValue === void 0) { defaultValue = ''; } var key = preference_utils_1.getPreferenceKey(category, name); var prefs = getMyPreferences(state); if (!(key in prefs)) { return defaultValue; } return prefs[key].value; } exports.get = get; function getBool(state, category, name, defaultValue) { if (defaultValue === void 0) { defaultValue = false; } var value = get(state, category, name, String(defaultValue)); return value !== 'false'; } exports.getBool = getBool; function getInt(state, category, name, defaultValue) { if (defaultValue === void 0) { defaultValue = 0; } var value = get(state, category, name, defaultValue); return parseInt(value, 10); } exports.getInt = getInt; function makeGetCategory() { return reselect_1.createSelector(getMyPreferences, function (state, category) { return category; }, function (preferences, category) { var prefix = category + '--'; var prefsInCategory = []; for (var key in preferences) { if (key.startsWith(prefix)) { prefsInCategory.push(preferences[key]); } } return prefsInCategory; }); } exports.makeGetCategory = makeGetCategory; var getDirectShowCategory = makeGetCategory(); function getDirectShowPreferences(state) { return getDirectShowCategory(state, constants_1.Preferences.CATEGORY_DIRECT_CHANNEL_SHOW); } exports.getDirectShowPreferences = getDirectShowPreferences; var getGroupShowCategory = makeGetCategory(); function getGroupShowPreferences(state) { return getGroupShowCategory(state, constants_1.Preferences.CATEGORY_GROUP_CHANNEL_SHOW); } exports.getGroupShowPreferences = getGroupShowPreferences; var getFavoritesCategory = makeGetCategory(); function getFavoritesPreferences(state) { var favorites = getFavoritesCategory(state, constants_1.Preferences.CATEGORY_FAVORITE_CHANNEL); return favorites.filter(function (f) { return f.value === 'true'; }).map(function (f) { return f.name; }); } exports.getFavoritesPreferences = getFavoritesPreferences; exports.getVisibleTeammate = reselect_1.createSelector(getDirectShowPreferences, function (direct) { return direct.filter(function (dm) { return dm.value === 'true' && dm.name; }).map(function (dm) { return dm.name; }); }); exports.getVisibleGroupIds = reselect_1.createSelector(getGroupShowPreferences, function (groups) { return groups.filter(function (dm) { return dm.value === 'true' && dm.name; }).map(function (dm) { return dm.name; }); }); exports.getTeammateNameDisplaySetting = reselect_1.createSelector(general_1.getConfig, getMyPreferences, general_1.getLicense, function (config, preferences, license) { var useAdminTeammateNameDisplaySetting = (license && license.LockTeammateNameDisplay === 'true') && config.LockTeammateNameDisplay === 'true'; var key = preference_utils_1.getPreferenceKey(constants_1.Preferences.CATEGORY_DISPLAY_SETTINGS, constants_1.Preferences.NAME_NAME_FORMAT); if (preferences[key] && !useAdminTeammateNameDisplaySetting) { return preferences[key].value; } else if (config.TeammateNameDisplay) { return config.TeammateNameDisplay; } return constants_1.General.TEAMMATE_NAME_DISPLAY.SHOW_USERNAME; }); var getThemePreference = reselect_1.createSelector(getMyPreferences, teams_1.getCurrentTeamId, function (myPreferences, currentTeamId) { // Prefer the user's current team-specific theme over the user's current global theme var themePreference; if (currentTeamId) { themePreference = myPreferences[preference_utils_1.getPreferenceKey(constants_1.Preferences.CATEGORY_THEME, currentTeamId)]; } if (!themePreference) { themePreference = myPreferences[preference_utils_1.getPreferenceKey(constants_1.Preferences.CATEGORY_THEME, '')]; } return themePreference; }); var getDefaultTheme = reselect_1.createSelector(general_1.getConfig, function (config) { if (config.DefaultTheme && config.DefaultTheme in constants_1.Preferences.THEMES) { var theme = constants_1.Preferences.THEMES[config.DefaultTheme]; if (theme) { return theme; } } // If no config.DefaultTheme or value doesn't refer to a valid theme name... return constants_1.Preferences.THEMES.default; }); exports.getTheme = helpers_1.createShallowSelector(getThemePreference, getDefaultTheme, function (themePreference, defaultTheme) { var e_1, _a; var _b, _c; var themeValue = (_b = themePreference === null || themePreference === void 0 ? void 0 : themePreference.value) !== null && _b !== void 0 ? _b : defaultTheme; // A custom theme will be a JSON-serialized object stored in a preference // At this point, the theme should be a plain object var theme = typeof themeValue === 'string' ? JSON.parse(themeValue) : themeValue; // If this is a system theme, find it in case the user's theme is missing any fields if (theme.type && theme.type !== 'custom') { var match = Object.values(constants_1.Preferences.THEMES).find(function (v) { return v.type === theme.type; }); if (match) { if (!match.mentionBg) { match.mentionBg = match.mentionBj; } return match; } } try { for (var _d = tslib_1.__values(Object.keys(defaultTheme)), _e = _d.next(); !_e.done; _e = _d.next()) { var key = _e.value; if (theme[key]) { // Fix a case where upper case theme colours are rendered as black theme[key] = (_c = theme[key]) === null || _c === void 0 ? void 0 : _c.toLowerCase(); } } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (_e && !_e.done && (_a = _d.return)) _a.call(_d); } finally { if (e_1) throw e_1.error; } } // Backwards compatability with old name if (!theme.mentionBg) { theme.mentionBg = theme.mentionBj; } return Object.assign({}, defaultTheme, theme); }); function makeGetStyleFromTheme() { return reselect_1.createSelector(exports.getTheme, function (state, getStyleFromTheme) { return getStyleFromTheme; }, function (theme, getStyleFromTheme) { return getStyleFromTheme(theme); }); } exports.makeGetStyleFromTheme = makeGetStyleFromTheme; var defaultSidebarPrefs = { grouping: 'by_type', unreads_at_top: 'true', favorite_at_top: 'true', sorting: 'alpha', }; exports.getSidebarPreferences = reselect_1.createSelector(function (state) { var config = general_1.getConfig(state); return config.ExperimentalGroupUnreadChannels !== constants_1.General.DISABLED && getBool(state, constants_1.Preferences.CATEGORY_SIDEBAR_SETTINGS, 'show_unread_section', config.ExperimentalGroupUnreadChannels === constants_1.General.DEFAULT_ON); }, function (state) { return get(state, constants_1.Preferences.CATEGORY_SIDEBAR_SETTINGS, '', null); }, function (showUnreadSection, sidebarPreference) { var sidebarPrefs = JSON.parse(sidebarPreference); if (sidebarPrefs === null) { // Support unread settings for old implementation sidebarPrefs = tslib_1.__assign(tslib_1.__assign({}, defaultSidebarPrefs), { unreads_at_top: showUnreadSection ? 'true' : 'false' }); } return sidebarPrefs; }); // shouldShowUnreadsCategory returns true if the user has unereads grouped separately with the new sidebar enabled. exports.shouldShowUnreadsCategory = reselect_1.createSelector(function (state) { return get(state, constants_1.Preferences.CATEGORY_SIDEBAR_SETTINGS, constants_1.Preferences.SHOW_UNREAD_SECTION); }, function (state) { return get(state, constants_1.Preferences.CATEGORY_SIDEBAR_SETTINGS, ''); }, function (state) { return general_1.getConfig(state).ExperimentalGroupUnreadChannels; }, function (userPreference, oldUserPreference, serverDefault) { // Prefer the show_unread_section user preference over the previous version if (userPreference) { return userPreference === 'true'; } if (oldUserPreference) { return JSON.parse(oldUserPreference).unreads_at_top === 'true'; } // The user setting is not set, so use the system default return serverDefault === constants_1.General.DEFAULT_ON; }); function shouldAutocloseDMs(state) { var config = general_1.getConfig(state); if (!config.CloseUnusedDirectMessages || config.CloseUnusedDirectMessages === 'false') { return false; } var preference = get(state, constants_1.Preferences.CATEGORY_SIDEBAR_SETTINGS, constants_1.Preferences.CHANNEL_SIDEBAR_AUTOCLOSE_DMS, constants_1.Preferences.AUTOCLOSE_DMS_ENABLED); return preference === constants_1.Preferences.AUTOCLOSE_DMS_ENABLED; } exports.shouldAutocloseDMs = shouldAutocloseDMs; //# sourceMappingURL=preferences.js.map