Skip to content

Commit 60f1a5d

Browse files
authored
Merge pull request #960 from jacksonloper/feat/flavortext-for-share
feat: supply titles when sharing to X and reddit
2 parents 3df3865 + c03fec6 commit 60f1a5d

File tree

1 file changed

+28
-7
lines changed

1 file changed

+28
-7
lines changed

packages/frontend/src/components/Election/ShareButton.tsx

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,16 @@ import { SecondaryButton } from "../styles";
1212
import useSnackbar from "../SnackbarContext";
1313
import { useSubstitutedTranslation } from "../util";
1414
import { useState } from "react"
15+
import useElection from '../ElectionContextProvider';
16+
1517
import IosShareIcon from '@mui/icons-material/IosShare';
1618

1719
export default function ShareButton({ url }: { url: string }) {
1820
const { setSnack } = useSnackbar()
1921
const [anchorElNav, setAnchorElNav] = useState(null)
2022

23+
const { election } = useElection();
24+
2125
const {t} = useSubstitutedTranslation();
2226

2327
const handleOpenNavMenu = (event) => {
@@ -35,19 +39,36 @@ export default function ShareButton({ url }: { url: string }) {
3539
const encodedAhref = encodeURIComponent(ahref)
3640
let link
3741

42+
const pageTitle = election?.title || "";
43+
const votingMethods = Array.from(new Set((election?.races || []).map(r => r.voting_method)));
44+
let votingDesc = "";
45+
const termType = election?.settings?.term_type === 'poll' ? 'poll' : 'election';
46+
if (votingMethods.length === 1) {
47+
votingDesc = ` [with the ${votingMethods[0]} voting system]`;
48+
} else if (votingMethods.length === 2) {
49+
votingDesc = ` [including 2 different voting systems: ${votingMethods.join(" and ")}]`;
50+
} else if (votingMethods.length > 2) {
51+
votingDesc = ` [including ${votingMethods.length} different voting systems: ${votingMethods.join(", ")}]`;
52+
}
53+
const shareTitle = encodeURIComponent(`Vote in a new BetterVoting ${termType}: "${pageTitle}"${votingDesc}`);
54+
3855
switch (e.currentTarget.id) {
3956
case "facebook":
40-
link = `https://www.facebook.com/sharer/sharer.php?u=${ahref}`
41-
open(link)
42-
break
57+
// Facebook automatically shows the page prettily, so URL is fine
58+
// Passing "quote" param to prefill text is weirdly spotty anyway
59+
link = `https://www.facebook.com/sharer/sharer.php?u=${encodedAhref}`;
60+
open(link);
61+
break;
4362

4463
case "X":
45-
link = `https://x.com/intent/tweet?url=${encodedAhref}`
46-
open(link)
47-
break
64+
// Reddit uses the "url" and "title" params to prefill submission
65+
link = `https://x.com/intent/tweet?url=${encodedAhref}&text=${shareTitle}`;
66+
open(link);
67+
break;
4868

4969
case "reddit":
50-
link = `https://www.reddit.com/submit?url=${encodedAhref}`
70+
// Reddit uses the "url" and "title" params to prefill submission
71+
link = `https://new.reddit.com/submit?url=${encodedAhref}&title=${shareTitle}`;
5172
open(link)
5273
break
5374

0 commit comments

Comments
 (0)