-
Notifications
You must be signed in to change notification settings - Fork 396
/
index.js
220 lines (195 loc) · 6.73 KB
/
index.js
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/* @flow */
import invariant from 'invariant';
import * as React from 'react';
import Helmet from 'react-helmet';
import { connect } from 'react-redux';
import { compose } from 'redux';
import serialize from 'serialize-javascript';
import HeadLinks from 'amo/components/HeadLinks';
import HeadMetaTags from 'amo/components/HeadMetaTags';
import {
ADDON_TYPE_DICT,
ADDON_TYPE_EXTENSION,
ADDON_TYPE_LANG,
ADDON_TYPE_OPENSEARCH,
ADDON_TYPE_STATIC_THEME,
CLIENT_APP_ANDROID,
} from 'core/constants';
import translate from 'core/i18n/translate';
import { getPreviewImage } from 'core/imageUtils';
import { getVersionById, getVersionInfo } from 'core/reducers/versions';
import { getAddonJsonLinkedData } from 'core/utils/addons';
import type { AppState } from 'amo/store';
import type { AddonVersionType, VersionInfoType } from 'core/reducers/versions';
import type { I18nType } from 'core/types/i18n';
import type { AddonType } from 'core/types/addons';
type Props = {|
addon: AddonType | null,
|};
type InternalProps = {|
...Props,
_getAddonJsonLinkedData: typeof getAddonJsonLinkedData,
clientApp: string,
currentVersion: AddonVersionType | null,
i18n: I18nType,
lang: string,
versionInfo: VersionInfoType | null,
|};
export class AddonHeadBase extends React.Component<InternalProps> {
static defaultProps = {
_getAddonJsonLinkedData: getAddonJsonLinkedData,
};
getPageTitle() {
const { addon, clientApp, i18n, lang } = this.props;
invariant(addon, 'addon is required');
const i18nValues = {
addonName: addon.name,
locale: lang,
};
let localizedTitle;
if (clientApp === CLIENT_APP_ANDROID) {
switch (addon.type) {
case ADDON_TYPE_DICT:
// translators: please keep the fox emoji next to "Firefox Android".
localizedTitle = i18n.gettext(`%(addonName)s – Get this Dictionary
for 🦊 Firefox Android (%(locale)s)`);
break;
case ADDON_TYPE_EXTENSION:
// translators: please keep the fox emoji next to "Firefox Android".
localizedTitle = i18n.gettext(`%(addonName)s – Get this Extension for
🦊 Firefox Android (%(locale)s)`);
break;
case ADDON_TYPE_LANG:
// translators: please keep the fox emoji next to "Firefox Android".
localizedTitle = i18n.gettext(`%(addonName)s – Get this Language Pack
for 🦊 Firefox Android (%(locale)s)`);
break;
case ADDON_TYPE_STATIC_THEME:
// translators: please keep the fox emoji next to "Firefox Android".
localizedTitle = i18n.gettext(
`%(addonName)s – Get this Theme for 🦊 Firefox Android (%(locale)s)`,
);
break;
case ADDON_TYPE_OPENSEARCH:
// translators: please keep the fox emoji next to "Firefox Android".
localizedTitle = i18n.gettext(`%(addonName)s – Get this Search Tool
for 🦊 Firefox Android (%(locale)s)`);
break;
default:
// translators: please keep the fox emoji next to "Firefox Android".
localizedTitle = i18n.gettext(`%(addonName)s – Get this Add-on for 🦊
Firefox Android (%(locale)s)`);
}
} else {
switch (addon.type) {
case ADDON_TYPE_DICT:
// translators: please keep the fox emoji next to "Firefox".
localizedTitle = i18n.gettext(`%(addonName)s – Get this Dictionary
for 🦊 Firefox (%(locale)s)`);
break;
case ADDON_TYPE_EXTENSION:
// translators: please keep the fox emoji next to "Firefox".
localizedTitle = i18n.gettext(`%(addonName)s – Get this Extension for
🦊 Firefox (%(locale)s)`);
break;
case ADDON_TYPE_LANG:
// translators: please keep the fox emoji next to "Firefox".
localizedTitle = i18n.gettext(`%(addonName)s – Get this Language Pack
for 🦊 Firefox (%(locale)s)`);
break;
case ADDON_TYPE_STATIC_THEME:
// translators: please keep the fox emoji next to "Firefox".
localizedTitle = i18n.gettext(`%(addonName)s – Get this Theme for 🦊
Firefox (%(locale)s)`);
break;
case ADDON_TYPE_OPENSEARCH:
// translators: please keep the fox emoji next to "Firefox".
localizedTitle = i18n.gettext(`%(addonName)s – Get this Search Tool
for 🦊 Firefox (%(locale)s)`);
break;
default:
// translators: please keep the fox emoji next to "Firefox".
localizedTitle = i18n.gettext(`%(addonName)s – Get this Add-on for 🦊
Firefox (%(locale)s)`);
}
}
return i18n.sprintf(localizedTitle, i18nValues);
}
getPageDescription() {
const { addon, i18n } = this.props;
invariant(addon, 'addon is required');
return i18n.sprintf(
i18n.gettext('Download %(addonName)s for Firefox. %(summary)s'),
{
addonName: addon.name,
summary: addon.summary,
},
);
}
render() {
const {
_getAddonJsonLinkedData,
addon,
currentVersion,
versionInfo,
} = this.props;
invariant(_getAddonJsonLinkedData, '_getAddonJsonLinkedData is required.');
if (!addon) {
return null;
}
const lastUpdated = versionInfo && versionInfo.created;
return (
<>
<Helmet titleTemplate={null}>
<title>{this.getPageTitle()}</title>
<script type="application/ld+json">
{serialize(_getAddonJsonLinkedData({ addon, currentVersion }), {
isJSON: true,
})}
</script>
</Helmet>
<HeadMetaTags
appendDefaultTitle={false}
date={addon.created}
description={this.getPageDescription()}
image={getPreviewImage(addon)}
lastModified={lastUpdated}
title={this.getPageTitle()}
withTwitterMeta={addon.type === ADDON_TYPE_EXTENSION}
/>
<HeadLinks />
</>
);
}
}
const mapStateToProps = (state: AppState, ownProps: InternalProps) => {
const { addon, i18n } = ownProps;
const { clientApp, lang } = state.api;
let currentVersion = null;
let versionInfo = null;
if (addon && addon.currentVersionId) {
currentVersion = getVersionById({
id: addon.currentVersionId,
state: state.versions,
});
}
if (currentVersion) {
versionInfo = getVersionInfo({
i18n,
state: state.versions,
userAgentInfo: state.api.userAgentInfo,
versionId: currentVersion.id,
});
}
return {
clientApp,
currentVersion,
lang,
versionInfo,
};
};
const AddonHead: React.ComponentType<Props> = compose(
translate(),
connect(mapStateToProps),
)(AddonHeadBase);
export default AddonHead;