-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathcopy-code-button.tsx
54 lines (48 loc) · 1.55 KB
/
copy-code-button.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
'use client'
import { useEffect, useState } from 'react'
import { addBreaklineBetweenBadges, copyToClipboard, replaceBadgesMarkdownToHtml } from '@/utils'
import { Check, Code2 } from 'lucide-react'
import { NodeHtmlMarkdown } from 'node-html-markdown'
import { cn } from '@/lib/utils'
import { useBuilder } from '@/store'
import { Button } from '@/components/ui/button'
export function CopyCodeButton() {
const readmeEditor = useBuilder((store) => store.readmeEditor)
const [clicked, setClicked] = useState(false)
useEffect(() => {
let timeoutId: ReturnType<typeof setTimeout>
if (clicked) {
timeoutId = setTimeout(() => {
setClicked(false)
}, 1500)
}
return () => clearTimeout(timeoutId)
}, [clicked])
return (
<Button
className={cn('w-full sm:w-44 p-2 copy-button group', clicked && 'animate')}
onClick={async () => {
setClicked(true)
const html = readmeEditor?.getHTML() as string
const md = NodeHtmlMarkdown.translate(html, {
bulletMarker: '-',
textReplace: [
[/\\_/g, '_'],
[/\\/g, '']
],
useInlineLinks: false
})
const markdown = addBreaklineBetweenBadges({ markdownContent: md })
const sanitizeMd = replaceBadgesMarkdownToHtml({ markdownContent: markdown })
await copyToClipboard(sanitizeMd)
}}
>
{!clicked ? (
<Code2 className='size-5 mr-1 group-hover:animate-shaking' />
) : (
<Check className='size-5 mr-1' />
)}
Copy Code
</Button>
)
}