forked from snapshot-labs/snapshot-v1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuseSharing.ts
137 lines (119 loc) · 3.67 KB
/
useSharing.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import { useShare } from '@vueuse/core';
import { ExtendedSpace, Proposal } from '@/helpers/interfaces';
export function useSharing() {
const { t } = useI18n();
const sharingItems = [
{
text: 'Twitter',
action: 'shareProposalTwitter',
extras: { icon: 'twitter' }
},
{
text: 'Hey',
action: 'shareProposalHey',
extras: { icon: 'hey' }
},
{
text: t('copyLink'),
action: 'shareToClipboard',
extras: { icon: 'link' }
}
];
function proposalUrl(key, proposal) {
return `https://${window.location.hostname}/#/${key}/proposal/${proposal.id}`;
}
function encodedProposalUrl(key, proposal) {
return encodeURIComponent(proposalUrl(key, proposal));
}
const { share, isSupported } = useShare();
function shareProposal(space, proposal) {
share({
title: '',
text: `${space.name} - ${proposal.title}`,
url: proposalUrl(space.id, proposal)
});
}
function shareVote(
shareTo: 'twitter' | 'hey',
payload: { space: ExtendedSpace; proposal: Proposal; choices: string }
) {
const postText = getSharingText(shareTo, payload);
if (window && shareTo === 'hey') return shareHey(postText);
if (isSupported.value)
return share({
title: '',
text: postText,
url: proposalUrl(payload.space.id, payload.proposal)
});
if (window && shareTo === 'twitter') return shareTwitter(postText);
}
function getSharingText(shareTo: 'twitter' | 'hey', payload): string {
const isSingleChoice =
payload.proposal.type === 'single-choice' ||
payload.proposal.type === 'basic';
const isPrivate = payload.proposal.privacy === 'shutter';
const votedText =
payload.choices && isSingleChoice && !isPrivate
? `I just voted "${payload.choices}" on`
: `I just voted on`;
const spaceHandle = payload.space.twitter
? `@${payload.space.twitter}`
: payload.space.name;
if (shareTo === 'hey')
return `${encodeURIComponent(votedText)}%20"${encodeURIComponent(
payload.proposal.title
)}"%20${encodedProposalUrl(
payload.space.id,
payload.proposal
)}&hashtags=Snapshot`;
if (isSupported.value)
return `${votedText} "${payload.proposal.title}" ${spaceHandle} #Snapshot`;
if (shareTo === 'twitter')
return `${encodeURIComponent(votedText)}%20"${encodeURIComponent(
payload.proposal.title
)}"%20${encodedProposalUrl(
payload.space.id,
payload.proposal
)}%20${spaceHandle}%20%23Snapshot`;
return `${votedText} "${payload.proposal.title}"`;
}
function shareTwitter(text) {
const url = `https://twitter.com/intent/tweet?text=${text}`;
window.open(url, '_blank')?.focus();
}
function shareHey(text) {
const url = `https://hey.xyz/?text=${text}`;
window.open(url, '_blank')?.focus();
}
function shareProposalTwitter(space, proposal) {
const handle = space.twitter ? `@${space.twitter}` : space.name;
shareTwitter(
`${encodeURIComponent(proposal.title)}%20${encodedProposalUrl(
space.id,
proposal
)}%20${handle}%20%23Snapshot`
);
}
function shareProposalHey(space, proposal) {
shareHey(
`${encodeURIComponent(proposal.title)}%20${encodedProposalUrl(
space.id,
proposal
)}&hashtags=Snapshot`
);
}
const { copyToClipboard } = useCopy();
function shareToClipboard(space, proposal) {
copyToClipboard(proposalUrl(space.id, proposal));
}
return {
shareProposalTwitter,
shareProposalHey,
shareToClipboard,
proposalUrl,
shareProposal,
shareVote,
sharingIsSupported: isSupported,
sharingItems
};
}