import React from 'react'
import { useRouter } from 'next/router'
import { useCopyTextHandler, useAsyncCallback } from 'actionsack'
import morph from 'morphmorph'
import { COLORS } from '../lib/constants'
import Button from './Button'
import Popout, { managePopout } from './Popout'
import CopySVG from './svg/Copy'
const toIFrame = (url, width, height) =>
`
`
const toURL = url => `${location.origin}${url}`
const toEncodedURL = morph.compose(encodeURI, toURL)
function CopyButton(props) {
return (
)
}
function CopyEmbed({ mapper, title }) {
const { asPath } = useRouter()
const text = React.useMemo(() => mapper(asPath), [mapper, asPath])
const { onClick, copied } = useCopyTextHandler(text)
return {copied ? 'Copied!' : title}
}
const popoutStyle = { width: '140px', right: 0 }
function useClipboardSupport() {
const [isClipboardSupports, setClipboardSupport] = React.useState(false)
React.useEffect(() => {
setClipboardSupport(
window.navigator && window.navigator.clipboard && typeof ClipboardItem === 'function'
)
}, [])
return isClipboardSupports
}
function CopyMenu({ isVisible, toggleVisibility, copyImage, carbonRef }) {
const clipboardSupported = useClipboardSupport()
const [showCopied, { loading: copied }] = useAsyncCallback(
() => new Promise(res => setTimeout(res, 1000))
)
const [copy, { loading }] = useAsyncCallback(async (...args) => {
await copyImage(...args)
showCopied()
})
return (
Copy to clipboard
{clipboardSupported && (
{loading ? 'Copying…' : copied ? 'Copied!' : 'Image'}
)}
toIFrame(url, carbonRef.clientWidth, carbonRef.clientHeight)}
/>
)
}
export default managePopout(React.memo(CopyMenu))