Skip to content

Instantly share code, notes, and snippets.

@lilpolymath
Created July 4, 2024 19:44
Show Gist options
  • Save lilpolymath/ecf1b0bdf38668d858a383a0787d1b02 to your computer and use it in GitHub Desktop.
Save lilpolymath/ecf1b0bdf38668d858a383a0787d1b02 to your computer and use it in GitHub Desktop.
Helper function to help share text to social media platforms.
const buildTweet = ({
pageUrl,
tweetContent,
}: {
tweetContent: string | undefined;
pageUrl: string;
}) => {
if (tweetContent !== undefined) {
const characterTotal = tweetContent.length + pageUrl.length;
if (characterTotal + 19 < 280) {
return encodeURIComponent(`"${tweetContent}" – `);
}
const amountToTrim = 280 - (pageUrl.length + 19);
return encodeURIComponent(`"${tweetContent.slice(0, amountToTrim)}…" – `);
}
return '';
};
export const buildTwitterTargetLink = (quote: string, pageUrl: string) => {
const twitterLink = (tweetContent: string) =>
`https://twitter.com/intent/tweet?text=${tweetContent}&url=${pageUrl}`;
const tweet = buildTweet({
pageUrl,
tweetContent: quote,
});
return twitterLink(tweet);
};
type ShareTextProps = {
text: string;
url: string;
platform: 'reddit' | 'twitter' | 'facebook' | 'linkedin' | 'whatsapp';
};
export const shareText = (props: ShareTextProps): string => {
let url = '';
switch (props.platform) {
case 'twitter':
url = buildTwitterTargetLink(props.text, props.url);
break;
case 'facebook':
url =
'http://www.facebook.com/sharer.php?u=' +
props.text +
'&quote=' +
encodeURIComponent(props.url);
break;
case 'linkedin':
url =
'https://www.linkedin.com/sharing/share-offsite?url=' +
encodeURIComponent(props.url) +
'&title=' +
props.text;
break;
case 'whatsapp':
url =
'https://api.whatsapp.com/send?text=' +
props.text +
encodeURIComponent(props.url);
break;
case 'reddit':
url =
'https://www.reddit.com/submit?url=' +
encodeURIComponent(props.url) +
'&title=' +
props.text;
}
return url;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment