(text?: string)
| 90 | } |
| 91 | |
| 92 | export function useCopyText(text?: string): [boolean, CopyNotifyFunction] { |
| 93 | const [copying, setCopying] = useState(false); |
| 94 | const { displayToast } = useToastNotification(); |
| 95 | |
| 96 | const copy: CopyNotifyFunction = async (props = {}) => { |
| 97 | const textToCopy = props.textToCopy || text; |
| 98 | |
| 99 | // Nothing to put on the clipboard; writing it anyway pastes "undefined". |
| 100 | if (!textToCopy) { |
| 101 | displayToast(noTextErrorMessage, { variant: ToastType.Error }); |
| 102 | |
| 103 | return; |
| 104 | } |
| 105 | |
| 106 | try { |
| 107 | await navigator.clipboard.writeText(textToCopy); |
| 108 | } catch { |
| 109 | displayToast(blockedMessage, { variant: ToastType.Error }); |
| 110 | |
| 111 | return; |
| 112 | } |
| 113 | |
| 114 | if (!props.disableToast) { |
| 115 | displayToast(props.message || defaultMessage, props); |
| 116 | } |
| 117 | |
| 118 | setCopying(true); |
| 119 | setTimeout(() => { |
| 120 | setCopying(false); |
| 121 | }, 1000); |
| 122 | }; |
| 123 | |
| 124 | return [copying, copy]; |
| 125 | } |
no test coverage detected