-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathMarkdownView.tsx
More file actions
39 lines (33 loc) · 1.09 KB
/
MarkdownView.tsx
File metadata and controls
39 lines (33 loc) · 1.09 KB
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
import { FC, useCallback, useMemo, useState } from 'react'
import { trackEvent } from '~app/plausible'
import { ChatMessageModel } from '~types'
import Button from '../Button'
interface Props {
messages: ChatMessageModel[]
}
const MarkdownView: FC<Props> = ({ messages }) => {
const [copied, setCopied] = useState(false)
const content = useMemo(() => {
return messages
.filter((m) => !!m.text)
.map((m) => `## ${m.author}` + '\n\n' + m.text)
.join('\n\n')
}, [messages])
const copy = useCallback(() => {
navigator.clipboard.writeText(content)
setCopied(true)
setTimeout(() => setCopied(false), 500)
trackEvent('share_chat_copy_markdown')
}, [content])
return (
<div className="px-5 pt-3 pb-4 overflow-hidden flex flex-col h-full">
<div className="mb-3">
<Button size="small" text={copied ? 'Copied!' : 'Copy'} onClick={copy} />
</div>
<pre className="text-sm whitespace-pre-wrap text-primary-text p-2 rounded-md overflow-auto h-full bg-secondary">
{content}
</pre>
</div>
)
}
export default MarkdownView