import {CheckIcon, ClipboardIcon, DocumentDuplicateIcon} from '@heroicons/react/24/outline' import {LinkIcon} from '@heroicons/react/24/solid' import clsx from 'clsx' import {ComponentProps, useState} from 'react' import toast from 'react-hot-toast' import {Button, ColorType, IconButton, SizeType} from 'web/components/buttons/button' import {useT} from 'web/lib/locale' import {track} from 'web/lib/service/analytics' import {copyToClipboard} from 'web/lib/util/copy' import {Tooltip} from '../widgets/tooltip' export function CopyLinkOrShareButton(props: { url: string eventTrackingName: string // was type ShareEventName — why?? tooltip?: string className?: string iconClassName?: string size?: SizeType children?: React.ReactNode color?: ColorType trackingInfo?: { contractId: string } }) { const {url, size, children, className, iconClassName, tooltip, color} = props const [isSuccess, setIsSuccess] = useState(false) const t = useT() const onClick = () => { if (!url) return copyToClipboard(url) mobileShare(url) setIsSuccess(true) setTimeout(() => setIsSuccess(false), 2000) // Reset after 2 seconds } const content = isSuccess ? t('about.settings.copied', 'Copied!') : children return ( ) } const ToolTipOrDiv = (props: {hasChildren: boolean} & ComponentProps) => props.hasChildren ? ( <>{props.children} ) : ( {' '} {props.children} ) export const CopyLinkRow = (props: { url?: string // required if not loading eventTrackingName: string linkBoxClassName?: string linkButtonClassName?: string }) => { const {url, linkBoxClassName, linkButtonClassName} = props // "copied" success state animations const [bgPressed, setBgPressed] = useState(false) const [iconPressed, setIconPressed] = useState(false) const t = useT() const onClick = () => { if (!url) return setBgPressed(true) setIconPressed(true) setTimeout(() => setBgPressed(false), 300) setTimeout(() => setIconPressed(false), 1000) copyToClipboard(url) toast.success(t('copy_link_button.link_copied', 'Link copied!')) } // remove any http:// prefix const displayUrl = url?.replace(/^https?:\/\//, '') ?? '' return ( ) } export function SimpleCopyTextButton(props: { text: string eventTrackingName: string // was type ShareEventName — why?? tooltip?: string className?: string }) { const {text, eventTrackingName, className, tooltip} = props const t = useT() const onClick = () => { if (!text) return copyToClipboard(text) toast.success(t('copy_link_button.link_copied', 'Link copied!')) track(eventTrackingName, {text}) } return ( ) } export async function mobileShare(url: string) { try { await navigator.share({ title: 'My Compass profile', text: 'Thoughtful connections > swiping.', url: url, }) return true } catch (e) { if (!(e instanceof Error && e.message.includes('navigator.share is not a function'))) { console.error('Failed to share', e) } return false } } export const share = async (url: string) => { const success = await mobileShare(url) if (!success) { window.open(url, '_blank', 'noopener,noreferrer') } } export const shareOnX = (profileUrl: string, text: string) => { const encodedText = encodeURIComponent(text) const encodedUrl = encodeURIComponent(profileUrl) const shareUrl = `https://twitter.com/intent/tweet?text=${encodedText}&url=${encodedUrl}` window.open(shareUrl, '_blank', 'noopener,noreferrer') } export const shareOnLinkedIn = (profileUrl: string) => { const encodedUrl = encodeURIComponent(profileUrl) const shareUrl = `https://www.linkedin.com/sharing/share-offsite/?url=${encodedUrl}` window.open(shareUrl, '_blank', 'noopener,noreferrer') } export const ShareProfileOnXButton = (props: {username: string; className?: string}) => { const {username, className} = props const t = useT() return ( ) } export const ShareProfileOnLinkedinButton = (props: {username: string; className?: string}) => { const {username, className} = props const t = useT() return ( ) }