import type { FC } from '../../lib/teact/teact'; import React, { useEffect, memo, useMemo, useCallback, useRef, } from '../../lib/teact/teact'; import { getActions, withGlobal } from '../../global'; import type { ApiSticker, ApiStickerSet } from '../../api/types'; import type { ObserveFn } from '../../hooks/useIntersectionObserver'; import { STICKER_SIZE_SEARCH } from '../../config'; import { selectIsCurrentUserPremium, selectShouldLoopStickers, selectStickerSet } from '../../global/selectors'; import useLang from '../../hooks/useLang'; import Button from '../ui/Button'; import StickerButton from '../common/StickerButton'; import Spinner from '../ui/Spinner'; type OwnProps = { stickerSetId: string; observeIntersection: ObserveFn; isModalOpen?: boolean; }; type StateProps = { set?: ApiStickerSet; shouldPlay?: boolean; isCurrentUserPremium?: boolean; }; const PREMIUM_STICKERS_TO_DISPLAY = 3; const STICKERS_TO_DISPLAY = 5; const StickerSetResult: FC = ({ stickerSetId, observeIntersection, set, shouldPlay, isModalOpen, isCurrentUserPremium, }) => { const { loadStickers, toggleStickerSet, openStickerSet } = getActions(); // eslint-disable-next-line no-null/no-null const sharedCanvasRef = useRef(null); const lang = useLang(); const isAdded = set && !set.isArchived && Boolean(set.installedDate); const areStickersLoaded = Boolean(set?.stickers); const displayedStickers = useMemo(() => { if (!set) { return []; } const premiumStickerIds = (set.stickers?.filter(({ hasEffect }) => hasEffect) ?? []) .slice(0, PREMIUM_STICKERS_TO_DISPLAY); const coverStickerIds = (set.covers || []).map(({ id }) => id); const otherStickers = set.stickers ? set.stickers.filter(({ id }) => !coverStickerIds.includes(id)) : []; return [...premiumStickerIds, ...(set.covers || []), ...otherStickers].slice(0, STICKERS_TO_DISPLAY); }, [set]); useEffect(() => { // Featured stickers are initialized with one sticker in collection (cover of SickerSet) if (!areStickersLoaded && displayedStickers.length < STICKERS_TO_DISPLAY && set) { loadStickers({ stickerSetInfo: { shortName: set.shortName, }, }); } }, [areStickersLoaded, displayedStickers.length, loadStickers, set, stickerSetId]); const handleAddClick = useCallback(() => { toggleStickerSet({ stickerSetId }); }, [toggleStickerSet, stickerSetId]); const handleStickerClick = useCallback((sticker: ApiSticker) => { openStickerSet({ stickerSetInfo: sticker.stickerSetInfo }); }, [openStickerSet]); if (!set) { return undefined; } const canRenderStickers = displayedStickers.length > 0; return (

{set.title}

{lang('Stickers', set.count, 'i')}

{!canRenderStickers && } {canRenderStickers && displayedStickers.map((sticker) => ( ))}
); }; export default memo(withGlobal( (global, { stickerSetId }): StateProps => { return { set: selectStickerSet(global, stickerSetId), shouldPlay: selectShouldLoopStickers(global), isCurrentUserPremium: selectIsCurrentUserPremium(global), }; }, )(StickerSetResult));