Skip to content

Commit 125b74d

Browse files
committed
feat: allow force refresh the page in index page
1 parent b172e16 commit 125b74d

File tree

2 files changed

+14
-38
lines changed

2 files changed

+14
-38
lines changed

src/pages/Index/index.tsx

Lines changed: 4 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
/** @jsx jsx */
22
import { jsx } from '@emotion/core'
3-
import React, { useCallback, useEffect, useRef } from 'react'
3+
import React, { useCallback } 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'
87
import tw from 'twin.macro'
98
import { delay } from 'bluebird'
109
import { useHistory } from 'react-router-dom'
@@ -20,12 +19,11 @@ import {
2019
usePlatformBuild,
2120
usePlatformVersion,
2221
useProfile,
23-
useProfileDispatch,
2422
} from '../../models/profile'
2523
import { Capability } from '../../types'
26-
import { isRunInSurge } from '../../utils'
24+
import { forceRefresh, isRunInSurge } from '../../utils'
2725
import { ExistingProfiles, LastUsedProfile } from '../../utils/constant'
28-
import fetcher, { httpClient } from '../../utils/fetcher'
26+
import fetcher from '../../utils/fetcher'
2927
import HostInfo from './components/HostInfo'
3028
import TrafficCell from './components/TrafficCell'
3129
import Events from './components/Events'
@@ -36,7 +34,6 @@ import menu from './menu'
3634
const Page: React.FC = () => {
3735
const history = useHistory()
3836
const profile = useProfile()
39-
const profileDispatch = useProfileDispatch()
4037
const { data: systemProxy } = useSWR<Capability>(
4138
profile?.platform === 'macos' ? '/features/system_proxy' : null,
4239
fetcher,
@@ -49,7 +46,6 @@ const Page: React.FC = () => {
4946
const platform = usePlatform()
5047
const platformVersion = usePlatformVersion()
5148
const platformBuild = usePlatformBuild()
52-
const isFirstShown = useRef(true)
5349

5450
const toggleSystemProxy = useCallback(() => {
5551
fetcher({
@@ -99,36 +95,6 @@ const Page: React.FC = () => {
9995
}
10096
}
10197

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-
13298
return (
13399
<React.Fragment>
134100
<div
@@ -143,7 +109,7 @@ const Page: React.FC = () => {
143109
>
144110
{profile && (
145111
<div tw="w-full flex justify-between items-center">
146-
<div tw="w-2/3" onDoubleClick={() => window.location.reload()}>
112+
<div tw="w-2/3" onDoubleClick={forceRefresh}>
147113
<HostInfo />
148114
</div>
149115

src/utils/index.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { unregisterAsync } from '../serviceWorkerRegistration'
2+
13
export const isFalsy = (obj: string | boolean | 1 | 0) =>
24
obj === 0 || obj === false || (typeof obj === 'string' && obj.length === 0)
35

@@ -6,3 +8,11 @@ export const isTruthy = (obj: string | boolean | 1 | 0) =>
68

79
export const isRunInSurge = (): boolean =>
810
'REACT_APP_RUN_IN_SURGE' in process.env
11+
12+
export const forceRefresh = async (): Promise<void> => {
13+
if (process.env.REACT_APP_USE_SW) {
14+
await unregisterAsync()
15+
}
16+
17+
window.location.reload()
18+
}

0 commit comments

Comments
 (0)