|
1 | | -import React from 'react'; |
2 | | -import { useBlogPost } from '@docusaurus/plugin-content-blog/client'; |
3 | | -import { FaTwitter, FaLinkedin, FaFacebook } from 'react-icons/fa'; |
4 | | -import './SocialShare.css'; |
5 | | - |
6 | | -const SocialShare = () => { |
7 | | - // Safe hook call |
8 | | - let blogPost; |
9 | | - try { |
10 | | - blogPost = useBlogPost(); |
11 | | - } catch (e) { |
| 1 | +import React, { useEffect, useState } from "react"; |
| 2 | +import useDocusaurusContext from "@docusaurus/useDocusaurusContext"; |
| 3 | +import { FaEnvelope, FaLink, FaLinkedinIn } from "react-icons/fa"; |
| 4 | +import { FaXTwitter } from "react-icons/fa6"; |
| 5 | + |
| 6 | +import "./SocialShare.css"; |
| 7 | + |
| 8 | +const COPY_RESET_DELAY_MS = 2000; |
| 9 | +const COPY_DEFAULT_LABEL = "Copy link"; |
| 10 | +const COPY_SUCCESS_LABEL = "Link copied"; |
| 11 | +const COPY_ERROR_LABEL = "Unable to copy link"; |
| 12 | + |
| 13 | +type SocialShareProps = { |
| 14 | + permalink?: string; |
| 15 | + title?: string; |
| 16 | +}; |
| 17 | + |
| 18 | +export default function SocialShare({ |
| 19 | + permalink, |
| 20 | + title, |
| 21 | +}: SocialShareProps): JSX.Element | null { |
| 22 | + const { siteConfig } = useDocusaurusContext(); |
| 23 | + const [copyState, setCopyState] = useState<"idle" | "copied" | "error">("idle"); |
| 24 | + |
| 25 | + useEffect(() => { |
| 26 | + if (copyState === "idle") { |
| 27 | + return undefined; |
| 28 | + } |
| 29 | + |
| 30 | + const timeout = window.setTimeout(() => setCopyState("idle"), COPY_RESET_DELAY_MS); |
| 31 | + |
| 32 | + return () => window.clearTimeout(timeout); |
| 33 | + }, [copyState]); |
| 34 | + |
| 35 | + if (!permalink || !title) { |
12 | 36 | return null; |
13 | 37 | } |
14 | | - |
15 | | - if (!blogPost) return null; |
16 | | - |
17 | | - const { metadata } = blogPost; |
18 | | - const { permalink, title } = metadata; |
19 | | - const blogUrl = `https://www.recodehive.com${permalink}`; |
20 | | - const shareText = encodeURIComponent(`Check out this article: ${title}`); |
| 38 | + |
| 39 | + const blogUrl = new URL(permalink, siteConfig.url).toString(); |
| 40 | + const shareText = `Check out this article: ${title}`; |
| 41 | + const encodedBlogUrl = encodeURIComponent(blogUrl); |
| 42 | + const encodedShareText = encodeURIComponent(shareText); |
| 43 | + |
| 44 | + const shareLinks = [ |
| 45 | + { |
| 46 | + className: "x", |
| 47 | + href: `https://twitter.com/intent/tweet?text=${encodedShareText}&url=${encodedBlogUrl}`, |
| 48 | + icon: <FaXTwitter aria-hidden="true" />, |
| 49 | + label: "Share on X", |
| 50 | + }, |
| 51 | + { |
| 52 | + className: "linkedin", |
| 53 | + href: `https://www.linkedin.com/sharing/share-offsite/?url=${encodedBlogUrl}`, |
| 54 | + icon: <FaLinkedinIn aria-hidden="true" />, |
| 55 | + label: "Share on LinkedIn", |
| 56 | + }, |
| 57 | + { |
| 58 | + className: "email", |
| 59 | + href: `mailto:?subject=${encodedShareText}&body=${encodeURIComponent(`${shareText}\n\n${blogUrl}`)}`, |
| 60 | + icon: <FaEnvelope aria-hidden="true" />, |
| 61 | + label: "Share by email", |
| 62 | + }, |
| 63 | + ]; |
| 64 | + |
| 65 | + const handleCopyLink = async () => { |
| 66 | + if (typeof navigator === "undefined" || !navigator.clipboard) { |
| 67 | + setCopyState("error"); |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + try { |
| 72 | + await navigator.clipboard.writeText(blogUrl); |
| 73 | + setCopyState("copied"); |
| 74 | + } catch { |
| 75 | + setCopyState("error"); |
| 76 | + } |
| 77 | + }; |
| 78 | + |
| 79 | + const copyLabel = |
| 80 | + copyState === "copied" |
| 81 | + ? COPY_SUCCESS_LABEL |
| 82 | + : copyState === "error" |
| 83 | + ? COPY_ERROR_LABEL |
| 84 | + : COPY_DEFAULT_LABEL; |
21 | 85 |
|
22 | 86 | return ( |
23 | | - <div className="blog-post-share-section"> |
24 | | - <h3 className="share-title">Enjoyed the article? Share it!</h3> |
| 87 | + <section className="blog-post-share-section" aria-label="Share this post"> |
| 88 | + <p className="share-title">Share this post</p> |
25 | 89 | <div className="share-buttons-row"> |
26 | | - <a |
27 | | - href={`https://twitter.com/intent/tweet?text=${shareText}&url=${encodeURIComponent(blogUrl)}`} |
28 | | - target="_blank" |
29 | | - rel="noopener noreferrer" |
30 | | - className="share-btn-large twitter" |
31 | | - title="Share on X (Twitter)" |
| 90 | + {shareLinks.map((link) => ( |
| 91 | + <a |
| 92 | + key={link.label} |
| 93 | + href={link.href} |
| 94 | + target="_blank" |
| 95 | + rel="noopener noreferrer" |
| 96 | + className={`share-btn-circle ${link.className}`} |
| 97 | + title={link.label} |
| 98 | + aria-label={link.label} |
| 99 | + > |
| 100 | + {link.icon} |
| 101 | + </a> |
| 102 | + ))} |
| 103 | + <button |
| 104 | + type="button" |
| 105 | + className={`share-btn-circle copy${copyState === "copied" ? " copied" : ""}${copyState === "error" ? " copy-error" : ""}`} |
| 106 | + onClick={() => { |
| 107 | + void handleCopyLink(); |
| 108 | + }} |
| 109 | + title={copyLabel} |
| 110 | + aria-label={copyLabel} |
32 | 111 | > |
33 | | - <FaTwitter /> <span>Share on X</span> |
34 | | - </a> |
35 | | - <a |
36 | | - href={`https://www.linkedin.com/sharing/share-offsite/?url=${encodeURIComponent(blogUrl)}`} |
37 | | - target="_blank" |
38 | | - rel="noopener noreferrer" |
39 | | - className="share-btn-large linkedin" |
40 | | - title="Share on LinkedIn" |
41 | | - > |
42 | | - <FaLinkedin /> <span>Share on LinkedIn</span> |
43 | | - </a> |
44 | | - <a |
45 | | - href={`https://www.facebook.com/sharer/sharer.php?u=${encodeURIComponent(blogUrl)}`} |
46 | | - target="_blank" |
47 | | - rel="noopener noreferrer" |
48 | | - className="share-btn-large facebook" |
49 | | - title="Share on Facebook" |
50 | | - > |
51 | | - <FaFacebook /> <span>Share on Facebook</span> |
52 | | - </a> |
| 112 | + <FaLink aria-hidden="true" /> |
| 113 | + </button> |
53 | 114 | </div> |
54 | | - </div> |
| 115 | + </section> |
55 | 116 | ); |
56 | | -}; |
57 | | - |
58 | | -export default SocialShare; |
| 117 | +} |
0 commit comments