Skip to content

Commit 7d48e43

Browse files
committed
feat: remember the language choice
1 parent 86ed610 commit 7d48e43

File tree

3 files changed

+66
-6
lines changed

3 files changed

+66
-6
lines changed

src/App.tsx

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
/** @jsx jsx */
22
import { jsx } from '@emotion/core'
33
import { find } from 'lodash-es'
4-
import React, { useCallback, useEffect, useState } from 'react'
4+
import React, { useCallback, useEffect, useRef, useState } from 'react'
5+
import { useTranslation } from 'react-i18next'
56
import {
67
Switch,
78
Route,
@@ -14,15 +15,19 @@ import tw from 'twin.macro'
1415
import styled from '@emotion/styled/macro'
1516
import store from 'store2'
1617
import ReactGA from 'react-ga'
17-
import { ToastContainer as OriginalToastContainer } from 'react-toastify'
18+
import { toast, ToastContainer as OriginalToastContainer } from 'react-toastify'
1819
import { SWRConfig } from 'swr'
1920
import 'react-toastify/dist/ReactToastify.css'
2021

2122
import FullLoading from './components/FullLoading'
2223
import NewVersionAlert from './components/NewVersionAlert'
2324
import ScrollToTop from './components/ScrollToTop'
2425
import NetworkErrorModal from './components/NetworkErrorModal'
25-
import { useProfile, useProfileDispatch } from './models/profile'
26+
import {
27+
usePlatformVersion,
28+
useProfile,
29+
useProfileDispatch,
30+
} from './models/profile'
2631
import {
2732
RegularLanding as LandingPage,
2833
SurgeLanding as SurgeLandingPage,
@@ -31,7 +36,12 @@ import IndexPage from './pages/Index'
3136
import PageLayout from './components/PageLayout'
3237
import { Profile } from './types'
3338
import { isRunInSurge } from './utils'
34-
import { ExistingProfiles, LastUsedProfile } from './utils/constant'
39+
import {
40+
ExistingProfiles,
41+
LastUsedLanguage,
42+
LastUsedProfile,
43+
} from './utils/constant'
44+
import { httpClient } from './utils/fetcher'
3545

3646
const PoliciesPage = loadable(() => import('./pages/Policies'), {
3747
fallback: <FullLoading />,
@@ -121,12 +131,15 @@ if (
121131
}
122132

123133
const App: React.FC = () => {
134+
const { t, i18n } = useTranslation()
124135
const [isNetworkModalOpen, setIsNetworkModalOpen] = useState(false)
125136
const location = useLocation()
126137
const history = useHistory()
127138
const profileDispatch = useProfileDispatch()
128139
const profile = useProfile()
129140
const [hasInit, setHasInit] = useState(false)
141+
const isCurrentVersionFetched = useRef(true)
142+
const platformVersion = usePlatformVersion()
130143

131144
const onCloseApplication = useCallback(() => {
132145
if (isRunInSurge()) {
@@ -166,6 +179,48 @@ const App: React.FC = () => {
166179
ReactGA.pageview(location.pathname)
167180
}, [location.pathname])
168181

182+
useEffect(() => {
183+
const language: string | null = store.get(LastUsedLanguage)
184+
185+
if (language && language !== i18n.language) {
186+
i18n.changeLanguage(language)
187+
}
188+
}, [i18n])
189+
190+
useEffect(() => {
191+
if (
192+
!profile?.platform ||
193+
!isCurrentVersionFetched.current ||
194+
location.pathname === '/'
195+
) {
196+
return
197+
}
198+
199+
httpClient
200+
.request({
201+
url: '/environment',
202+
method: 'GET',
203+
})
204+
.then((res) => {
205+
const currentPlatformVersion = res.headers['x-surge-version']
206+
207+
if (currentPlatformVersion !== platformVersion) {
208+
profileDispatch({
209+
type: 'updatePlatformVersion',
210+
payload: {
211+
platformVersion: currentPlatformVersion,
212+
},
213+
})
214+
}
215+
216+
isCurrentVersionFetched.current = false
217+
})
218+
.catch((err) => {
219+
console.error(err)
220+
toast.error(t('common.surge_too_old'))
221+
})
222+
}, [location, platformVersion, profile?.platform, profileDispatch, t])
223+
169224
return (
170225
<SWRConfig
171226
value={{

src/components/ChangeLanguage.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
/** @jsx jsx */
22
import { jsx } from '@emotion/core'
33
import { Select } from '@sumup/circuit-ui'
4-
import { ChangeEventHandler, useCallback, useState } from 'react'
4+
import { ChangeEventHandler, useCallback, useEffect, useState } from 'react'
55
import { useTranslation } from 'react-i18next'
6+
import store from 'store2'
67
import tw from 'twin.macro'
78
import css from '@emotion/css/macro'
89

10+
import { LastUsedLanguage } from '../utils/constant'
11+
912
const ChangeLanguage = (): JSX.Element => {
10-
const { t, i18n } = useTranslation()
13+
const { i18n } = useTranslation()
1114
const options = [
1215
{
1316
value: 'en',
@@ -23,6 +26,7 @@ const ChangeLanguage = (): JSX.Element => {
2326
const onChange: ChangeEventHandler<HTMLSelectElement> = useCallback(
2427
(e) => {
2528
setIsLoading(true)
29+
store.set(LastUsedLanguage, e.target.value)
2630
i18n.changeLanguage(e.target.value).finally(() => setIsLoading(false))
2731
},
2832
[i18n],

src/utils/constant.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export const ExistingProfiles = 'existingProfiles'
22
export const LastUsedProfile = 'lastUsedProfile'
33
export const LastUsedVersion = 'lastUsedVersion'
4+
export const LastUsedLanguage = 'lastUsedLanguage'

0 commit comments

Comments
 (0)