import * as React from 'react'; import { createPortal } from 'react-dom'; import { StickerPackInstallButton } from './StickerPackInstallButton'; import { ConfirmationDialog } from '../ConfirmationDialog'; import { LocalizerType } from '../../types/Util'; import { StickerPackType } from '../../state/ducks/stickers'; export type OwnProps = { readonly onClose: () => unknown; readonly installStickerPack: (packId: string, packKey: string) => unknown; readonly uninstallStickerPack: (packId: string, packKey: string) => unknown; readonly pack: StickerPackType; readonly i18n: LocalizerType; }; export type Props = OwnProps; function focusRef(el: HTMLElement | null) { if (el) { el.focus(); } } export const StickerPreviewModal = React.memo( // tslint:disable-next-line max-func-body-length ({ onClose, pack, i18n, installStickerPack, uninstallStickerPack, }: Props) => { const [root, setRoot] = React.useState(null); const [confirmingUninstall, setConfirmingUninstall] = React.useState(false); React.useEffect(() => { const div = document.createElement('div'); document.body.appendChild(div); setRoot(div); return () => { document.body.removeChild(div); setRoot(null); }; }, []); const isInstalled = pack.status === 'installed'; const handleToggleInstall = React.useCallback( () => { if (isInstalled) { setConfirmingUninstall(true); } else { installStickerPack(pack.id, pack.key); onClose(); } }, [isInstalled, pack, setConfirmingUninstall, installStickerPack, onClose] ); const handleUninstall = React.useCallback( () => { uninstallStickerPack(pack.id, pack.key); setConfirmingUninstall(false); // onClose is called by the confirmation modal }, [uninstallStickerPack, setConfirmingUninstall, pack] ); React.useEffect( () => { const handler = ({ key }: KeyboardEvent) => { if (key === 'Escape') { onClose(); } }; document.addEventListener('keyup', handler); return () => { document.removeEventListener('keyup', handler); }; }, [onClose] ); const handleClickToClose = React.useCallback( (e: React.MouseEvent) => { if (e.target === e.currentTarget) { onClose(); } }, [onClose] ); return root ? createPortal(
{confirmingUninstall ? ( {i18n('stickers--StickerManager--UninstallWarning')} ) : (

{i18n('stickers--StickerPreview--Title')}

{pack.stickers.map(({ id, url }) => (
{pack.title}
))}

{pack.title} {pack.isBlessed ? ( ) : null}

{pack.author}

)}
, root ) : null; } );