1
1
/** @jsx jsx */
2
2
import { jsx } from '@emotion/core'
3
3
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'
5
6
import {
6
7
Switch ,
7
8
Route ,
@@ -14,15 +15,19 @@ import tw from 'twin.macro'
14
15
import styled from '@emotion/styled/macro'
15
16
import store from 'store2'
16
17
import ReactGA from 'react-ga'
17
- import { ToastContainer as OriginalToastContainer } from 'react-toastify'
18
+ import { toast , ToastContainer as OriginalToastContainer } from 'react-toastify'
18
19
import { SWRConfig } from 'swr'
19
20
import 'react-toastify/dist/ReactToastify.css'
20
21
21
22
import FullLoading from './components/FullLoading'
22
23
import NewVersionAlert from './components/NewVersionAlert'
23
24
import ScrollToTop from './components/ScrollToTop'
24
25
import NetworkErrorModal from './components/NetworkErrorModal'
25
- import { useProfile , useProfileDispatch } from './models/profile'
26
+ import {
27
+ usePlatformVersion ,
28
+ useProfile ,
29
+ useProfileDispatch ,
30
+ } from './models/profile'
26
31
import {
27
32
RegularLanding as LandingPage ,
28
33
SurgeLanding as SurgeLandingPage ,
@@ -31,7 +36,12 @@ import IndexPage from './pages/Index'
31
36
import PageLayout from './components/PageLayout'
32
37
import { Profile } from './types'
33
38
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'
35
45
36
46
const PoliciesPage = loadable ( ( ) => import ( './pages/Policies' ) , {
37
47
fallback : < FullLoading /> ,
@@ -121,12 +131,15 @@ if (
121
131
}
122
132
123
133
const App : React . FC = ( ) => {
134
+ const { t, i18n } = useTranslation ( )
124
135
const [ isNetworkModalOpen , setIsNetworkModalOpen ] = useState ( false )
125
136
const location = useLocation ( )
126
137
const history = useHistory ( )
127
138
const profileDispatch = useProfileDispatch ( )
128
139
const profile = useProfile ( )
129
140
const [ hasInit , setHasInit ] = useState ( false )
141
+ const isCurrentVersionFetched = useRef ( true )
142
+ const platformVersion = usePlatformVersion ( )
130
143
131
144
const onCloseApplication = useCallback ( ( ) => {
132
145
if ( isRunInSurge ( ) ) {
@@ -166,6 +179,48 @@ const App: React.FC = () => {
166
179
ReactGA . pageview ( location . pathname )
167
180
} , [ location . pathname ] )
168
181
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
+
169
224
return (
170
225
< SWRConfig
171
226
value = { {
0 commit comments