import React, { FC, memo, useCallback, useEffect, useRef, } from '../../lib/teact/teact'; import { withGlobal } from '../../lib/teact/teactn'; import { ApiSticker, ApiStickerSet } from '../../api/types'; import { GlobalActions } from '../../global/types'; import { STICKER_SIZE_MODAL } from '../../config'; import { pick } from '../../util/iteratees'; import { selectChat, selectCurrentMessageList, selectStickerSet, selectStickerSetByShortName, } from '../../modules/selectors'; import { useIntersectionObserver } from '../../hooks/useIntersectionObserver'; import useLang from '../../hooks/useLang'; import renderText from './helpers/renderText'; import { getAllowedAttachmentOptions, getCanPostInChat } from '../../modules/helpers'; import Modal from '../ui/Modal'; import Button from '../ui/Button'; import Loading from '../ui/Loading'; import StickerButton from './StickerButton'; import './StickerSetModal.scss'; export type OwnProps = { isOpen: boolean; fromSticker?: ApiSticker; stickerSetShortName?: string; onClose: () => void; }; type StateProps = { canSendStickers?: boolean; stickerSet?: ApiStickerSet; }; type DispatchProps = Pick; const INTERSECTION_THROTTLE = 200; const StickerSetModal: FC = ({ isOpen, fromSticker, stickerSetShortName, stickerSet, canSendStickers, onClose, loadStickers, toggleStickerSet, sendMessage, }) => { // eslint-disable-next-line no-null/no-null const containerRef = useRef(null); const lang = useLang(); const { observe: observeIntersection, } = useIntersectionObserver({ rootRef: containerRef, throttleMs: INTERSECTION_THROTTLE, isDisabled: !isOpen }); useEffect(() => { if (isOpen) { if (fromSticker) { const { stickerSetId, stickerSetAccessHash } = fromSticker; loadStickers({ stickerSetId, stickerSetAccessHash, }); } else if (stickerSetShortName) { loadStickers({ stickerSetShortName, }); } } }, [isOpen, fromSticker, loadStickers, stickerSetShortName]); const handleSelect = useCallback((sticker: ApiSticker) => { sticker = { ...sticker, isPreloadedGlobally: true, }; sendMessage({ sticker }); onClose(); }, [onClose, sendMessage]); const handleButtonClick = useCallback(() => { if (stickerSet) { toggleStickerSet({ stickerSetId: stickerSet.id }); onClose(); } }, [onClose, stickerSet, toggleStickerSet]); return ( {stickerSet?.stickers ? ( <>
{stickerSet.stickers.map((sticker) => ( ))}
) : ( )}
); }; export default memo(withGlobal( (global, { fromSticker, stickerSetShortName }): StateProps => { const currentMessageList = selectCurrentMessageList(global); const { chatId, threadId } = currentMessageList || {}; const chat = chatId && selectChat(global, chatId); const sendOptions = chat ? getAllowedAttachmentOptions(chat) : undefined; const canSendStickers = Boolean( chat && threadId && getCanPostInChat(chat, threadId) && sendOptions?.canSendStickers, ); return { canSendStickers, stickerSet: fromSticker ? selectStickerSet(global, fromSticker.stickerSetId) : stickerSetShortName ? selectStickerSetByShortName(global, stickerSetShortName) : undefined, }; }, (setGlobal, actions): DispatchProps => pick(actions, [ 'loadStickers', 'toggleStickerSet', 'sendMessage', ]), )(StickerSetModal));