import React, { FC, useCallback, useMemo, memo, } from '../../../lib/teact/teact'; import { getDispatch, withGlobal } from '../../../lib/teact/teactn'; import { LeftColumnContent, ISettings } from '../../../types'; import { ApiChat } from '../../../api/types'; import { ANIMATION_LEVEL_MAX, APP_NAME, APP_VERSION, FEEDBACK_URL, } from '../../../config'; import { IS_SINGLE_COLUMN_LAYOUT } from '../../../util/environment'; import buildClassName from '../../../util/buildClassName'; import { formatDateToString } from '../../../util/dateFormat'; import switchTheme from '../../../util/switchTheme'; import { setPermanentWebVersion } from '../../../util/permanentWebVersion'; import { selectTheme } from '../../../modules/selectors'; import { isChatArchived } from '../../../modules/helpers'; import useLang from '../../../hooks/useLang'; import { disableHistoryBack } from '../../../hooks/useHistoryBack'; import DropdownMenu from '../../ui/DropdownMenu'; import MenuItem from '../../ui/MenuItem'; import Button from '../../ui/Button'; import SearchInput from '../../ui/SearchInput'; import PickerSelectedItem from '../../common/PickerSelectedItem'; import Switcher from '../../ui/Switcher'; import './LeftMainHeader.scss'; type OwnProps = { content: LeftColumnContent; contactsFilter: string; shouldSkipTransition?: boolean; onSearchQuery: (query: string) => void; onSelectSettings: () => void; onSelectContacts: () => void; onSelectArchived: () => void; onReset: () => void; }; type StateProps = { searchQuery?: string; isLoading: boolean; currentUserId?: string; globalSearchChatId?: string; searchDate?: number; theme: ISettings['theme']; animationLevel: 0 | 1 | 2; chatsById?: Record; }; const ANIMATION_LEVEL_OPTIONS = [0, 1, 2]; const PRODUCTION_HOSTNAME = 'web.telegram.org'; const LEGACY_VERSION_URL = 'https://web.telegram.org/?legacy=1'; const WEBK_VERSION_URL = 'https://web.telegram.org/k/'; const LeftMainHeader: FC = ({ content, contactsFilter, onSearchQuery, onSelectSettings, onSelectContacts, onSelectArchived, onReset, searchQuery, isLoading, shouldSkipTransition, currentUserId, globalSearchChatId, searchDate, theme, animationLevel, chatsById, }) => { const { openChat, openTipsChat, setGlobalSearchDate, setSettingOption, setGlobalSearchChatId, } = getDispatch(); const lang = useLang(); const hasMenu = content === LeftColumnContent.ChatList; const clearedDateSearchParam = { date: undefined }; const clearedChatSearchParam = { id: undefined }; const selectedSearchDate = useMemo(() => { return searchDate ? formatDateToString(new Date(searchDate * 1000)) : undefined; }, [searchDate]); const archivedUnreadChatsCount = useMemo(() => { if (!hasMenu || !chatsById) { return 0; } return Object.values(chatsById).reduce((total, chat) => { if (!isChatArchived(chat)) { return total; } return chat.unreadCount ? total + 1 : total; }, 0); }, [hasMenu, chatsById]); const withOtherVersions = window.location.hostname === PRODUCTION_HOSTNAME; const MainButton: FC<{ onTrigger: () => void; isOpen?: boolean }> = useMemo(() => { return ({ onTrigger, isOpen }) => ( ); }, [hasMenu, lang, onReset, shouldSkipTransition]); const handleSearchFocus = useCallback(() => { if (!searchQuery) { onSearchQuery(''); } }, [searchQuery, onSearchQuery]); const handleSelectSaved = useCallback(() => { openChat({ id: currentUserId, shouldReplaceHistory: true }); }, [currentUserId, openChat]); const handleDarkModeToggle = useCallback((e: React.SyntheticEvent) => { e.stopPropagation(); const newTheme = theme === 'light' ? 'dark' : 'light'; setSettingOption({ theme: newTheme }); setSettingOption({ shouldUseSystemTheme: false }); switchTheme(newTheme, animationLevel === ANIMATION_LEVEL_MAX); }, [animationLevel, setSettingOption, theme]); const handleAnimationLevelChange = useCallback((e: React.SyntheticEvent) => { e.stopPropagation(); const newLevel = animationLevel === 0 ? 2 : 0; ANIMATION_LEVEL_OPTIONS.forEach((_, i) => { document.body.classList.toggle(`animation-level-${i}`, newLevel === i); }); setSettingOption({ animationLevel: newLevel }); }, [animationLevel, setSettingOption]); const handleSwitchToWebK = () => { setPermanentWebVersion('K'); disableHistoryBack(); }; const handleOpenTipsChat = () => { openTipsChat({ langCode: lang.code }); }; const isSearchFocused = ( Boolean(globalSearchChatId) || content === LeftColumnContent.GlobalSearch || content === LeftColumnContent.Contacts ); const searchInputPlaceholder = content === LeftColumnContent.Contacts ? lang('SearchFriends') : lang('Search'); return (
{lang('SavedMessages')} {lang('ArchivedChats')} {archivedUnreadChatsCount > 0 && (
{archivedUnreadChatsCount}
)}
{lang('Contacts')} {lang('Settings')} {lang('lng_menu_night_mode')} {lang('Appearance.Animations').toLowerCase()} 0} /> {lang('TelegramFeatures')} Report Bug {withOtherVersions && ( <> Switch to K Version Switch to Old Version )}
{selectedSearchDate && ( )} {globalSearchChatId && ( )}
); }; export default memo(withGlobal( (global): StateProps => { const { query: searchQuery, fetchingStatus, chatId, date, } = global.globalSearch; const { currentUserId } = global; const { byId: chatsById } = global.chats; const { animationLevel } = global.settings.byKey; return { searchQuery, isLoading: fetchingStatus ? Boolean(fetchingStatus.chats || fetchingStatus.messages) : false, currentUserId, chatsById, globalSearchChatId: chatId, searchDate: date, theme: selectTheme(global), animationLevel, }; }, )(LeftMainHeader));