import React, { memo, useCallback, useEffect, useMemo, useRef, } from '../../lib/teact/teact'; import { getActions, withGlobal } from '../../global'; import type { FC } from '../../lib/teact/teact'; import type { ApiSticker, ApiStickerSet } from '../../api/types'; import { EMOJI_SIZE_MODAL, STICKER_SIZE_MODAL, TME_LINK_PREFIX } from '../../config'; import { selectCanScheduleUntilOnline, selectChat, selectCurrentMessageList, selectIsChatWithSelf, selectIsCurrentUserPremium, selectShouldSchedule, selectStickerSet, } from '../../global/selectors'; import renderText from './helpers/renderText'; import { copyTextToClipboard } from '../../util/clipboard'; import { getAllowedAttachmentOptions, getCanPostInChat } from '../../global/helpers'; import { useIntersectionObserver } from '../../hooks/useIntersectionObserver'; import useLang from '../../hooks/useLang'; import useAppLayout from '../../hooks/useAppLayout'; import useSchedule from '../../hooks/useSchedule'; import usePrevious from '../../hooks/usePrevious'; import Modal from '../ui/Modal'; import Button from '../ui/Button'; import Loading from '../ui/Loading'; import StickerButton from './StickerButton'; import DropdownMenu from '../ui/DropdownMenu'; import MenuItem from '../ui/MenuItem'; import './StickerSetModal.scss'; export type OwnProps = { isOpen: boolean; fromSticker?: ApiSticker; stickerSetShortName?: string; onClose: () => void; }; type StateProps = { canSendStickers?: boolean; stickerSet?: ApiStickerSet; canScheduleUntilOnline?: boolean; shouldSchedule?: boolean; isSavedMessages?: boolean; isCurrentUserPremium?: boolean; }; const INTERSECTION_THROTTLE = 200; const StickerSetModal: FC = ({ isOpen, fromSticker, stickerSetShortName, stickerSet, canSendStickers, canScheduleUntilOnline, shouldSchedule, isSavedMessages, isCurrentUserPremium, onClose, }) => { const { loadStickers, toggleStickerSet, sendMessage, showNotification, } = getActions(); // eslint-disable-next-line no-null/no-null const containerRef = useRef(null); // eslint-disable-next-line no-null/no-null const sharedCanvasRef = useRef(null); const lang = useLang(); const { isMobile } = useAppLayout(); const prevStickerSet = usePrevious(stickerSet); const renderingStickerSet = stickerSet || prevStickerSet; const isAdded = Boolean(!renderingStickerSet?.isArchived && renderingStickerSet?.installedDate); const isEmoji = renderingStickerSet?.isEmoji; const [requestCalendar, calendar] = useSchedule(canScheduleUntilOnline); const { observe: observeIntersection, } = useIntersectionObserver({ rootRef: containerRef, throttleMs: INTERSECTION_THROTTLE, isDisabled: !isOpen }); useEffect(() => { if (isOpen && !renderingStickerSet?.stickers) { loadStickers({ stickerSetInfo: fromSticker ? fromSticker.stickerSetInfo : { shortName: stickerSetShortName! }, }); } }, [isOpen, fromSticker, loadStickers, stickerSetShortName, renderingStickerSet]); const handleSelect = useCallback((sticker: ApiSticker, isSilent?: boolean, isScheduleRequested?: boolean) => { sticker = { ...sticker, isPreloadedGlobally: true, }; if (shouldSchedule || isScheduleRequested) { requestCalendar((scheduledAt) => { sendMessage({ sticker, isSilent, scheduledAt, }); onClose(); }); } else { sendMessage({ sticker, isSilent, shouldUpdateStickerSetsOrder: isAdded }); onClose(); } }, [onClose, requestCalendar, sendMessage, shouldSchedule, isAdded]); const handleButtonClick = useCallback(() => { if (renderingStickerSet) { toggleStickerSet({ stickerSetId: renderingStickerSet.id }); onClose(); } }, [onClose, renderingStickerSet, toggleStickerSet]); const handleCopyLink = useCallback(() => { if (!renderingStickerSet) return; const { shortName } = renderingStickerSet; const suffix = isEmoji ? 'addemoji' : 'addstickers'; const url = `${TME_LINK_PREFIX}${suffix}/${shortName}`; copyTextToClipboard(url); showNotification({ message: lang('LinkCopied'), }); }, [isEmoji, lang, renderingStickerSet, showNotification]); const renderButtonText = () => { if (!renderingStickerSet) return lang('Loading'); const suffix = isEmoji ? 'Emoji' : 'Sticker'; return lang( isAdded ? `StickerPack.Remove${suffix}Count` : `StickerPack.Add${suffix}Count`, renderingStickerSet.count, 'i', ); }; const MoreMenuButton: FC<{ onTrigger: () => void; isOpen?: boolean }> = useMemo(() => { return ({ onTrigger, isOpen: isMenuOpen }) => ( ); }, [isMobile]); function renderHeader() { return (
{renderingStickerSet ? renderText(renderingStickerSet.title, ['emoji', 'links']) : lang('AccDescrStickerSet')}
{lang('StickersCopy')}
); } return ( {renderingStickerSet?.stickers ? ( <>
{renderingStickerSet.stickers.map((sticker) => ( ))}
) : ( )} {calendar}
); }; 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, ); const isSavedMessages = Boolean(chatId) && selectIsChatWithSelf(global, chatId); const stickerSetInfo = fromSticker ? fromSticker.stickerSetInfo : stickerSetShortName ? { shortName: stickerSetShortName } : undefined; const stickerSet = stickerSetInfo ? selectStickerSet(global, stickerSetInfo) : undefined; return { canScheduleUntilOnline: Boolean(chatId) && selectCanScheduleUntilOnline(global, chatId), canSendStickers, isSavedMessages, shouldSchedule: selectShouldSchedule(global), stickerSet, isCurrentUserPremium: selectIsCurrentUserPremium(global), }; }, )(StickerSetModal));