webapp/src/components/Article/SharePopup.tsx

86 lines
2.6 KiB
TypeScript
Raw Normal View History

2022-11-14 17:41:05 +00:00
import { Icon } from '../_shared/Icon'
2023-02-17 09:21:02 +00:00
import { createSocialShare, TWITTER, VK, FACEBOOK, TELEGRAM } from '@solid-primitives/share'
2022-11-20 21:25:59 +00:00
import styles from '../_shared/Popup/Popup.module.scss'
2022-11-14 10:02:08 +00:00
import type { PopupProps } from '../_shared/Popup'
import { Popup } from '../_shared/Popup'
2023-02-17 09:21:02 +00:00
import { useLocalize } from '../../context/localize'
import { createEffect, createSignal } from 'solid-js'
import { useSnackbar } from '../../context/snackbar'
2022-10-25 15:36:32 +00:00
type SharePopupProps = {
title: string
shareUrl?: string
imageUrl: string
description: string
isVisible?: (value: boolean) => void
} & Omit<PopupProps, 'children'>
2022-10-25 15:36:32 +00:00
2023-01-31 13:58:28 +00:00
export const getShareUrl = (params: { pathname?: string } = {}) => {
if (typeof location === 'undefined') return ''
const pathname = params.pathname ?? location.pathname
return location.origin + pathname
}
2022-10-25 15:36:32 +00:00
export const SharePopup = (props: SharePopupProps) => {
2023-02-17 09:21:02 +00:00
const { t } = useLocalize()
const [isVisible, setIsVisible] = createSignal(false)
const {
actions: { showSnackbar }
} = useSnackbar()
createEffect(() => {
if (props.isVisible) {
props.isVisible(isVisible())
}
})
const [share] = createSocialShare(() => ({
title: props.title,
url: props.shareUrl,
description: props.description
}))
const copyLink = async () => {
2023-01-31 13:58:28 +00:00
await navigator.clipboard.writeText(props.shareUrl)
showSnackbar({ body: t('Link copied') })
}
2022-10-25 15:36:32 +00:00
return (
<Popup {...props} variant="bordered" onVisibilityChange={(value) => setIsVisible(value)}>
2022-10-25 15:36:32 +00:00
<ul class="nodash">
<li>
2023-02-08 20:43:59 +00:00
<button role="button" class={styles.shareControl} onClick={() => share(VK)}>
2022-10-25 15:36:32 +00:00
<Icon name="vk-white" class={styles.icon} />
VK
</button>
2022-10-25 15:36:32 +00:00
</li>
<li>
2023-02-08 20:43:59 +00:00
<button role="button" class={styles.shareControl} onClick={() => share(FACEBOOK)}>
2022-10-25 15:36:32 +00:00
<Icon name="facebook-white" class={styles.icon} />
Facebook
</button>
2022-10-25 15:36:32 +00:00
</li>
<li>
2023-02-08 20:43:59 +00:00
<button role="button" class={styles.shareControl} onClick={() => share(TWITTER)}>
2022-10-25 15:36:32 +00:00
<Icon name="twitter-white" class={styles.icon} />
Twitter
</button>
2022-10-25 15:36:32 +00:00
</li>
<li>
2023-02-08 20:43:59 +00:00
<button role="button" class={styles.shareControl} onClick={() => share(TELEGRAM)}>
2022-10-25 15:36:32 +00:00
<Icon name="telegram-white" class={styles.icon} />
Telegram
</button>
2022-10-25 15:36:32 +00:00
</li>
<li>
2023-02-08 20:43:59 +00:00
<button role="button" class={styles.shareControl} onClick={copyLink}>
2022-10-25 15:36:32 +00:00
<Icon name="link-white" class={styles.icon} />
{t('Copy link')}
</button>
2022-10-25 15:36:32 +00:00
</li>
</ul>
</Popup>
)
}