import React, { FC, useCallback, useMemo, memo, } from '../../../lib/teact/teact'; import { withGlobal } from '../../../lib/teact/teactn'; import { GlobalActions } from '../../../global/types'; import { LeftColumnContent } from '../../../types'; import { ApiChat } from '../../../api/types'; import { IS_MOBILE_SCREEN } from '../../../util/environment'; import buildClassName from '../../../util/buildClassName'; import { pick } from '../../../util/iteratees'; import { isChatArchived } from '../../../modules/helpers'; import { formatDateToString } from '../../../util/dateFormat'; import useLang from '../../../hooks/useLang'; 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 './LeftMainHeader.scss'; type OwnProps = { content: LeftColumnContent; contactsFilter: string; onSearchQuery: (query: string) => void; onSelectSettings: () => void; onSelectContacts: () => void; onSelectNewGroup: () => void; onSelectArchived: () => void; onReset: () => void; }; type StateProps = { searchQuery?: string; isLoading: boolean; currentUserId?: number; globalSearchChatId?: number; searchDate?: number; chatsById?: Record; }; type DispatchProps = Pick; const LeftMainHeader: FC = ({ content, contactsFilter, onSearchQuery, onSelectSettings, onSelectContacts, onSelectNewGroup, onSelectArchived, setGlobalSearchChatId, onReset, searchQuery, isLoading, currentUserId, globalSearchChatId, searchDate, chatsById, openChat, openSupportChat, setGlobalSearchDate, }) => { 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 MainButton: FC<{ onTrigger: () => void; isOpen?: boolean }> = useMemo(() => { return ({ onTrigger, isOpen }) => ( ); }, [hasMenu, onReset]); const handleSearchFocus = useCallback(() => { if (!searchQuery) { onSearchQuery(''); } }, [searchQuery, onSearchQuery]); const handleSelectSaved = useCallback(() => { openChat({ id: currentUserId }); }, [currentUserId, openChat]); const lang = useLang(); const isSearchFocused = Boolean(globalSearchChatId) || content === LeftColumnContent.GlobalSearch || content === LeftColumnContent.Contacts; const searchInputPlaceholder = content === LeftColumnContent.Contacts ? lang('SearchFriends') : lang('Search'); return (
{lang('NewGroup')} {lang('Contacts')} {lang('Archived')} {archivedUnreadChatsCount > 0 && (
{archivedUnreadChatsCount}
)}
{lang('Saved')} {lang('Settings')} {lang('BotHelp')}
{selectedSearchDate && ( )} {globalSearchChatId && ( )}
); }; export default memo(withGlobal( (global): StateProps => { const { query: searchQuery, fetchingStatus, chatId, date, } = global.globalSearch; const { currentUserId } = global; const { byId: chatsById } = global.chats; return { searchQuery, isLoading: fetchingStatus ? Boolean(fetchingStatus.chats || fetchingStatus.messages) : false, currentUserId, chatsById, globalSearchChatId: chatId, searchDate: date, }; }, (setGlobal, actions): DispatchProps => pick(actions, [ 'openChat', 'openSupportChat', 'setGlobalSearchDate', 'setGlobalSearchChatId', ]), )(LeftMainHeader));