Skip to content

Commit

Permalink
refactor(share): enhance Web Share API implementation with validation
Browse files Browse the repository at this point in the history
  • Loading branch information
NekoAria committed Nov 24, 2024
1 parent a66fef7 commit 871cdeb
Showing 1 changed file with 20 additions and 10 deletions.
30 changes: 20 additions & 10 deletions src/components/Article/ActionButtons.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -139,16 +139,26 @@ const ActionButtons = () => {
]

const handleShare = async () => {
if (navigator.share) {
try {
await navigator.share({
title: activeContent.title,
url: activeContent.url,
})
} catch (error) {
if (error.name !== "AbortError") {
console.error("Error sharing article:", error)
}
if (!navigator.share) {
console.error("Web Share API is not supported")
return
}

const shareData = {
title: activeContent.title,
url: activeContent.url,
}

if (navigator.canShare && !navigator.canShare(shareData)) {
console.error("This content cannot be shared")
return
}

try {
await navigator.share(shareData)
} catch (error) {
if (error.name !== "AbortError") {
console.error("Error sharing article:", error)
}
}
}
Expand Down

0 comments on commit 871cdeb

Please sign in to comment.