-
-
Notifications
You must be signed in to change notification settings - Fork 128
Expand file tree
/
Copy pathShareButton.tsx
More file actions
55 lines (50 loc) · 1.71 KB
/
Copy pathShareButton.tsx
File metadata and controls
55 lines (50 loc) · 1.71 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { CheckOutlined, LinkOutlined } from '@ant-design/icons'
import IconButton from '@mui/material/IconButton'
import { Tooltip } from 'antd'
import { useCallback, useContext, useMemo, useState } from 'react'
import { useTranslation } from 'react-i18next'
import { useLocation } from 'react-router'
import { GlobalSearchContext } from 'src/model/globalState'
import { PageShareParamsContext } from 'src/model/routeContext'
import { buildShareUrl } from './shareUrl'
export const ShareButton = () => {
const { search } = useContext(GlobalSearchContext)
const { params: pageParams } = useContext(PageShareParamsContext)
const location = useLocation()
const [copied, setCopied] = useState(false)
const { t } = useTranslation()
const shareUrl = useMemo(
() => buildShareUrl(location.pathname, search, pageParams),
[location.pathname, search, pageParams],
)
const handleShare = useCallback(() => {
navigator.clipboard
.writeText(shareUrl)
.then(() => {
setCopied(true)
setTimeout(() => setCopied(false), 2000)
})
.catch(() => {
// clipboard API not available; silent fail
})
}, [shareUrl])
const tooltipTitle = copied ? (
<span>{t('link_copied')}</span>
) : (
<span>
{t('share_link')}
<br />
<span style={{ opacity: 0.75, fontSize: '0.85em', wordBreak: 'break-all' }}>{shareUrl}</span>
</span>
)
return (
<Tooltip title={tooltipTitle} open={copied || undefined} placement="bottomRight">
<IconButton
size="small"
onClick={handleShare}
aria-label={copied ? t('link_copied') : t('share_link')}>
{copied ? <CheckOutlined /> : <LinkOutlined />}
</IconButton>
</Tooltip>
)
}