mirror of
https://git.mirrors.martin98.com/https://github.com/langgenius/dify.git
synced 2025-08-11 01:18:58 +08:00
parent
668b059c07
commit
34f55739e0
@ -1,29 +1,31 @@
|
|||||||
import { useState } from 'react'
|
import { useCallback, useState } from 'react'
|
||||||
|
import writeText from 'copy-to-clipboard'
|
||||||
|
|
||||||
type CopiedValue = string | null
|
type CopiedValue = string | null
|
||||||
type CopyFn = (text: string) => Promise<boolean>
|
type CopyFn = (text: string) => Promise<boolean>
|
||||||
|
|
||||||
function useCopyToClipboard(): [CopiedValue, CopyFn] {
|
function useCopyToClipboard(): [CopiedValue, CopyFn] {
|
||||||
const [copiedText, setCopiedText] = useState<CopiedValue>(null)
|
const [copiedText, setCopiedText] = useState<CopiedValue>(null)
|
||||||
|
|
||||||
const copy: CopyFn = async text => {
|
const copy: CopyFn = useCallback(async (text: string) => {
|
||||||
if (!navigator?.clipboard) {
|
if (!navigator?.clipboard) {
|
||||||
console.warn('Clipboard not supported')
|
console.warn('Clipboard not supported')
|
||||||
return false
|
return false
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
await navigator.clipboard.writeText(text)
|
|
||||||
setCopiedText(text)
|
|
||||||
return true
|
|
||||||
} catch (error) {
|
|
||||||
console.warn('Copy failed', error)
|
|
||||||
setCopiedText(null)
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return [copiedText, copy]
|
try {
|
||||||
|
writeText(text)
|
||||||
|
setCopiedText(text)
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
catch (error) {
|
||||||
|
console.warn('Copy failed', error)
|
||||||
|
setCopiedText(null)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}, [])
|
||||||
|
|
||||||
|
return [copiedText, copy]
|
||||||
}
|
}
|
||||||
|
|
||||||
export default useCopyToClipboard
|
export default useCopyToClipboard
|
||||||
|
Loading…
x
Reference in New Issue
Block a user