Skip to content

Commit 1a3e401

Browse files
committed
fix: version in local storage isn't up-to-date
1 parent d42f0a3 commit 1a3e401

File tree

6 files changed

+88
-4
lines changed

6 files changed

+88
-4
lines changed

src/i18n/en/translation.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
"success_interaction": "Success",
1212
"failed_interaction": "Failed",
1313
"see": "See",
14-
"save": "Save"
14+
"save": "Save",
15+
"surge_too_old": "Your Surge is too old, please upgrade in order to use this application!"
1516
},
1617
"form": {
1718
"err_invalid": "Invalid value",

src/i18n/zh/translation.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
"success_interaction": "操作成功",
1212
"failed_interaction": "操作失败",
1313
"see": "查看",
14-
"save": "保存"
14+
"save": "保存",
15+
"surge_too_old": "您的 Surge 版本过低,请考虑升级 Surge 以使用本程序!"
1516
},
1617
"form": {
1718
"err_invalid": "无效的输入内容",

src/models/profile.tsx

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React, { createContext, Dispatch, Reducer, useReducer } from 'react'
22

33
import { Profile } from '../types'
44
import { setServer } from '../utils/fetcher'
5+
import { updateStoredProfile } from '../utils/store'
56

67
interface IProfileContext {
78
profile?: Profile
@@ -15,6 +16,12 @@ type ReducerAction =
1516
| {
1617
type: 'clear'
1718
}
19+
| {
20+
type: 'updatePlatformVersion'
21+
payload: {
22+
platformVersion: Profile['platformVersion']
23+
}
24+
}
1825

1926
const profileReducer: Reducer<IProfileContext, ReducerAction> = (
2027
state,
@@ -32,6 +39,25 @@ const profileReducer: Reducer<IProfileContext, ReducerAction> = (
3239
return {
3340
profile: undefined,
3441
}
42+
case 'updatePlatformVersion': {
43+
if (!state.profile) {
44+
throw new Error(
45+
'updatePlatformVersion cannot be dispatched if the profile is absent.',
46+
)
47+
}
48+
49+
const profile = state.profile
50+
const updated = {
51+
...profile,
52+
platformVersion: action.payload.platformVersion,
53+
}
54+
55+
updateStoredProfile(updated.id, updated)
56+
57+
return {
58+
profile: updated,
59+
}
60+
}
3561
default:
3662
throw new Error()
3763
}

src/pages/Index/index.tsx

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
/** @jsx jsx */
22
import { jsx } from '@emotion/core'
3-
import React, { useCallback } from 'react'
3+
import React, { useCallback, useEffect, useRef } from 'react'
44
import { Button, Heading, Toggle } from '@sumup/circuit-ui'
55
import css from '@emotion/css/macro'
66
import { useTranslation } from 'react-i18next'
7+
import { toast } from 'react-toastify'
78
import tw from 'twin.macro'
89
import { delay } from 'bluebird'
910
import { useHistory } from 'react-router-dom'
@@ -19,11 +20,12 @@ import {
1920
usePlatformBuild,
2021
usePlatformVersion,
2122
useProfile,
23+
useProfileDispatch,
2224
} from '../../models/profile'
2325
import { Capability } from '../../types'
2426
import { isRunInSurge } from '../../utils'
2527
import { ExistingProfiles, LastUsedProfile } from '../../utils/constant'
26-
import fetcher from '../../utils/fetcher'
28+
import fetcher, { httpClient } from '../../utils/fetcher'
2729
import HostInfo from './components/HostInfo'
2830
import TrafficCell from './components/TrafficCell'
2931
import Events from './components/Events'
@@ -34,6 +36,7 @@ import menu from './menu'
3436
const 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

src/utils/fetcher.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,4 @@ const fetcher = <T>(requestConfig: AxiosRequestConfig): Promise<T> => {
6464
}
6565

6666
export default fetcher
67+
export { client as httpClient }

src/utils/store.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { findIndex } from 'lodash-es'
2+
import store from 'store2'
3+
4+
import { Profile } from '../types'
5+
import { ExistingProfiles } from './constant'
6+
7+
export const updateStoredProfile = (
8+
profileId: Profile['id'],
9+
newProfile: Profile,
10+
): void => {
11+
const storedExistingProfiles: Profile[] = store.get(ExistingProfiles)
12+
13+
if (storedExistingProfiles) {
14+
const result = findIndex(storedExistingProfiles, { id: profileId })
15+
16+
if (result !== -1) {
17+
storedExistingProfiles.splice(result, 1, newProfile)
18+
store.set(ExistingProfiles, storedExistingProfiles)
19+
}
20+
}
21+
}

0 commit comments

Comments
 (0)