/
LocalStorageToast.tsx
66 lines (57 loc) 路 1.86 KB
/
LocalStorageToast.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
import * as React from 'react'
import { Close } from 'decentraland-ui'
import { getLocalStorage } from 'decentraland-dapps/dist/lib/localStorage'
import { t, T } from 'decentraland-dapps/dist/modules/translation/utils'
import { getAnalytics } from 'decentraland-dapps/dist/modules/analytics/utils'
import { locations } from 'routing/locations'
import { Props, State } from './LocalStorageToast.types'
import './LocalStorageToast.css'
export const LOCAL_STORAGE_SIGN_IN_EVENT = 'Sign in from editor toast'
export const LOCALSTORAGE_TOAST_KEY = 'builder-localstorage-signin-toast'
const localStorage = getLocalStorage()
export default class LocalStorageToast extends React.PureComponent<Props, State> {
state = {
isVisible: !localStorage.getItem(LOCALSTORAGE_TOAST_KEY) && this.props.isVisible
}
analytics = getAnalytics()
componentWillReceiveProps(nextProps: Props) {
if (nextProps.isVisible && !localStorage.getItem(LOCALSTORAGE_TOAST_KEY)) {
this.setState({ isVisible: true })
}
}
handleClose = () => {
this.setState({ isVisible: false })
localStorage.setItem(LOCALSTORAGE_TOAST_KEY, '1')
}
handleLogin = () => {
const { project, onLogin } = this.props
if (project) {
this.analytics.track(LOCAL_STORAGE_SIGN_IN_EVENT)
onLogin({
returnUrl: locations.editor(project.id)
})
}
}
render() {
if (!this.state.isVisible) {
return null
}
return (
<div className="LocalStorageToast">
<T
id="toasts.localstorage"
values={{
sign_in: (
<span className="sign-in" onClick={this.handleLogin}>
{t('global.sign_in')}
</span>
)
}}
/>
<div className="close" onClick={this.handleClose} title={t('global.dismiss')}>
<Close small />
</div>
</div>
)
}
}