Skip to content

Commit 872151a

Browse files
committed
fix: wrong viewport position when history goes forward or backward
1 parent 101289c commit 872151a

File tree

5 files changed

+69
-10
lines changed

5 files changed

+69
-10
lines changed

src/components/BackButton/index.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { jsx } from '@emotion/core'
33
import { IconButton } from '@sumup/circuit-ui'
44
import { ChevronLeft } from '@sumup/icons'
55
import React from 'react'
6-
import styled from '@emotion/styled/macro'
76
import css from '@emotion/css/macro'
87
import tw from 'twin.macro'
98
import { useHistory } from 'react-router-dom'

src/components/ScrollToTop/index.tsx

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,39 @@
11
import { useModal } from '@sumup/circuit-ui'
22
import React, { useEffect } from 'react'
3-
import { useLocation } from 'react-router-dom'
3+
import { useHistory, useLocation } from 'react-router-dom'
4+
5+
function useScrollMemory(): void {
6+
const history = useHistory<{ scroll: number } | undefined>()
7+
8+
useEffect(() => {
9+
const { push, replace } = history
10+
11+
// Override the history PUSH method to automatically set scroll state.
12+
history.push = (path: string) => {
13+
push(path, { scroll: window.scrollY })
14+
}
15+
// Override the history REPLACE method to automatically set scroll state.
16+
history.replace = (path: string) => {
17+
replace(path, { scroll: window.scrollY })
18+
}
19+
20+
// Listen for location changes and set the scroll position accordingly.
21+
// @ts-ignore
22+
const unregister = history.listen((location, action) => {
23+
window.scrollTo(0, action !== 'POP' ? 0 : location.state?.scroll ?? 0)
24+
})
25+
26+
// Unregister listener when component unmounts.
27+
return () => {
28+
unregister()
29+
}
30+
}, [history])
31+
}
432

533
const ScrollToTop: React.FC = () => {
634
const { pathname } = useLocation()
735
const { isModalOpen, removeModal } = useModal()
8-
9-
useEffect(() => {
10-
window.scrollTo(0, 0)
11-
}, [pathname])
36+
useScrollMemory()
1237

1338
useEffect(
1439
() => {

src/models/profile.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ export const usePlatformVersion = ():
7777

7878
return context.profile?.platformVersion
7979
}
80+
export const usePlatformBuild = (): Profile['platformBuild'] | undefined => {
81+
const context = React.useContext(ProfileContext)
82+
83+
return context.profile?.platformBuild
84+
}
8085

8186
export const useSurgeHost = (): string | null => {
8287
const context = React.useContext(ProfileContext)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/** @jsx jsx */
2+
import { jsx } from '@emotion/core'
3+
import React from 'react'
4+
import tw from 'twin.macro'
5+
import css from '@emotion/css/macro'
6+
7+
import ProfileCell from '../../../components/ProfileCell'
8+
import { useProfile } from '../../../models/profile'
9+
10+
const HostInfo = (): JSX.Element => {
11+
const profile = useProfile()
12+
13+
return (
14+
<div tw="bg-gray-100 rounded-lg">
15+
{profile && <ProfileCell variant="left" profile={profile} />}
16+
</div>
17+
)
18+
}
19+
20+
export default HostInfo

src/pages/Index/index.tsx

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import { jsx } from '@emotion/core'
33
import React, { useCallback } from 'react'
44
import { Button, Heading, Toggle } from '@sumup/circuit-ui'
5-
import styled from '@emotion/styled/macro'
65
import css from '@emotion/css/macro'
76
import { useTranslation } from 'react-i18next'
87
import tw from 'twin.macro'
@@ -13,18 +12,19 @@ import store from 'store2'
1312

1413
import ChangeLanguage from '../../components/ChangeLanguage'
1514
import { DataGroup, DataRow, DataRowMain } from '../../components/Data'
16-
import ProfileCell from '../../components/ProfileCell'
1715
import Ad from '../../components/Ad'
1816
import VersionSupport from '../../components/VersionSupport'
1917
import {
2018
usePlatform,
19+
usePlatformBuild,
2120
usePlatformVersion,
2221
useProfile,
2322
} from '../../models/profile'
2423
import { Capability } from '../../types'
2524
import { isRunInSurge } from '../../utils'
2625
import { ExistingProfiles, LastUsedProfile } from '../../utils/constant'
2726
import fetcher from '../../utils/fetcher'
27+
import HostInfo from './components/HostInfo'
2828
import TrafficCell from './components/TrafficCell'
2929
import Events from './components/Events'
3030
import MenuTile, { MenuTileTitle } from './components/MenuTile'
@@ -45,6 +45,7 @@ const Page: React.FC = () => {
4545
const { t } = useTranslation()
4646
const platform = usePlatform()
4747
const platformVersion = usePlatformVersion()
48+
const platformBuild = usePlatformBuild()
4849

4950
const toggleSystemProxy = useCallback(() => {
5051
fetcher({
@@ -107,9 +108,9 @@ const Page: React.FC = () => {
107108
{profile && (
108109
<div tw="w-full flex justify-between items-center">
109110
<div
110-
css={[tw`w-2/3 bg-gray-100 rounded-lg`]}
111+
tw="w-2/3"
111112
onDoubleClick={() => window.location.reload(true)}>
112-
<ProfileCell variant="left" profile={profile} />
113+
<HostInfo />
113114
</div>
114115

115116
{isRunInSurge() ? (
@@ -209,6 +210,15 @@ const Page: React.FC = () => {
209210
<div tw="mt-4 px-4">
210211
<ChangeLanguage />
211212
</div>
213+
214+
<div tw="text-center mt-4 text-sm">
215+
{Boolean(platform && platformBuild && platformVersion) && (
216+
<code tw="px-4 py-2 rounded bg-gray-100 text-gray-500">
217+
v{process.env.REACT_APP_VERSION} - {platform} v{platformVersion}
218+
({platformBuild})
219+
</code>
220+
)}
221+
</div>
212222
</div>
213223
</div>
214224
</React.Fragment>

0 commit comments

Comments
 (0)