This repository has been archived by the owner on Jul 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
418 lines (379 loc) · 12.4 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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/** @jsx jsx */
/* global window, navigator */
// eslint-disable-next-line no-unused-vars
import { useEffect, Fragment } from 'react'
import PropTypes from 'prop-types'
import { jsx, useColorMode } from 'theme-ui'
import Helmet from 'react-helmet'
import { Global, css } from '@emotion/core'
import { ToastContainer } from 'react-toastify'
// import i18n from 'i18next'
// import { I18nextProvider, translate } from 'react-i18next'
import Header from '@components/Header'
import ReferrerMessages from '@components/ReferrerMessages'
import Footer from '@components/Footer'
import * as QS from '@gaiama/query-string'
import { toast } from '@src/utils/toast.js'
import '@src/utils/fontawesome'
import {
media,
screenReaderAndFocusable,
focusOutlineNone,
InstagramGradient,
maxWidthContent,
} from '@src/theme'
import { globalStyles } from './global.js'
import './fragments'
import 'react-toastify/dist/ReactToastify.css'
import { getCookie, setCookie } from '@src/utils/cookie.js'
// import { CookieBanner } from '@components/CookieBanner.js'
// const isDev = process.env.NODE_ENV === `development`
// const globalPolyfills = [
// // `IntersectionObserver`,
// `default`,
// `Symbol`,
// ]
// based on https://stackoverflow.com/a/52112155/3484824
const getNavigatorLanguage = () => {
let lang = ``
if (navigator.languages && navigator.languages.length) {
lang = navigator.languages[0]
} else {
lang =
navigator.userLanguage ||
navigator.language ||
navigator.browserLanguage ||
`en`
}
return lang.includes(`-`) ? lang.split(`-`)[0] : lang
}
const getLangFactory = languages => (lang, alternate = false) => {
if (alternate) {
const alternates = languages.filter(l => l.node.frontmatter.id !== lang)
return alternates ? alternates.map(x => x.node.frontmatter) : []
}
const _lang = languages.find(l => l.node.frontmatter.id === lang)
return _lang ? _lang.node.frontmatter : {}
}
const generateMainMenu = items =>
items
.filter(({ node }) => node.frontmatter.menu === `main`)
.map(({ node }) => ({
title: node.frontmatter.title,
titleShort: node.frontmatter.titleShort,
to: node.fields.url,
}))
const generateMetaMenu = ({ translations, getLang, menuItems }) =>
translations
.map(x => (x.fields ? x : { frontmatter: {}, fields: {} }))
.map(x => ({
...getLang(x.frontmatter.lang),
to: x.fields.url,
}))
.concat(
menuItems
.filter(x => x.node.frontmatter.menu === `meta`)
.map(({ node }) => ({
title: node.frontmatter.title,
titleShort: node.frontmatter.titleShort,
icon: node.frontmatter.icon,
to: node.fields.url,
}))
)
// const getI18nStore = (lang, messages) => ({
// [lang]: { translations: messages }
// })
// i18n.init({
// initImmediate: false,
// fallbackLng: `en`,
// ns: [`translations`],
// defaultNS: `translations`,
// debug: false,
// interpolation: {
// escapeValue: false,
// },
// react: {
// wait: false,
// bindI18n: false,
// bindStore: false,
// },
// })
// const injectGTMCode = () => {
// const gtm = document.createElement(`script`)
// gtm.innerHTML = `(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
// new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
// j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
// 'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
// })(window,document,'script','dataLayer','GTM-P2HCKV6');`
// document.head.insertBefore(gtm, document.head.children[1])
// }
const isDev = process.env.NODE_ENV !== `production`
const MainLayout = props => {
const [colorMode, setColorMode] = useColorMode()
const {
pageContext,
wrapperStyles,
data: { site, SiteMeta, languages, homepage, page, menu },
// localPolyfills,
// location,
} = props
const lang = pageContext.lang
const translations = page.fields.translations
const alternate = translations.find(x => x.id !== lang)
useEffect(() => {
const storedLang = getCookie(`nf_lang`)
if (storedLang === lang) return
const navigatorLang = getNavigatorLanguage()
if (navigatorLang !== lang) {
toast.warning(
<Fragment>
<div sx={{ color: `black`, fontWeight: 500 }}>
<span>{SiteMeta.frontmatter.langToast.prefix}</span>
<span sx={{ mx: 1 }}>{alternate.title}?</span>
<span sx={{ textDecoration: `underline` }}>
{SiteMeta.frontmatter.langToast.suffix}
</span>
</div>
</Fragment>,
{
autoClose: 25000,
closeOnClick: false,
onClick: () => {
setCookie(`nf_lang`, alternate.id)
window.location.replace(alternate.to)
},
onClose: () => {
setCookie(`nf_lang`, lang)
},
}
)
}
}, [lang, alternate, SiteMeta.frontmatter.langToast])
useEffect(() => {
function handleInstall(event) {
console.log(`Thank you for installing our app!`, event)
}
window.onappinstalled = handleInstall
if (typeof window !== `undefined`) {
window.GaiAma = {
...window.GaiAma,
accounts: props.data.Accounts.frontmatter.accounts,
}
}
}, [props.data.Accounts.frontmatter.accounts])
isDev && console.log(`DEVMODE`, props)
// const i18nStore = getI18nStore(lang, pageContext.messages)
const getLang = getLangFactory(languages.edges)
const menuItems = menu.edges || []
const mainMenu = generateMainMenu(menuItems)
const metaMenu = generateMetaMenu({
translations,
getLang,
menuItems,
})
const urlParams = QS.parse()
const translationEn = translations.find(t => t.frontmatter.lang === `en`)
// const polyfills = [...globalPolyfills, ...localPolyfills]
const cover = `${site.siteMetadata.siteUrl}${props.cover ||
page.frontmatter?.cover?.publicURL ||
SiteMeta.frontmatter.assets.globalCover.publicURL}`
return (
// <I18nextProvider // i18n={i18n} // initialLanguage={lang} // initialI18nStore={i18nStore} // >
<Fragment>
<Helmet
titleTemplate={`%s ♡ ${site.siteMetadata.title}`}
defaultTitle={page.frontmatter.title}
>
<title>{page.frontmatter.title}</title>
{/* TODO: SVG favicons https://twitter.com/JoshWComeau/status/1241887478627303432 */}
<meta
name="description"
itemProp="description"
content={page.frontmatter.summary || page.frontmatter.excerpt}
/>
<meta
name="apple-mobile-web-app-title"
content={site.siteMetadata.title}
/>
<meta name="application-name" content={site.siteMetadata.title} />
<meta property="og:site_name" content={site.siteMetadata.title} />
<meta
property="og:url"
content={`${site.siteMetadata.siteUrl}${page.fields.url}`}
/>
<meta property="og:locale" content={getLang(lang).lc} />
{getLang(lang, true).map(x => (
<meta property="og:locale:alternate" content={x.lc} key={x.lc} />
))}
<meta
property="og:title"
content={`${page.frontmatter.title} - ${site.siteMetadata.title}`}
/>
<meta property="og:type" content="website" />
<meta property="fb:admins" content="100000166597534" />
<meta
property="og:description"
content={page.frontmatter.summary || page.frontmatter.excerpt}
/>
{[`og:image`, `image`].map(x => (
<meta property={x} key={x} content={cover} />
))}
{/* <meta property="og:image:width" content="1200"> */}
{/* <meta property="og:image:height" content="628"> */}
{/* <meta property="og:image:alt" content={`A shiny red apple with a bite taken out`} /> */}
{/* twitter */}
<meta property="twitter:site" content="hellogaiama" />
<meta name="twitter:card" content="summary_large_image" />
<link
rel="sitemap"
type="application/xml"
href={`/${lang}/sitemap.xml`}
/>
<link
rel="alternate"
href={site.siteMetadata.siteUrl + translationEn.fields.url}
hrefLang="x-default"
/>
{translations.map(({ frontmatter: t, fields }) => (
<link
rel="alternate"
href={`${site.siteMetadata.siteUrl}${fields.url}`}
hrefLang={t.lang}
key={t.lang}
/>
))}
{/* {[`atom.xml`, `rss.xml`, `feed.json`].map(feed => (
<link
rel="alternate"
href={`${site.siteMetadata.siteUrl}/${lang}/blog/${feed}`}
type={
feed.includes(`json`) ? `application/json` : `application/rss+xml`
}
key={feed}
/>
))} */}
{/* {!isDev && (<script src={`https://cdn.polyfill.io/v2/polyfill.min.js?features=${polyfills.join(`,`)}`}/>)} */}
{!isDev && (
<script
src={`https://polyfill.io/v3/polyfill.min.js?features=Object.assign`}
/>
)}
<html lang={lang} />
</Helmet>
<Global styles={globalStyles} />
<a href="#main" css={screenReaderAndFocusable}>
{SiteMeta.frontmatter.skipLinks.toContent}
</a>
<a href="#main-nav" css={screenReaderAndFocusable}>
{SiteMeta.frontmatter.skipLinks.toNav}
</a>
{urlParams.ref && <ReferrerMessages urlParams={urlParams} lang={lang} />}
<Header
homepage={homepage}
meta={metaMenu}
menu={mainMenu}
logo={SiteMeta.frontmatter.assets.logo.image}
bgImage={SiteMeta.frontmatter.assets.headerBg.image}
/>
<main
id="main"
tabIndex="-1"
sx={{
...focusOutlineNone,
margin: `0 auto`,
padding: `3rem 0 1.45rem`,
width: !wrapperStyles.width && `98%`,
...maxWidthContent,
[media.greaterThan(`medium`)]: {
width: !wrapperStyles.width && `90%`,
},
...wrapperStyles,
}}
>
{props.children}
</main>
<Footer
menu={mainMenu.concat(metaMenu.reverse())}
menuTitle={SiteMeta.frontmatter.footer.menuTitle}
socialTitle={SiteMeta.frontmatter.footer.socialTitle}
supportTitle={SiteMeta.frontmatter.footer.supportTitle}
legal={props.data.legal.edges}
bgImage={SiteMeta.frontmatter.assets.headerBg.image}
accounts={props.data.Accounts}
info={SiteMeta.body}
sponsors={SiteMeta.frontmatter.sponsors}
/>
{isDev && (
<div
css={css`
position: fixed;
bottom: 1rem;
left: 1rem;
font-size: 0.5rem;
`}
>
<button
onClick={() =>
setColorMode(colorMode === `dark` ? `light` : `dark`)
}
>
Toggle Theme
</button>
</div>
)}
{/* <CookieBanner onAccept={injectGTMCode}>
{({ setAccept, setReject }) => (
<div
css={css`
position: fixed;
bottom: 0;
left: 0;
right: 0;
color: #fff;
background: ${colors.primaryLite};
padding: 1rem;
z-index: 10;
display: flex;
align-content: center;
justify-content: space-between;
`}
>
<div>
Cookie Banner
<span
css={css`
font-size: smaller;
`}
>
<Link to="/datenschutz">Datenschutz</Link>
</span>
</div>
<div>
<button onClick={setReject}>Reject</button>
<button onClick={setAccept}>Accept</button>
</div>
</div>
)}
</CookieBanner> */}
<InstagramGradient />
<ToastContainer position="top-center" />
</Fragment>
// </I18nextProvider>
)
}
MainLayout.propTypes = {
data: PropTypes.object,
wrapperStyles: PropTypes.object,
pageContext: PropTypes.object,
localPolyfills: PropTypes.array,
cover: PropTypes.string,
location: PropTypes.object,
}
MainLayout.defaultProps = {
wrapperStyles: {},
pageContext: {
lang: `en`,
},
}
// export default translate(`translations`, { i18n })(MainLayout)
export default MainLayout