-
Notifications
You must be signed in to change notification settings - Fork 80
/
ShareModal.tsx
186 lines (161 loc) Β· 5.34 KB
/
ShareModal.tsx
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import * as React from 'react'
import { env } from 'decentraland-commons'
import { Loader, ModalNavigation } from 'decentraland-ui'
import Modal from 'decentraland-dapps/dist/containers/Modal'
import { t } from 'decentraland-dapps/dist/modules/translation/utils'
import { locations } from 'routing/locations'
import Icon from 'components/Icon'
import { ShareTarget } from 'modules/ui/share/types'
import LoginModal from '../LoginModal'
import { Props, ShareModalType, State } from './ShareModal.types'
import './ShareModal.css'
const SHARE_SCENE_URL = env.get('REACT_APP_SHARE_SCENE_URL', '')
export default class ShareModal extends React.PureComponent<Props, State> {
state: State = {
copied: false,
type: ShareModalType.PROJECT,
id: null
}
input = React.createRef<HTMLInputElement>()
componentDidMount() {
const { metadata, onUpdate, isLoggedIn } = this.props
if (metadata) {
this.setState({
type: metadata.type,
id: metadata.id
})
if (isLoggedIn) {
onUpdate(metadata.id)
}
}
}
handleClose = () => {
this.props.onClose()
}
handleFocusLink = () => {
if (this.input.current) {
this.input.current.select()
}
}
handleCopyLink = (e: React.MouseEvent<HTMLAnchorElement>) => {
e.preventDefault()
if (this.input.current) {
const input = this.input.current
input.select()
document.execCommand('copy')
const selection = document.getSelection()
if (selection) {
selection.removeAllRanges()
input.blur()
this.setState({ copied: true })
}
this.props.onShare(ShareTarget.LINK)
}
}
handleShare = (e: React.MouseEvent<HTMLAnchorElement>, target: ShareTarget) => {
e.preventDefault()
const width = 600
const height = 250
const top = Math.ceil(window.outerHeight / 2 - height / 2)
const left = Math.ceil(window.outerWidth / 2 - width / 2)
window.open(
e.currentTarget.href,
'targetWindow',
`toolbar=no,location=0,status=no,menubar=no,scrollbars=yes,resizable=yes,width=${width},height=${height},top=${top},left=${left}`
)
this.props.onShare(target)
}
handleShareWithFacebook = (e: React.MouseEvent<HTMLAnchorElement>) => this.handleShare(e, ShareTarget.FACEBOOK)
handleShareWithTwitter = (e: React.MouseEvent<HTMLAnchorElement>) => this.handleShare(e, ShareTarget.TWITTER)
handleLogin = () => {
const { project, name, metadata, onLogin } = this.props
onLogin({
returnUrl: locations.editor(project.id),
openModal: {
name,
metadata
}
})
}
getFacebookLink = () => {
const { project } = this.props
const url = this.getShareLink()
return encodeURI(t('share_modal.uri.facebook', { ...project, url }))
}
getTwitterLink = () => {
const { project } = this.props
const url = this.getShareLink()
return encodeURI(t('share_modal.uri.twitter', { ...project, url }))
}
getShareLink = () => {
const { project } = this.props
const { type } = this.state
switch (type) {
case ShareModalType.PROJECT:
return SHARE_SCENE_URL + `/scene/${project.id}`
default:
return SHARE_SCENE_URL + `/${type}/${project.id}`
}
}
renderLogin() {
return (
<LoginModal
name={this.props.name}
onClose={this.props.onClose}
onLogin={this.handleLogin}
title={t('share_modal.sign_in.title')}
subtitle={t('share_modal.sign_in.subtitle')}
callToAction={t('global.sign_in')}
/>
)
}
renderLoading() {
const { name } = this.props
return (
<Modal name={name} onClose={this.handleClose}>
<Loader size="large" />
</Modal>
)
}
render() {
const { name, project, isLoading, isLoggedIn, isScreenshotReady } = this.props
const { copied } = this.state
if (!isLoggedIn) {
return this.renderLogin()
}
if (isLoading || !project.isPublic) {
return this.renderLoading()
}
return (
<Modal name={name} onClose={this.handleClose}>
<ModalNavigation title={t('share_modal.title')} subtitle={t('share_modal.description')} onClose={this.handleClose} />
<div className="share-modal">
<div
className="thumbnail"
style={{
backgroundImage: isScreenshotReady ? `url("${project.thumbnail}")` : '',
backgroundSize: 'cover',
backgroundPosition: 'center'
}}
>
{!isScreenshotReady && <Loader size="small" />}
</div>
<div className="button-group">
<a className="button facebook" onClick={this.handleShareWithFacebook} href={this.getFacebookLink()}>
<Icon name="facebook" /> {t(`global.share`)}
</a>
<a className="button twitter" onClick={this.handleShareWithTwitter} href={this.getTwitterLink()}>
<Icon name="twitter" /> {t(`global.share`)}
</a>
</div>
<div className="copy-group">
<input ref={this.input} className="copy-input" readOnly={true} value={this.getShareLink()} onFocus={this.handleFocusLink} />
<a className="copy-button" onClick={this.handleCopyLink} href={this.getShareLink()}>
{copied ? t(`share_modal.copied`) : t(`share_modal.copy`)}
</a>
</div>
</div>
</Modal>
)
}
}