2024-02-04 11:25:21 +00:00
|
|
|
import { FACEBOOK, TELEGRAM, TWITTER, VK, createSocialShare } from '@solid-primitives/share'
|
2024-01-05 19:31:28 +00:00
|
|
|
import { clsx } from 'clsx'
|
2024-02-04 11:25:21 +00:00
|
|
|
import { Show, createSignal } from 'solid-js'
|
2024-01-05 19:31:28 +00:00
|
|
|
|
|
|
|
import { useLocalize } from '../../../context/localize'
|
|
|
|
import { useSnackbar } from '../../../context/snackbar'
|
|
|
|
import { Icon } from '../Icon'
|
|
|
|
import { Popover } from '../Popover'
|
|
|
|
|
|
|
|
import styles from './ShareLinks.module.scss'
|
|
|
|
|
|
|
|
type Props = {
|
|
|
|
title: string
|
|
|
|
description: string
|
|
|
|
shareUrl: string
|
|
|
|
imageUrl?: string
|
|
|
|
class?: string
|
|
|
|
variant: 'inModal' | 'inPopup'
|
2024-01-10 10:01:02 +00:00
|
|
|
onShareClick?: () => void
|
2024-01-05 19:31:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const ShareLinks = (props: Props) => {
|
|
|
|
const { t } = useLocalize()
|
|
|
|
const [isLinkCopied, setIsLinkCopied] = createSignal(false)
|
2024-01-08 13:02:52 +00:00
|
|
|
|
2024-01-05 19:31:28 +00:00
|
|
|
const {
|
|
|
|
actions: { showSnackbar },
|
|
|
|
} = useSnackbar()
|
|
|
|
|
|
|
|
const [share] = createSocialShare(() => ({
|
|
|
|
title: props.title,
|
|
|
|
url: props.shareUrl,
|
|
|
|
description: props.description,
|
|
|
|
}))
|
2024-01-10 10:01:02 +00:00
|
|
|
|
|
|
|
const handleShare = (network) => {
|
|
|
|
share(network)
|
|
|
|
if (props.variant === 'inModal') {
|
|
|
|
props.onShareClick()
|
|
|
|
}
|
|
|
|
}
|
2024-01-05 19:31:28 +00:00
|
|
|
const copyLink = async () => {
|
|
|
|
await navigator.clipboard.writeText(props.shareUrl)
|
|
|
|
if (props.variant === 'inModal') {
|
|
|
|
setIsLinkCopied(true)
|
2024-01-10 10:01:02 +00:00
|
|
|
setTimeout(() => {
|
|
|
|
setIsLinkCopied(false)
|
|
|
|
props.onShareClick()
|
|
|
|
}, 3000)
|
2024-01-05 19:31:28 +00:00
|
|
|
} else {
|
|
|
|
showSnackbar({ body: t('Link copied') })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div class={clsx(styles.ShareLinks, props.class, { [styles.inModal]: props.variant === 'inModal' })}>
|
|
|
|
<ul class="nodash">
|
|
|
|
<li>
|
2024-01-10 10:01:02 +00:00
|
|
|
<button role="button" class={styles.shareControl} onClick={() => handleShare(FACEBOOK)}>
|
2024-01-05 19:31:28 +00:00
|
|
|
<Icon name="facebook-white" class={styles.icon} />
|
|
|
|
Facebook
|
|
|
|
</button>
|
|
|
|
</li>
|
|
|
|
<li>
|
2024-01-10 10:01:02 +00:00
|
|
|
<button role="button" class={styles.shareControl} onClick={() => handleShare(TWITTER)}>
|
2024-01-05 19:31:28 +00:00
|
|
|
<Icon name="twitter-white" class={styles.icon} />
|
|
|
|
Twitter
|
|
|
|
</button>
|
|
|
|
</li>
|
|
|
|
<li>
|
2024-01-10 10:01:02 +00:00
|
|
|
<button role="button" class={styles.shareControl} onClick={() => handleShare(TELEGRAM)}>
|
2024-01-05 19:31:28 +00:00
|
|
|
<Icon name="telegram-white" class={styles.icon} />
|
|
|
|
Telegram
|
|
|
|
</button>
|
|
|
|
</li>
|
|
|
|
<li>
|
2024-01-10 10:01:02 +00:00
|
|
|
<button role="button" class={styles.shareControl} onClick={() => handleShare(VK)}>
|
2024-01-05 19:31:28 +00:00
|
|
|
<Icon name="vk-white" class={styles.icon} />
|
|
|
|
VK
|
|
|
|
</button>
|
|
|
|
</li>
|
|
|
|
<li>
|
|
|
|
<Show
|
|
|
|
when={props.variant === 'inModal'}
|
|
|
|
fallback={
|
|
|
|
<button role="button" class={styles.shareControl} onClick={copyLink}>
|
|
|
|
<Icon name="link-white" class={styles.icon} />
|
|
|
|
{t('Copy link')}
|
|
|
|
</button>
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<form class={clsx('pretty-form__item', styles.linkInput)}>
|
|
|
|
<input type="text" name="link" readonly value={props.shareUrl} />
|
|
|
|
<label for="link">{t('Copy link')}</label>
|
|
|
|
|
|
|
|
<Popover content={t('Copy link')}>
|
|
|
|
{(triggerRef: (el) => void) => (
|
|
|
|
<div class={styles.copyButton} onClick={copyLink} ref={triggerRef}>
|
|
|
|
<Icon name="copy" class={styles.icon} />
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</Popover>
|
|
|
|
|
|
|
|
<Show when={isLinkCopied()}>
|
|
|
|
<div class={styles.isCopied}>{t('Link copied to clipboard')}</div>
|
|
|
|
</Show>
|
|
|
|
</form>
|
|
|
|
</Show>
|
|
|
|
</li>
|
|
|
|
</ul>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|