-
Notifications
You must be signed in to change notification settings - Fork 2k
/
launcher.jsx
159 lines (136 loc) · 4.37 KB
/
launcher.jsx
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
/**
* External dependencies
*/
import PropTypes from 'prop-types';
import i18n, { localize } from 'i18n-calypso';
import React, { Fragment } from 'react';
import Gridicon from 'components/gridicon';
import { connect } from 'react-redux';
import classNames from 'classnames';
/**
* Internal dependencies
*/
import config from 'config';
import translator, { trackTranslatorStatus } from 'lib/translator-jumpstart';
import localStorageHelper from 'store';
import { Dialog } from '@automattic/components';
import analytics from 'lib/analytics';
import { TranslationScanner } from 'lib/i18n-utils/translation-scanner';
import getUserSettings from 'state/selectors/get-user-settings';
import getOriginalUserSetting from 'state/selectors/get-original-user-setting';
import QueryUserSettings from 'components/data/query-user-settings';
/**
* Style dependencies
*/
import './style.scss';
class TranslatorLauncher extends React.Component {
static propTypes = {
translate: PropTypes.func,
};
static translationScanner =
config.isEnabled( 'i18n/translation-scanner' ) && new TranslationScanner();
static getDerivedStateFromProps( nextProps, prevState ) {
translator.init( nextProps.isUserSettingsReady );
trackTranslatorStatus( nextProps.isTranslatorEnabled );
if ( prevState.isEnabled !== translator.isEnabled() )
return { ...prevState, isEnabled: translator.isEnabled() };
return null;
}
state = {
infoDialogVisible: false,
firstActivation: true,
isActive: translator.isActivated(),
isEnabled: translator.isEnabled(),
};
componentDidMount() {
i18n.on( 'change', this.onI18nChange );
}
componentWillUnmount() {
i18n.off( 'change', this.onI18nChange );
}
onI18nChange = () => {
if ( ! this.state.isActive && translator.isActivated() ) {
// Activating
this.setState( { isActive: true } );
if ( ! localStorageHelper.get( 'translator_hide_infodialog' ) ) {
this.setState( { infoDialogVisible: true } );
}
if ( this.state.firstActivation ) {
analytics.mc.bumpStat( 'calypso_translator_toggle', 'intial_activation' );
this.setState( { firstActivation: false } );
}
} else if ( this.state.isActive && ! translator.isActivated() ) {
// Deactivating
this.setState( { isActive: false } );
}
};
toggleInfoCheckbox = event => {
localStorageHelper.set( 'translator_hide_infodialog', event.target.checked );
};
infoDialogClose = () => {
this.setState( { infoDialogVisible: false } );
};
toggle = event => {
event.preventDefault();
const nextIsActive = translator.toggle();
analytics.mc.bumpStat( 'calypso_translator_toggle', nextIsActive ? 'on' : 'off' );
this.setState( { isActive: nextIsActive } );
};
renderConfirmationModal() {
const { translate } = this.props;
const infoDialogButtons = [ { action: 'cancel', label: translate( 'OK' ) } ];
return (
<Dialog
isVisible
buttons={ infoDialogButtons }
onClose={ this.infoDialogClose }
additionalClassNames="community-translator__modal"
>
<h1>{ translate( 'Community Translator' ) }</h1>
<p>
{ translate(
'You have now enabled the translator. Right click the text to translate it.'
) }
</p>
<p>
<label htmlFor="toggle">
<input type="checkbox" id="toggle" onClick={ this.toggleInfoCheckbox } />
<span>{ translate( "Don't show again" ) }</span>
</label>
</p>
</Dialog>
);
}
render() {
const { translate } = this.props;
const { isEnabled, isActive, infoDialogVisible } = this.state;
const launcherClasses = classNames( 'community-translator', { 'is-active': isActive } );
const toggleString = isActive
? translate( 'Disable Translator' )
: translate( 'Enable Translator' );
return (
<Fragment>
<QueryUserSettings />
{ isEnabled && (
<Fragment>
<div className={ launcherClasses }>
<button
className="community-translator__button"
onClick={ this.toggle }
title={ translate( 'Community Translator' ) }
>
<Gridicon icon="globe" />
<div className="community-translator__text">{ toggleString }</div>
</button>
</div>
{ infoDialogVisible && this.renderConfirmationModal() }
</Fragment>
) }
</Fragment>
);
}
}
export default connect( state => ( {
isUserSettingsReady: !! getUserSettings( state ),
isTranslatorEnabled: getOriginalUserSetting( state, 'enable_translator' ),
} ) )( localize( TranslatorLauncher ) );