import React from 'react' import { withRouter } from 'next/router' import { useCopyTextHandler, useOnline, useKeyboardListener, useAsyncCallback } from 'actionsack' import { COLORS, EXPORT_SIZES } from '../lib/constants' import Button from './Button' import Input from './Input' import Popout, { managePopout } from './Popout' const toIFrame = url => ` ` const toURL = url => encodeURI(`${location.origin}${url}`) const MAX_PAYLOAD_SIZE = 5e6 // bytes function verifyPayloadSize(str) { if (typeof str !== 'string') return true if (typeof window !== 'undefined') { return new Blob([str]).size < MAX_PAYLOAD_SIZE } return Buffer.byteLength(str, 'utf8') } const CopyEmbed = withRouter(({ router: { asPath }, mapper, title, margin }) => { const text = React.useMemo(() => mapper(asPath), [mapper, asPath]) const { onClick, copied } = useCopyTextHandler(text) return ( ) }) const popoutStyle = { width: '280px', right: 0 } function useSafari() { const [isSafari, setSafari] = React.useState(false) React.useEffect(() => { setSafari( window.navigator && window.navigator.userAgent.indexOf('Safari') !== -1 && window.navigator.userAgent.indexOf('Chrome') === -1 ) }, []) return isSafari } function ExportMenu({ backgroundImage, onChange, exportSize, isVisible, toggleVisibility, exportImage: exp }) { const tooLarge = React.useMemo(() => !verifyPayloadSize(backgroundImage), [backgroundImage]) const online = useOnline() const isSafari = useSafari() const [exportImage, { loading }] = useAsyncCallback(exp) useKeyboardListener('⌘-⇧-e', () => exportImage()) const disablePNG = isSafari && (tooLarge || !online) const input = React.useRef() const handleExportSizeChange = selectedSize => () => onChange('exportSize', selectedSize) const handleExport = format => () => exportImage(format, { filename: input.current.value }) return (
) } export default managePopout(React.memo(ExportMenu))