import { Button } from './ui'
import { t } from '@/lib/i18n'
import { useToast } from './ui/ToastContainer'
interface ShareButtonsProps {
articleId: string
articleTitle: string
authorPubkey?: string
}
export function ShareButtons({ articleId, articleTitle, authorPubkey }: ShareButtonsProps): React.ReactElement {
const { showToast } = useToast()
const articleUrl = typeof window !== 'undefined' ? `${window.location.origin}/article/${articleId}` : ''
return (
{authorPubkey !== undefined && (
)}
)
}
function CopyLinkButton({
url,
showToast,
}: {
url: string
showToast: (message: string, variant?: 'success' | 'info' | 'warning' | 'error', duration?: number) => void
}): React.ReactElement {
const handleCopy = async (): Promise => {
try {
await navigator.clipboard.writeText(url)
showToast(t('share.copySuccess'), 'success', 2000)
} catch (error) {
console.error('Failed to copy link:', error)
showToast(t('share.copyFailed'), 'error')
}
}
return (
)
}
function handleNostrShare(params: {
articleId: string
authorPubkey: string
articleTitle: string
showToast: (message: string, variant?: 'success' | 'info' | 'warning' | 'error', duration?: number) => void
}): Promise {
const articleUrl = typeof window !== 'undefined' ? `${window.location.origin}/article/${params.articleId}` : ''
const nostrNote = `nostr:${params.authorPubkey}:${params.articleId}`
const shareData: ShareData = {
title: params.articleTitle,
text: params.articleTitle,
url: articleUrl,
}
if (navigator.share !== undefined) {
return handleNativeShare(shareData, params.showToast)
}
return handleClipboardShare(nostrNote, params.showToast)
}
async function handleNativeShare(
shareData: ShareData,
showToast: (message: string, variant?: 'success' | 'info' | 'warning' | 'error', duration?: number) => void
): Promise {
try {
await navigator.share(shareData)
showToast(t('share.shareSuccess'), 'success', 2000)
} catch (error) {
if (error instanceof Error && error.name === 'AbortError') {
return
}
throw error
}
}
async function handleClipboardShare(
nostrNote: string,
showToast: (message: string, variant?: 'success' | 'info' | 'warning' | 'error', duration?: number) => void
): Promise {
await navigator.clipboard.writeText(nostrNote)
showToast(t('share.nostrLinkCopied'), 'success', 2000)
}
function ShareToNostrButton({
articleId,
articleTitle,
authorPubkey,
showToast,
}: {
articleId: string
articleTitle: string
authorPubkey: string
showToast: (message: string, variant?: 'success' | 'info' | 'warning' | 'error', duration?: number) => void
}): React.ReactElement {
const handleShare = async (): Promise => {
try {
await handleNostrShare({ articleId, authorPubkey, articleTitle, showToast })
} catch (error) {
console.error('Failed to share:', error)
showToast(t('share.shareFailed'), 'error')
}
}
return (
)
}