import type { FC } from '../../../lib/teact/teact'; import { memo, useCallback, useMemo } from '../../../lib/teact/teact'; import { getActions, getGlobal, withGlobal } from '../../../global'; import type { ApiMessage } from '../../../api/types'; import type { StateProps } from './helpers/createMapStateToProps'; import { AudioOrigin, LoadMoreDirection } from '../../../types'; import { SLIDE_TRANSITION_DURATION } from '../../../config'; import { getIsDownloading } from '../../../global/helpers'; import { selectMessageDownloadableMedia } from '../../../global/selectors/media'; import { formatMonthAndYear, toYearMonth } from '../../../util/dates/dateFormat'; import { parseSearchResultKey } from '../../../util/keys/searchResultKey'; import { MEMO_EMPTY_ARRAY } from '../../../util/memo'; import { throttle } from '../../../util/schedulers'; import { createMapStateToProps } from './helpers/createMapStateToProps'; import { getSenderName } from './helpers/getSenderName'; import useOldLang from '../../../hooks/useOldLang'; import useAsyncRendering from '../../right/hooks/useAsyncRendering'; import Audio from '../../common/Audio'; import NothingFound from '../../common/NothingFound'; import InfiniteScroll from '../../ui/InfiniteScroll'; import Loading from '../../ui/Loading'; import Transition from '../../ui/Transition.tsx'; export type OwnProps = { isVoice?: boolean; searchQuery?: string; }; const runThrottled = throttle((cb) => cb(), 500, true); const AudioResults: FC = ({ theme, isVoice, searchQuery, isLoading, chatsById, usersById, globalMessagesByChatId, foundIds, activeDownloads, }) => { const { searchMessagesGlobal, focusMessage, openAudioPlayer, } = getActions(); const lang = useOldLang(); const currentType = isVoice ? 'voice' : 'audio'; const handleLoadMore = useCallback(({ direction }: { direction: LoadMoreDirection }) => { if (direction === LoadMoreDirection.Backwards) { runThrottled(() => { searchMessagesGlobal({ type: currentType, }); }); } // eslint-disable-next-line react-hooks-static-deps/exhaustive-deps -- `searchQuery` is required to prevent infinite message loading }, [currentType, searchMessagesGlobal, searchQuery]); const foundMessages = useMemo(() => { if (!foundIds || !globalMessagesByChatId) { return MEMO_EMPTY_ARRAY; } return foundIds.map((id) => { const [chatId, messageId] = parseSearchResultKey(id); return globalMessagesByChatId[chatId]?.byId[messageId]; }).filter(Boolean); }, [globalMessagesByChatId, foundIds]); const handleMessageFocus = useCallback((message: ApiMessage) => { focusMessage({ chatId: message.chatId, messageId: message.id }); }, [focusMessage]); const handlePlayAudio = useCallback((messageId: number, chatId: string) => { openAudioPlayer({ chatId, messageId }); }, [openAudioPlayer]); function renderList() { return foundMessages.map((message, index) => { const global = getGlobal(); const isFirst = index === 0; const shouldDrawDateDivider = isFirst || toYearMonth(message.date) !== toYearMonth(foundMessages[index - 1].date); const media = selectMessageDownloadableMedia(global, message)!; return ( <> {shouldDrawDateDivider && (

{formatMonthAndYear(lang, new Date(message.date * 1000))}

)}
); }); } const canRenderContents = useAsyncRendering([searchQuery], SLIDE_TRANSITION_DURATION) && !isLoading; return ( {!canRenderContents && } {canRenderContents && (!foundIds || foundIds.length === 0) && ( )} {canRenderContents && foundIds && foundIds.length > 0 && renderList()} ); }; export default memo(withGlobal( createMapStateToProps('audio'), )(AudioResults));