import type { FC } from '../../lib/teact/teact'; import React, { memo, useCallback } from '../../lib/teact/teact'; import { getActions } from '../../global'; import type { ApiMessage } from '../../api/types'; import { getPictogramDimensions } from '../common/helpers/mediaDimensions'; import { getMessageIsSpoiler, getMessageMediaHash, getMessageSingleInlineButton, } from '../../global/helpers'; import buildClassName from '../../util/buildClassName'; import { IS_TOUCH_ENV, MouseButton } from '../../util/windowEnvironment'; import renderText from '../common/helpers/renderText'; import useMedia from '../../hooks/useMedia'; import useThumbnail from '../../hooks/useThumbnail'; import useFlag from '../../hooks/useFlag'; import useLang from '../../hooks/useLang'; import useAsyncRendering from '../right/hooks/useAsyncRendering'; import RippleEffect from '../ui/RippleEffect'; import ConfirmDialog from '../ui/ConfirmDialog'; import Button from '../ui/Button'; import PinnedMessageNavigation from './PinnedMessageNavigation'; import MessageSummary from '../common/MessageSummary'; import MediaSpoiler from '../common/MediaSpoiler'; import AnimatedCounter from '../common/AnimatedCounter'; import Transition from '../ui/Transition'; import Spinner from '../ui/Spinner'; import styles from './HeaderPinnedMessage.module.scss'; const SHOW_LOADER_DELAY = 450; type OwnProps = { message: ApiMessage; index: number; count: number; customTitle?: string; className?: string; onUnpinMessage?: (id: number) => void; onClick?: (e: React.MouseEvent) => void; onAllPinnedClick?: () => void; isLoading?: boolean; isFullWidth?: boolean; }; const HeaderPinnedMessage: FC = ({ message, count, index, customTitle, className, onUnpinMessage, onClick, onAllPinnedClick, isLoading, isFullWidth, }) => { const { clickBotInlineButton } = getActions(); const lang = useLang(); const mediaThumbnail = useThumbnail(message); const mediaBlobUrl = useMedia(getMessageMediaHash(message, 'pictogram')); const isSpoiler = getMessageIsSpoiler(message); const canRenderLoader = useAsyncRendering([isLoading], SHOW_LOADER_DELAY); const shouldShowLoader = canRenderLoader && isLoading; const [isUnpinDialogOpen, openUnpinDialog, closeUnpinDialog] = useFlag(); const handleUnpinMessage = useCallback(() => { closeUnpinDialog(); if (onUnpinMessage) { onUnpinMessage(message.id); } }, [closeUnpinDialog, onUnpinMessage, message.id]); const inlineButton = getMessageSingleInlineButton(message); const handleInlineButtonClick = useCallback(() => { if (inlineButton) { clickBotInlineButton({ messageId: message.id, button: inlineButton }); } }, [clickBotInlineButton, inlineButton, message.id]); const [noHoverColor, markNoHoverColor, unmarkNoHoverColor] = useFlag(); const handleClick = useCallback((e: React.MouseEvent) => { if (e.type === 'mousedown' && e.button !== MouseButton.Main) { return; } onClick?.(e); }, [onClick]); function renderPictogram(thumbDataUri?: string, blobUrl?: string, spoiler?: boolean) { const { width, height } = getPictogramDimensions(); const srcUrl = blobUrl || thumbDataUri; return (
{thumbDataUri && !spoiler && } {thumbDataUri && }
); } return (
{(count > 1 || shouldShowLoader) && ( )} {onUnpinMessage && ( )}
{renderPictogram( mediaThumbnail, mediaBlobUrl, isSpoiler, )}
{!customTitle && ( 0 ? `#${count - index}` : ''}`} /> )} {customTitle && renderText(customTitle)}

{inlineButton && ( )}
); }; export default memo(HeaderPinnedMessage);