import React from 'react' import { 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 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 popoutStyle = { width: '256px', 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 input = React.useRef() const [exportImage, { loading }] = useAsyncCallback(exp) useKeyboardListener('⌘-⇧-e', () => exportImage()) const disablePNG = isSafari && (tooLarge || !online) const handleExportSizeChange = selectedSize => () => onChange('exportSize', selectedSize) const handleExport = format => () => exportImage(format, { filename: input.current.value }) return (
) } export default managePopout(React.memo(ExportMenu))