Skip to content

Commit 032fa5d

Browse files
committed
feat: improve history and profile management
1 parent 72c9ee2 commit 032fa5d

File tree

20 files changed

+595
-585
lines changed

20 files changed

+595
-585
lines changed

.eslintrc.cjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ module.exports = {
3636
'plugin:prettier/recommended',
3737
],
3838
rules: {
39+
'no-debugger': 'warn',
3940
'react/no-unknown-property': ['error', { ignore: ['css'] }],
4041
'react/prop-types': 'off',
4142
'@typescript-eslint/ban-ts-comment': 'off',

scripts/build.mjs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ await (async () => {
2525

2626
case 'release-ci':
2727
process.env.NODE_ENV = 'production'
28-
process.env.REACT_APP_SHOW_AD = 'true'
2928
process.env.REACT_APP_HASH_ROUTER = 'true'
3029
process.env.REACT_APP_USE_SW = 'true'
3130
process.env.PUBLIC_URL = getUrlPathPrefix()

src/App.tsx

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
} from 'react-router-dom'
1717
import { toast, ToastContainer as OriginalToastContainer } from 'react-toastify'
1818
import styled from '@emotion/styled'
19+
import { init } from 'i18next'
1920
import { find } from 'lodash-es'
2021
import store from 'store2'
2122
import { SWRConfig } from 'swr'
@@ -27,7 +28,10 @@ import NewVersionAlert from '@/components/NewVersionAlert'
2728
import PageLayout from '@/components/PageLayout'
2829
import useTrafficUpdater from '@/hooks/useTrafficUpdater'
2930
import {
31+
HistoryActions,
3032
ProfileActions,
33+
useHistory,
34+
useHistoryDispatch,
3135
usePlatformVersion,
3236
useProfile,
3337
useProfileDispatch,
@@ -36,11 +40,7 @@ import HomePage from '@/pages/Home'
3640
import { LandingPage } from '@/pages/Landing'
3741
import { Profile } from '@/types'
3842
import { isRunInSurge } from '@/utils'
39-
import {
40-
ExistingProfiles,
41-
LastUsedLanguage,
42-
LastUsedProfile,
43-
} from '@/utils/constant'
43+
import { LastUsedLanguage, LastUsedProfile } from '@/utils/constant'
4444
import { httpClient } from '@/utils/fetcher'
4545

4646
import 'react-toastify/dist/ReactToastify.css'
@@ -118,47 +118,39 @@ const App: React.FC = () => {
118118
const [isNetworkModalOpen, setIsNetworkModalOpen] = useState(false)
119119
const location = useLocation()
120120
const navigate = useNavigate()
121+
122+
const historyDispatch = useHistoryDispatch()
121123
const profileDispatch = useProfileDispatch()
124+
const platformVersion = usePlatformVersion()
122125
const profile = useProfile()
126+
const history = useHistory()
127+
123128
const [hasInit, setHasInit] = useState(false)
124129
const isCurrentVersionFetched = useRef(true)
125-
const platformVersion = usePlatformVersion()
126130

127131
useTrafficUpdater()
128132

129133
const onCloseApplication = useCallback(() => {
130134
if (isRunInSurge()) {
131-
store.remove(LastUsedProfile)
132-
store.remove(ExistingProfiles)
135+
historyDispatch({
136+
type: HistoryActions.DELETE_ALL_HISTORY,
137+
})
133138
}
134139

135140
window.location.replace('/')
136-
}, [])
137-
138-
useEffect(
139-
() => {
140-
const existingProfiles = store.get(ExistingProfiles)
141-
const lastId = store.get(LastUsedProfile)
142-
const result = find<Profile>(existingProfiles, { id: lastId })
143-
144-
if (result) {
145-
profileDispatch({
146-
type: ProfileActions.Update,
147-
payload: result,
148-
})
149-
}
150-
151-
setHasInit(true)
152-
},
153-
// eslint-disable-next-line
154-
[],
155-
)
141+
}, [historyDispatch])
142+
143+
useEffect(() => {
144+
historyDispatch({
145+
type: HistoryActions.LOAD_HISTORY,
146+
})
147+
}, [historyDispatch])
156148

157149
useEffect(() => {
158-
if (hasInit && !profile && location.pathname !== '/') {
150+
if (history && !profile && location.pathname !== '/') {
159151
navigate('/', { replace: true })
160152
}
161-
}, [hasInit, location, navigate, profile])
153+
}, [hasInit, history, location, navigate, profile])
162154

163155
useEffect(() => {
164156
// Log page view

src/AppContainer.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
import { ThemeProvider } from '@emotion/react'
99
import { light } from '@sumup/design-tokens'
1010

11-
import { ProfileProvider, TrafficProvider } from './models'
11+
import { HistoryProvider, ProfileProvider, TrafficProvider } from './models'
1212

1313
const ReactRouter: React.FC<BrowserRouterProps | HashRouterProps> = (args) => {
1414
return process.env.REACT_APP_HASH_ROUTER ? (
@@ -26,7 +26,9 @@ const AppContainer: React.FC<{ children: ReactNode }> = ({ children }) => {
2626
<ReactRouter>
2727
<ProfileProvider>
2828
<TrafficProvider>
29-
<ThemeProvider theme={light}>{children}</ThemeProvider>
29+
<HistoryProvider>
30+
<ThemeProvider theme={light}>{children}</ThemeProvider>
31+
</HistoryProvider>
3032
</TrafficProvider>
3133
</ProfileProvider>
3234
</ReactRouter>

src/components/VersionSupport.tsx

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,29 @@
11
import React from 'react'
22

3-
import { useVersionSupport } from '../hooks'
3+
import { useVersionSupport } from '@/hooks'
4+
import { isRunInSurge as _isRunInSurge } from '@/utils'
45

56
interface VersionSupportProps {
6-
macos?: string
7-
ios?: string
7+
macos?: string | boolean
8+
ios?: string | boolean
9+
tvos?: string | boolean
10+
isRunInSurge?: boolean
811
children: React.ReactNode
912
}
1013

1114
const VersionSupport: React.FC<VersionSupportProps> = ({
1215
macos,
1316
ios,
17+
tvos,
18+
isRunInSurge,
1419
children,
1520
}) => {
16-
const isSupported = useVersionSupport({ macos, ios })
21+
const isSupported = useVersionSupport({ macos, ios, tvos })
22+
const surgeCheck =
23+
typeof isRunInSurge === 'boolean' ? isRunInSurge === _isRunInSurge() : true
1724

18-
if (isSupported) {
19-
return <React.Fragment>{children}</React.Fragment>
25+
if (isSupported && surgeCheck) {
26+
return <>{children}</>
2027
}
2128

2229
return null

src/components/ui/typography.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export function TypographyH4({
6060
}: { children: React.ReactNode } & React.HTMLAttributes<HTMLHeadingElement>) {
6161
return (
6262
<h4
63-
className={cn('scroll-m-20 text-xl tracking-tight', className)}
63+
className={cn('scroll-m-20 text-lg font-bold tracking-tight', className)}
6464
{...props}
6565
>
6666
{children}

src/hooks/useVersionSupport.ts

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@ import gte from 'semver/functions/gte'
33
import { usePlatform, usePlatformVersion } from '@/models'
44

55
interface VersionSupportProps {
6-
macos?: string
7-
ios?: string
6+
macos?: string | boolean
7+
ios?: string | boolean
8+
tvos?: string | boolean
89
}
910

1011
export const useVersionSupport = ({
1112
macos,
1213
ios,
14+
tvos,
1315
}: VersionSupportProps): boolean => {
1416
const platform = usePlatform()
1517
const platformVersion = usePlatformVersion()
@@ -18,13 +20,28 @@ export const useVersionSupport = ({
1820
return false
1921
}
2022

21-
if (macos && platform === 'macos' && gte(platformVersion, macos)) {
23+
if (
24+
macos &&
25+
platform === 'macos' &&
26+
gte(platformVersion, parseVersion(macos))
27+
) {
2228
return true
2329
}
2430

25-
if (ios && platform === 'ios' && gte(platformVersion, ios)) {
31+
if (ios && platform === 'ios' && gte(platformVersion, parseVersion(ios))) {
32+
return true
33+
}
34+
35+
if (tvos && platform === 'tvos' && gte(platformVersion, parseVersion(tvos))) {
2636
return true
2737
}
2838

2939
return false
3040
}
41+
42+
function parseVersion(version: string | boolean): string {
43+
if (typeof version === 'string') {
44+
return version
45+
}
46+
return '0.0.0'
47+
}

0 commit comments

Comments
 (0)