import { cloneElement, memo, useCallback, useMemo, useState } from 'react' import { Button, ButtonProps } from 'antd' import { CopyOutlined } from '@ant-design/icons' import { invokeWebApiWrapperAsync, notify } from './factory' export type CopyUrlProps = { sendLoading?: boolean hideUnsupported?: boolean onCopy?: () => (void | Promise) children: JSX.Element } export const CopyUrl = memo(({ children, onCopy, sendLoading, hideUnsupported = true }) => { const props = useMemo(() => children.props, [children]) const [loading, setLoading] = useState(false) const supported = !!navigator?.clipboard?.writeText // Проверка поддержки const onClick = useCallback((event: MouseEvent) => { if (supported) { invokeWebApiWrapperAsync( async () => { await navigator.clipboard.writeText(window.location.href) await onCopy?.() notify('URL успешно скопирован', 'info') }, setLoading, `Не удалось скопировать URL в буфер обмена` ) } props.onClick?.(event) // Запуск onClick по-умолчанию }, [props]) if (hideUnsupported && !supported) return null return cloneElement(children, { onClick, loading: sendLoading ? loading : props.loading }) }) export type CopyUrlButtonProps = Omit & ButtonProps export const CopyUrlButton = memo(({ sendLoading, hideUnsupported, onCopy, ...other }) => { return (