import type { FC } from '../../../lib/teact/teact'; import React, { useEffect, useCallback, useRef, memo, } from '../../../lib/teact/teact'; import { getActions, withGlobal } from '../../../global'; import type { ApiUser } from '../../../api/types'; import { getUserFirstOrLastName } from '../../../global/helpers'; import renderText from '../../common/helpers/renderText'; import { throttle } from '../../../util/schedulers'; import buildClassName from '../../../util/buildClassName'; import useHorizontalScroll from '../../../hooks/useHorizontalScroll'; import useLang from '../../../hooks/useLang'; import Button from '../../ui/Button'; import Avatar from '../../common/Avatar'; import LeftSearchResultChat from './LeftSearchResultChat'; import './RecentContacts.scss'; type OwnProps = { onReset: () => void; }; type StateProps = { topUserIds?: string[]; usersById: Record; recentlyFoundChatIds?: string[]; }; const SEARCH_CLOSE_TIMEOUT_MS = 250; const NBSP = '\u00A0'; const runThrottled = throttle((cb) => cb(), 60000, true); const RecentContacts: FC = ({ topUserIds, usersById, recentlyFoundChatIds, onReset, }) => { const { loadTopUsers, openChat, addRecentlyFoundChatId, clearRecentlyFoundChats, } = getActions(); // eslint-disable-next-line no-null/no-null const topUsersRef = useRef(null); // Due to the parent Transition, this component never gets unmounted, // that's why we use throttled API call on every update. useEffect(() => { runThrottled(() => { loadTopUsers(); }); }, [loadTopUsers]); useHorizontalScroll(topUsersRef, !topUserIds); const handleClick = useCallback((id: string) => { openChat({ id, shouldReplaceHistory: true }); onReset(); setTimeout(() => { addRecentlyFoundChatId({ id }); }, SEARCH_CLOSE_TIMEOUT_MS); }, [openChat, addRecentlyFoundChatId, onReset]); const handleClearRecentlyFoundChats = useCallback(() => { clearRecentlyFoundChats(); }, [clearRecentlyFoundChats]); const lang = useLang(); return (
{topUserIds && (
{topUserIds.map((userId) => (
handleClick(userId)} dir={lang.isRtl ? 'rtl' : undefined} >
{renderText(getUserFirstOrLastName(usersById[userId]) || NBSP)}
))}
)} {recentlyFoundChatIds && (

{lang('Recent')}

{recentlyFoundChatIds.map((id) => ( ))}
)}
); }; export default memo(withGlobal( (global): StateProps => { const { userIds: topUserIds } = global.topPeers; const usersById = global.users.byId; const { recentlyFoundChatIds } = global; return { topUserIds, usersById, recentlyFoundChatIds, }; }, )(RecentContacts));