import { Group, PerpPosition } from '@blockworks-foundation/mango-v4' import { TwitterIcon } from '@components/icons/TwitterIcon' import Button from '@components/shared/Button' import { Dialog } from '@headlessui/react' import { DocumentDuplicateIcon } from '@heroicons/react/20/solid' import { useScreenshot } from 'hooks/useScreenshot' import { useTranslation } from 'next-i18next' import { createRef, useEffect, useMemo, useState } from 'react' import { ModalProps } from 'types/modal' import { formatNumericValue, getDecimalCount } from 'utils/numbers' interface SharePositionModalProps { group: Group position: PerpPosition } type ModalCombinedProps = SharePositionModalProps & ModalProps async function copyToClipboard(image: HTMLCanvasElement) { try { image.toBlob((blob: Blob | null) => { if (blob) { navigator.clipboard.write([new ClipboardItem({ 'image/png': blob })]) } }, 'image/png') } catch (error) { console.error(error) } } const SharePositionModal = ({ group, isOpen, onClose, position, }: ModalCombinedProps) => { const { t } = useTranslation('trade') const ref = createRef() const [copied, setCopied] = useState(false) const [showButton, setShowButton] = useState(true) const [image, takeScreenshot] = useScreenshot() const market = group.getPerpMarketByMarketIndex(position.marketIndex) const basePosition = position.getBasePositionUi(market) const entryPrice = position.getAverageEntryPriceUi(market) const side = basePosition > 0 ? 'long' : 'short' const roi = useMemo(() => { if (!market) return 0 let roi const indexPrice = market.uiPrice if (basePosition > 0) { roi = (indexPrice / entryPrice - 1) * 100 } else { roi = (indexPrice / entryPrice - 1) * -100 } return roi }, [basePosition, entryPrice, market]) useEffect(() => { if (image) { copyToClipboard(image) setCopied(true) setShowButton(true) } }, [image]) useEffect(() => { // if the button is hidden we are taking a screenshot if (!showButton) { takeScreenshot(ref.current as HTMLElement) } }, [showButton]) const handleCopyToClipboard = () => { setShowButton(false) } return ( ) } export default SharePositionModal