signal-desktop/ts/state/selectors/items.ts

142 lines
4.2 KiB
TypeScript
Raw Normal View History

2022-01-26 23:05:26 +00:00
// Copyright 2019-2022 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import { createSelector } from 'reselect';
import { isInteger } from 'lodash';
2021-06-01 20:45:43 +00:00
import { ITEM_NAME as UNIVERSAL_EXPIRE_TIMER_ITEM } from '../../util/universalExpireTimer';
2022-01-26 23:05:26 +00:00
import type { ConfigKeyType, ConfigMapType } from '../../RemoteConfig';
2021-06-01 20:45:43 +00:00
import type { StateType } from '../reducer';
import type { ItemsStateType } from '../ducks/items';
import type {
2021-06-02 21:05:09 +00:00
ConversationColorType,
CustomColorType,
} from '../../types/Colors';
import { DEFAULT_CONVERSATION_COLOR } from '../../types/Colors';
import { getPreferredReactionEmoji as getPreferredReactionEmojiFromStoredValue } from '../../reactions/preferredReactionEmoji';
import { isBeta } from '../../util/version';
const DEFAULT_PREFERRED_LEFT_PANE_WIDTH = 320;
export const getItems = (state: StateType): ItemsStateType => state.items;
2022-05-06 19:02:44 +00:00
export const getHasAllStoriesMuted = createSelector(
getItems,
({ hasAllStoriesMuted }): boolean => Boolean(hasAllStoriesMuted)
);
export const getAreWeASubscriber = createSelector(
getItems,
({ areWeASubscriber }: Readonly<ItemsStateType>): boolean =>
Boolean(areWeASubscriber)
);
export const getUserAgent = createSelector(
getItems,
(state: ItemsStateType): string => state.userAgent as string
);
export const getPinnedConversationIds = createSelector(
getItems,
(state: ItemsStateType): Array<string> =>
(state.pinnedConversationIds || []) as Array<string>
);
2021-06-01 20:45:43 +00:00
export const getUniversalExpireTimer = createSelector(
getItems,
(state: ItemsStateType): number => state[UNIVERSAL_EXPIRE_TIMER_ITEM] || 0
);
2021-06-02 21:05:09 +00:00
2022-01-26 23:05:26 +00:00
const isRemoteConfigFlagEnabled = (
config: Readonly<ConfigMapType>,
key: ConfigKeyType
): boolean => Boolean(config[key]?.enabled);
const getRemoteConfig = createSelector(
getItems,
2022-01-26 23:05:26 +00:00
(state: ItemsStateType): ConfigMapType => state.remoteConfig || {}
);
export const getUsernamesEnabled = createSelector(
getRemoteConfig,
2022-01-26 23:05:26 +00:00
(remoteConfig: ConfigMapType): boolean =>
isRemoteConfigFlagEnabled(remoteConfig, 'desktop.usernames')
);
// Note: types/Stories is the other place this check is done
2022-03-04 21:14:52 +00:00
export const getStoriesEnabled = createSelector(
2022-07-20 00:47:05 +00:00
getItems,
2022-03-04 21:14:52 +00:00
getRemoteConfig,
2022-07-20 00:47:05 +00:00
(state: ItemsStateType, remoteConfig: ConfigMapType): boolean =>
!state.hasStoriesDisabled &&
2022-07-20 00:47:05 +00:00
(isRemoteConfigFlagEnabled(remoteConfig, 'desktop.internalUser') ||
isRemoteConfigFlagEnabled(remoteConfig, 'desktop.stories') ||
(isRemoteConfigFlagEnabled(remoteConfig, 'desktop.stories.beta') &&
isBeta(window.getVersion())))
2022-03-04 21:14:52 +00:00
);
2021-06-02 21:05:09 +00:00
export const getDefaultConversationColor = createSelector(
getItems,
(
state: ItemsStateType
): {
color: ConversationColorType;
customColorData?: {
id: string;
value: CustomColorType;
};
} => state.defaultConversationColor ?? DEFAULT_CONVERSATION_COLOR
2021-06-02 21:05:09 +00:00
);
2021-08-18 20:08:14 +00:00
export const getCustomColors = createSelector(
getItems,
(state: ItemsStateType): Record<string, CustomColorType> | undefined =>
state.customColors?.colors
);
export const getEmojiSkinTone = createSelector(
getItems,
({ skinTone }: Readonly<ItemsStateType>): number =>
typeof skinTone === 'number' &&
isInteger(skinTone) &&
skinTone >= 0 &&
skinTone <= 5
? skinTone
: 0
);
export const getPreferredLeftPaneWidth = createSelector(
getItems,
({ preferredLeftPaneWidth }: Readonly<ItemsStateType>): number =>
typeof preferredLeftPaneWidth === 'number' &&
Number.isInteger(preferredLeftPaneWidth)
? preferredLeftPaneWidth
: DEFAULT_PREFERRED_LEFT_PANE_WIDTH
);
export const getPreferredReactionEmoji = createSelector(
getItems,
getEmojiSkinTone,
(state: Readonly<ItemsStateType>, skinTone: number): Array<string> =>
getPreferredReactionEmojiFromStoredValue(
state.preferredReactionEmoji,
skinTone
)
);
export const getHideMenuBar = createSelector(
getItems,
(state: ItemsStateType): boolean => Boolean(state['hide-menu-bar'])
);
2022-08-23 17:24:55 +00:00
export const getHasSetMyStoriesPrivacy = createSelector(
getItems,
(state: ItemsStateType): boolean => Boolean(state.hasSetMyStoriesPrivacy)
);
export const getHasReadReceiptSetting = createSelector(
getItems,
(state: ItemsStateType): boolean => Boolean(state['read-receipt-setting'])
);