@@ -12,12 +12,16 @@ import { SecondaryButton } from "../styles";
12
12
import useSnackbar from "../SnackbarContext" ;
13
13
import { useSubstitutedTranslation } from "../util" ;
14
14
import { useState } from "react"
15
+ import useElection from '../ElectionContextProvider' ;
16
+
15
17
import IosShareIcon from '@mui/icons-material/IosShare' ;
16
18
17
19
export default function ShareButton ( { url } : { url : string } ) {
18
20
const { setSnack } = useSnackbar ( )
19
21
const [ anchorElNav , setAnchorElNav ] = useState ( null )
20
22
23
+ const { election } = useElection ( ) ;
24
+
21
25
const { t} = useSubstitutedTranslation ( ) ;
22
26
23
27
const handleOpenNavMenu = ( event ) => {
@@ -35,19 +39,36 @@ export default function ShareButton({ url }: { url: string }) {
35
39
const encodedAhref = encodeURIComponent ( ahref )
36
40
let link
37
41
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
+
38
55
switch ( e . currentTarget . id ) {
39
56
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 ;
43
62
44
63
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 ;
48
68
49
69
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 } ` ;
51
72
open ( link )
52
73
break
53
74
0 commit comments