import React, { memo, useCallback, useEffect, useRef, } from '../../../lib/teact/teact'; import { getActions, withGlobal } from '../../../global'; import type { FC } from '../../../lib/teact/teact'; import type { ApiSticker } from '../../../api/types'; import type { GlobalActions } from '../../../global/types'; import { COMPOSER_EMOJI_SIZE_PICKER } from '../../../config'; import { selectIsChatWithSelf, selectIsCurrentUserPremium } from '../../../global/selectors'; import buildClassName from '../../../util/buildClassName'; import captureEscKeyListener from '../../../util/captureEscKeyListener'; import { useIntersectionObserver } from '../../../hooks/useIntersectionObserver'; import useShowTransition from '../../../hooks/useShowTransition'; import usePrevious from '../../../hooks/usePrevious'; import useHorizontalScroll from '../../../hooks/useHorizontalScroll'; import Loading from '../../ui/Loading'; import StickerButton from '../../common/StickerButton'; import styles from './CustomEmojiTooltip.module.scss'; export type OwnProps = { chatId: string; isOpen: boolean; onCustomEmojiSelect: (customEmoji: ApiSticker) => void; addRecentCustomEmoji: GlobalActions['addRecentCustomEmoji']; }; type StateProps = { customEmoji?: ApiSticker[]; isSavedMessages?: boolean; isCurrentUserPremium?: boolean; }; const INTERSECTION_THROTTLE = 200; const CustomEmojiTooltip: FC = ({ isOpen, customEmoji, isSavedMessages, isCurrentUserPremium, onCustomEmojiSelect, addRecentCustomEmoji, }) => { const { clearCustomEmojiForEmoji } = getActions(); // eslint-disable-next-line no-null/no-null const containerRef = useRef(null); const { shouldRender, transitionClassNames } = useShowTransition(isOpen, undefined, undefined, false); const prevStickers = usePrevious(customEmoji, true); const displayedStickers = customEmoji || prevStickers; useHorizontalScroll(containerRef.current); const { observe: observeIntersection, } = useIntersectionObserver({ rootRef: containerRef, throttleMs: INTERSECTION_THROTTLE }); useEffect(() => ( isOpen ? captureEscKeyListener(clearCustomEmojiForEmoji) : undefined ), [isOpen, clearCustomEmojiForEmoji]); const handleCustomEmojiSelect = useCallback((ce: ApiSticker) => { if (!isOpen) return; onCustomEmojiSelect(ce); addRecentCustomEmoji({ documentId: ce.id, }); clearCustomEmojiForEmoji(); }, [addRecentCustomEmoji, clearCustomEmojiForEmoji, isOpen, onCustomEmojiSelect]); const className = buildClassName( styles.root, 'composer-tooltip custom-scroll-x', transitionClassNames, !displayedStickers?.length && styles.hidden, ); return (
{shouldRender && displayedStickers ? ( displayedStickers.map((sticker) => ( )) ) : shouldRender ? ( ) : undefined}
); }; export default memo(withGlobal( (global, { chatId }): StateProps => { const { stickers: customEmoji } = global.customEmojis.forEmoji; const isSavedMessages = selectIsChatWithSelf(global, chatId); const isCurrentUserPremium = selectIsCurrentUserPremium(global); return { customEmoji, isSavedMessages, isCurrentUserPremium }; }, )(CustomEmojiTooltip));