Skip to content

Commit 2edb0d5

Browse files
committed
4149: Changed naming BackToContentButton to BackToRegionButton and replaced useRef with useState
1 parent 7dfe7f9 commit 2edb0d5

3 files changed

Lines changed: 15 additions & 17 deletions

File tree

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import Button from '@mui/material/Button'
22
import { styled } from '@mui/material/styles'
3-
import React, { ReactElement, useRef } from 'react'
3+
import React, { ReactElement, useState } from 'react'
44
import { useTranslation } from 'react-i18next'
55
import { useNavigate } from 'react-router'
66

@@ -12,29 +12,27 @@ const StyledButton = styled(Button)({
1212
alignSelf: 'flex-start',
1313
})
1414

15-
const historyIndex = (): number => window.history.state?.idx ?? 0
16-
17-
const BackToContentButton = (): ReactElement | null => {
15+
const BackToRegionButton = (): ReactElement | null => {
1816
const navigate = useNavigate()
1917
const { mobile } = useDimensions()
2018
const { t } = useTranslation('layout')
19+
const currentHistoryIndex = window.history.state?.idx ?? 0
2120

22-
// Index of the content page we arrived from so that we can navigate back even if the language changed.
23-
const entryIndex = useRef(historyIndex()).current
21+
// Initial history index to account for language changes or other user interactions on this page.
22+
const [initialHistoryIndex] = useState(currentHistoryIndex)
2423

25-
if (!mobile || entryIndex === 0) {
24+
if (!mobile || initialHistoryIndex === 0) {
2625
return null
2726
}
2827

2928
return (
3029
<StyledButton
31-
onClick={() => navigate(entryIndex - historyIndex() - 1)}
30+
onClick={() => navigate(initialHistoryIndex - currentHistoryIndex - 1)}
3231
startIcon={<DirectionDependentBackIcon />}
33-
color='inherit'
34-
aria-label={t('backToContent')}>
32+
color='inherit'>
3533
{t('backToContent')}
3634
</StyledButton>
3735
)
3836
}
3937

40-
export default BackToContentButton
38+
export default BackToRegionButton

web/src/components/__tests__/BackToContentButton.spec.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import React from 'react'
44
import { mockDimensions } from '../../__mocks__/useDimensions'
55
import useDimensions from '../../hooks/useDimensions'
66
import { renderWithRouterAndTheme } from '../../testing/render'
7-
import BackToContentButton from '../BackToContentButton'
7+
import BackToRegionButton from '../BackToRegionButton'
88

99
const mockNavigate = jest.fn()
1010
jest.mock('react-i18next')
@@ -14,7 +14,7 @@ jest.mock('react-router', () => ({
1414
useNavigate: () => mockNavigate,
1515
}))
1616

17-
describe('BackToContentButton', () => {
17+
describe('BackToRegionButton', () => {
1818
const { mocked } = jest
1919

2020
const setHistoryIndex = (idx: number) => window.history.replaceState({ idx }, '')
@@ -26,7 +26,7 @@ describe('BackToContentButton', () => {
2626
})
2727

2828
it('should render on mobile and navigate back on click', () => {
29-
const { getByText } = renderWithRouterAndTheme(<BackToContentButton />)
29+
const { getByText } = renderWithRouterAndTheme(<BackToRegionButton />)
3030

3131
fireEvent.click(getByText('layout:backToContent'))
3232

@@ -35,7 +35,7 @@ describe('BackToContentButton', () => {
3535

3636
it('should render nothing when there is no history to go back to', () => {
3737
setHistoryIndex(0)
38-
const { queryByText } = renderWithRouterAndTheme(<BackToContentButton />)
38+
const { queryByText } = renderWithRouterAndTheme(<BackToRegionButton />)
3939

4040
expect(queryByText('layout:backToContent')).toBeFalsy()
4141
})

web/src/routes/RegionsPage.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { useTranslation } from 'react-i18next'
33

44
import { createRegionsEndpoint } from 'shared/api'
55

6-
import BackToContentButton from '../components/BackToContentButton'
6+
import BackToRegionButton from '../components/BackToRegionButton'
77
import FailureSwitcherWithHelmet from '../components/FailureSwitcherWithHelmet'
88
import Footer from '../components/Footer'
99
import GeneralHeader from '../components/GeneralHeader'
@@ -45,7 +45,7 @@ const RegionsPage = ({ languageCode }: RegionsPageProps): ReactElement => {
4545
</>
4646
}>
4747
<Helmet pageTitle={pageTitle} metaDescription={metaDescription} rootPage />
48-
<BackToContentButton />
48+
<BackToRegionButton />
4949
<RegionSelector regions={regions ?? []} language={languageCode} stickyTop={stickyTop} loading={isPending} />
5050
</Layout>
5151
)

0 commit comments

Comments
 (0)