11/** @jsx jsx */
22import { jsx } from '@emotion/core'
3- import React , { useCallback } from 'react'
3+ import React , { useCallback , useEffect , useRef } from 'react'
44import { Button , Heading , Toggle } from '@sumup/circuit-ui'
55import css from '@emotion/css/macro'
66import { useTranslation } from 'react-i18next'
7+ import { toast } from 'react-toastify'
78import tw from 'twin.macro'
89import { delay } from 'bluebird'
910import { useHistory } from 'react-router-dom'
@@ -19,11 +20,12 @@ import {
1920 usePlatformBuild ,
2021 usePlatformVersion ,
2122 useProfile ,
23+ useProfileDispatch ,
2224} from '../../models/profile'
2325import { Capability } from '../../types'
2426import { isRunInSurge } from '../../utils'
2527import { ExistingProfiles , LastUsedProfile } from '../../utils/constant'
26- import fetcher from '../../utils/fetcher'
28+ import fetcher , { httpClient } from '../../utils/fetcher'
2729import HostInfo from './components/HostInfo'
2830import TrafficCell from './components/TrafficCell'
2931import Events from './components/Events'
@@ -34,6 +36,7 @@ import menu from './menu'
3436const Page : React . FC = ( ) => {
3537 const history = useHistory ( )
3638 const profile = useProfile ( )
39+ const profileDispatch = useProfileDispatch ( )
3740 const { data : systemProxy } = useSWR < Capability > (
3841 profile ?. platform === 'macos' ? '/features/system_proxy' : null ,
3942 fetcher ,
@@ -46,6 +49,7 @@ const Page: React.FC = () => {
4649 const platform = usePlatform ( )
4750 const platformVersion = usePlatformVersion ( )
4851 const platformBuild = usePlatformBuild ( )
52+ const isFirstShown = useRef ( true )
4953
5054 const toggleSystemProxy = useCallback ( ( ) => {
5155 fetcher ( {
@@ -95,6 +99,36 @@ const Page: React.FC = () => {
9599 }
96100 }
97101
102+ useEffect ( ( ) => {
103+ if ( ! profile ?. platform || ! isFirstShown . current ) {
104+ return
105+ }
106+
107+ httpClient
108+ . request ( {
109+ url : '/environment' ,
110+ method : 'GET' ,
111+ } )
112+ . then ( ( res ) => {
113+ const currentPlatformVersion = res . headers [ 'x-surge-version' ]
114+
115+ if ( currentPlatformVersion !== platformVersion ) {
116+ profileDispatch ( {
117+ type : 'updatePlatformVersion' ,
118+ payload : {
119+ platformVersion : currentPlatformVersion ,
120+ } ,
121+ } )
122+ }
123+
124+ isFirstShown . current = false
125+ } )
126+ . catch ( ( err ) => {
127+ console . error ( err )
128+ toast . error ( t ( 'common.surge_too_old' ) )
129+ } )
130+ } , [ platformVersion , profile ?. platform , profileDispatch , t ] )
131+
98132 return (
99133 < React . Fragment >
100134 < div
0 commit comments