(text: string | (() => string))
| 3 | import { isJetBrains } from "../util"; |
| 4 | |
| 5 | export default function useCopy(text: string | (() => string)) { |
| 6 | const [copied, setCopied] = useState<boolean>(false); |
| 7 | const ideMessenger = useContext(IdeMessengerContext); |
| 8 | |
| 9 | const copyText = useCallback(() => { |
| 10 | const textVal = typeof text === "string" ? text : text(); |
| 11 | if (isJetBrains()) { |
| 12 | ideMessenger.post("copyText", { text: textVal }); |
| 13 | } else { |
| 14 | navigator.clipboard.writeText(textVal); |
| 15 | } |
| 16 | |
| 17 | setCopied(true); |
| 18 | setTimeout(() => setCopied(false), 2000); |
| 19 | }, [text, ideMessenger]); |
| 20 | |
| 21 | return { copied, copyText }; |
| 22 | } |
no test coverage detected