/
gatsby-browser.js
100 lines (87 loc) · 3 KB
/
gatsby-browser.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
// ./gatsby-browser.js
import React from "react";
import { contains } from 'ramda';
import { silentAuth } from "./src/utils/auth";
import { globalHistory } from '@reach/router';
import i18next from 'i18next';
import SmoothScroll from './src/utils/smoothScroll';
import i18n from "./src/i18n";
import convertLinkLocale from './src/utils/convertLinkLocale';
import supportedLngs from 'src/locales/supportedLngs';
import { MEDIUM_BREAKPOINT } from './src/utils/breakpoints';
import CookiesPolicy from './src/components/CookiesPolicy/CookiesPolicy';
import { initFacebookSDK } from './src/libs/facebook-sdk';
// import SmoothScroll from 'src/utils/smoothScroll';
const LanguageWrapper = ({ children }) => (
<div id="main-wrapper">
{children}
</div>
)
class SessionCheck extends React.Component {
constructor(props) {
super(props)
this.state = {
loading: true,
}
this.handleCheckSession = this.handleCheckSession.bind(this)
}
handleCheckSession = () => {
this.setState({ loading: false })
}
UNSAFE_componentWillMount() {
if(typeof window !== undefined) {
initFacebookSDK();
globalHistory.listen(({ action }) => {
if(action === 'PUSH') {
const { pathname } = window.location;
const pageLngCode = pathname.split('/')[1] === '' ? 'en' : pathname.split('/')[1];
const lngCode = supportedLngs.find(lng => lng === pageLngCode) || 'en';
// i18next.changeLanguage(lngCode);
}
});
}
// Set user pref language
if(typeof window !== undefined && globalHistory.location.pathname.indexOf(i18next.language) == -1 && globalHistory.location.pathname.indexOf('callback') == -1 && i18next.language !== 'en') {
const { pathname } = globalHistory.location;
const pathnameCleared = pathname.replace('-zh-tc', '').replace('-zh', '');
const lastLetterSlash = pathnameCleared[pathnameCleared.length - 1] === '/';
const path = lastLetterSlash ? pathnameCleared.slice(0, pathnameCleared.length - 1) : pathnameCleared;
window.location.pathname = contains('category', path) && i18next.language !== 'en' ?
`${convertLinkLocale(path, i18next.language)}-${i18next.language.replace('_', '-')}` :
convertLinkLocale(path, i18next.language);
}
}
componentDidMount() {
silentAuth(this.handleCheckSession);
if(typeof window !== "undefined" && window.innerWidth > MEDIUM_BREAKPOINT) {
SmoothScroll({
animationTime: 1600,
stepSize: 200,
accelerationDelta: 20,
accelerationMax: 1,
pulseAlgorithm : true,
pulseScale : 3,
pulseNormalize : 1,
});
}
}
render() {
return (
!this.state.loading && (
<React.Fragment>
<LanguageWrapper>
{this.props.children}
</LanguageWrapper>
{/* <CookiesPolicy /> */}
</React.Fragment>
)
)
}
}
export const wrapRootElement = ({ element }) => {
return (
<SessionCheck>
{element}
</SessionCheck>
)
}