import type { ApiMessage, ApiPeer, ApiSponsoredMessage } from '../../api/types'; import type { CustomPeer, PerformanceTypeKey, ThemeKey } from '../../types'; import type { GlobalState, TabArgs } from '../types'; import { NewChatMembersProgress, RightColumnContent } from '../../types'; import { IS_SNAP_EFFECT_SUPPORTED } from '../../util/browser/windowEnvironment'; import { getCurrentTabId } from '../../util/establishMultitabRole'; import { getMessageVideo, getWebPageVideo } from '../helpers/messageMedia'; import { selectCurrentManagement } from './management'; import { selectWebPageFromMessage } from './messages'; import { selectSharedSettings } from './sharedState'; import { selectIsStatisticsShown } from './statistics'; import { selectTabState } from './tabs'; export function selectIsMediaViewerOpen( global: T, ...[tabId = getCurrentTabId()]: TabArgs ) { const { mediaViewer: { chatId, messageId, isAvatarView, standaloneMedia, isSponsoredMessage, }, } = selectTabState(global, tabId); return Boolean(standaloneMedia || (chatId && (isAvatarView || messageId || isSponsoredMessage))); } export function selectRightColumnContentKey( global: T, isMobile?: boolean, ...[tabId = getCurrentTabId()]: TabArgs ) { const tabState = selectTabState(global, tabId); return tabState.editTopicPanel ? ( RightColumnContent.EditTopic ) : tabState.createTopicPanel ? ( RightColumnContent.CreateTopic ) : tabState.pollResults.messageId ? ( RightColumnContent.PollResults ) : selectCurrentManagement(global, tabId) ? ( RightColumnContent.Management ) : tabState.isStatisticsShown && tabState.statistics.currentMessageId ? ( RightColumnContent.MessageStatistics ) : tabState.isStatisticsShown && tabState.statistics.currentStoryId ? ( RightColumnContent.StoryStatistics ) : selectIsStatisticsShown(global, tabId) ? ( RightColumnContent.Statistics ) : tabState.boostStatistics ? ( RightColumnContent.BoostStatistics ) : tabState.monetizationStatistics ? ( RightColumnContent.MonetizationStatistics ) : tabState.stickerSearch.query !== undefined ? ( RightColumnContent.StickerSearch ) : tabState.gifSearch.query !== undefined ? ( RightColumnContent.GifSearch ) : tabState.newChatMembersProgress !== NewChatMembersProgress.Closed ? ( RightColumnContent.AddingMembers ) : tabState.chatInfo.isOpen && tabState.messageLists.length ? ( RightColumnContent.ChatInfo ) : undefined; } export function selectIsRightColumnShown( global: T, isMobile?: boolean, ...[tabId = getCurrentTabId()]: TabArgs ) { return selectRightColumnContentKey(global, isMobile, tabId) !== undefined; } export function selectTheme(global: T) { return selectSharedSettings(global).theme; } export function selectThemeValues(global: T, themeKey: ThemeKey) { return global.settings.themes[themeKey]; } export function selectIsForumPanelOpen( global: T, ...[tabId = getCurrentTabId()]: TabArgs ) { const tabState = selectTabState(global, tabId); return Boolean(tabState.forumPanelChatId) && ( tabState.globalSearch.query === undefined || Boolean(tabState.globalSearch.isClosing) ); } export function selectIsForumPanelClosed( global: T, ...[tabId = getCurrentTabId()]: TabArgs ) { return !selectIsForumPanelOpen(global, tabId); } export function selectIsReactionPickerOpen( global: T, ...[tabId = getCurrentTabId()]: TabArgs ) { const { reactionPicker } = selectTabState(global, tabId); return Boolean(reactionPicker?.position); } export function selectPerformanceSettings(global: T) { return selectSharedSettings(global).performance; } export function selectPerformanceSettingsValue( global: T, key: PerformanceTypeKey, ) { return selectPerformanceSettings(global)[key]; } export function selectCanAutoPlayMedia(global: T, message: ApiMessage | ApiSponsoredMessage) { const webPage = selectWebPageFromMessage(global, message); const video = getMessageVideo(message) || getWebPageVideo(webPage); if (!video) { return undefined; } const canAutoPlayVideos = selectPerformanceSettingsValue(global, 'autoplayVideos'); const canAutoPlayGifs = selectPerformanceSettingsValue(global, 'autoplayGifs'); const asGif = video.isGif || video.isRound; return (canAutoPlayVideos && !asGif) || (canAutoPlayGifs && asGif); } export function selectShouldLoopStickers(global: T) { return selectPerformanceSettingsValue(global, 'loopAnimatedStickers'); } export function selectCanPlayAnimatedEmojis(global: T) { return selectPerformanceSettingsValue(global, 'animatedEmoji'); } export function selectCanAnimateInterface(global: T) { return selectPerformanceSettingsValue(global, 'pageTransitions'); } export function selectCanAnimateRightColumn(global: T) { return selectPerformanceSettingsValue(global, 'rightColumnAnimations'); } export function selectCanAnimateSnapEffect(global: T) { return IS_SNAP_EFFECT_SUPPORTED && selectPerformanceSettingsValue(global, 'snapEffect'); } export function selectIsContextMenuTranslucent(global: T) { return selectPerformanceSettingsValue(global, 'contextMenuBlur'); } export function selectIsSynced(global: T) { return global.isSynced; } export function selectWebApp( global: T, key: string, ...[tabId = getCurrentTabId()]: TabArgs ) { return selectTabState(global, tabId).webApps.openedWebApps[key]; } export function selectActiveWebApp( global: T, ...[tabId = getCurrentTabId()]: TabArgs ) { const activeWebAppKey = selectTabState(global, tabId).webApps.activeWebAppKey; if (!activeWebAppKey) return undefined; return selectWebApp(global, activeWebAppKey, tabId); } export function selectLeftColumnContentKey( global: T, ...[tabId = getCurrentTabId()]: TabArgs ) { return selectTabState(global, tabId).leftColumn.contentKey; } export function selectSettingsScreen( global: T, ...[tabId = getCurrentTabId()]: TabArgs ) { return selectTabState(global, tabId).leftColumn.settingsScreen; } export function selectPeerProfileColor(global: T, peer: ApiPeer | CustomPeer) { const key = 'isCustomPeer' in peer ? peer.peerColorId : peer.profileColor?.color; if (!key) return undefined; return global.peerColors?.profile?.[key]; }