Skip to content

Commit 2f0b887

Browse files
committed
header state independent
1 parent b597da1 commit 2f0b887

File tree

2 files changed

+48
-25
lines changed

2 files changed

+48
-25
lines changed

src/app/components/Header.tsx

Lines changed: 46 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import {
1515
MenuItem,
1616
Select,
1717
useTheme,
18-
Link,
1918
Alert,
2019
AlertTitle,
2120
} from '@mui/material';
@@ -43,11 +42,8 @@ import { defaultRemoteConfigValues } from '../interface/RemoteConfig';
4342
import { animatedButtonStyling } from './Header.style';
4443
import ThemeToggle from './ThemeToggle';
4544
import { useTranslations, useLocale } from 'next-intl';
46-
import { useSelector } from 'react-redux';
47-
import {
48-
selectIsAuthenticated,
49-
selectUserEmail,
50-
} from '../store/profile-selectors';
45+
import Link from 'next/link';
46+
import { app } from '../../firebase';
5147

5248
// Lazy load components not needed for initial render
5349
const LogoutConfirmModal = dynamic(
@@ -78,6 +74,9 @@ function useClientSearchParams(): URLSearchParams | null {
7874
}
7975

8076
export default function DrawerAppBar(): React.ReactElement {
77+
const [currentUser, setCurrentUser] = React.useState<
78+
{ email: string; isAuthenticated: boolean } | undefined
79+
>(undefined);
8180
const clientSearchParams = useClientSearchParams();
8281
const hasTransitFeedsRedirectParam =
8382
clientSearchParams?.get('utm_source') === 'transitfeeds';
@@ -96,6 +95,21 @@ export default function DrawerAppBar(): React.ReactElement {
9695
const { config } = useRemoteConfig();
9796
const t = useTranslations('common');
9897

98+
React.useEffect(() => {
99+
const auth = app.auth();
100+
const unsubscribe = auth.onAuthStateChanged(async (user) => {
101+
if (user) {
102+
setCurrentUser({
103+
email: user.email || '',
104+
isAuthenticated: !user.isAnonymous,
105+
});
106+
} else {
107+
setCurrentUser(undefined);
108+
}
109+
});
110+
return () => unsubscribe();
111+
}, []);
112+
99113
React.useEffect(() => {
100114
if (hasTransitFeedsRedirectParam) {
101115
setHasTransitFeedsRedirect(true);
@@ -111,8 +125,10 @@ export default function DrawerAppBar(): React.ReactElement {
111125
}, [config]);
112126

113127
const router = useRouter();
114-
const isAuthenticated = useSelector(selectIsAuthenticated);
115-
const userEmail = useSelector(selectUserEmail);
128+
129+
const isAuthenticated =
130+
currentUser != null && currentUser.isAuthenticated === true;
131+
const userEmail = currentUser?.email;
116132

117133
const handleDrawerToggle = (): void => {
118134
setMobileOpen((prevState) => !prevState);
@@ -184,7 +200,7 @@ export default function DrawerAppBar(): React.ReactElement {
184200
>
185201
<MenuIcon />
186202
</IconButton>
187-
<a
203+
<Link
188204
href={'/'}
189205
style={{
190206
textDecoration: 'none',
@@ -213,28 +229,34 @@ export default function DrawerAppBar(): React.ReactElement {
213229
>
214230
Mobility Database
215231
</Typography>
216-
</a>
232+
</Link>
217233
</Box>
218234

219235
<Box sx={{ display: { xs: 'none', md: 'block' } }}>
220236
{navigationItems.map((item) => (
221-
<Button
222-
sx={(theme) => ({
223-
...animatedButtonStyling(theme),
224-
color: theme.palette.text.primary,
225-
})}
237+
<Link
238+
data-cy={
239+
'header-' + item.title.toLowerCase().replace(/\s+/g, '-')
240+
}
226241
href={item.external === true ? item.target : '/' + item.target}
227242
key={item.title}
228243
target={item.external === true ? '_blank' : '_self'}
229244
rel={item.external === true ? 'noopener noreferrer' : ''}
230-
variant={'text'}
231-
endIcon={item.external === true ? <OpenInNew /> : null}
232-
className={
233-
activeTab.includes('/' + item.target) ? 'active' : ''
234-
}
235245
>
236-
{item.title}
237-
</Button>
246+
<Button
247+
sx={(theme) => ({
248+
...animatedButtonStyling(theme),
249+
color: theme.palette.text.primary,
250+
})}
251+
variant={'text'}
252+
endIcon={item.external === true ? <OpenInNew /> : null}
253+
className={
254+
activeTab.includes('/' + item.target) ? 'active' : ''
255+
}
256+
>
257+
{item.title}
258+
</Button>
259+
</Link>
238260
))}
239261
{config.gbfsValidator && (
240262
<>
@@ -383,6 +405,7 @@ export default function DrawerAppBar(): React.ReactElement {
383405
onClose={handleMenuClose}
384406
>
385407
<MenuItem
408+
data-cy='accountDetailsHeader'
386409
onClick={() => {
387410
handleMenuItemClick(navigationAccountItem);
388411
}}
@@ -495,6 +518,7 @@ export default function DrawerAppBar(): React.ReactElement {
495518
}}
496519
>
497520
<DrawerContent
521+
isAuthenticated={isAuthenticated}
498522
onLogoutClick={handleLogoutClick}
499523
navigationItems={navigationItems}
500524
metricsOptionsEnabled={metricsOptionsEnabled}

src/app/components/HeaderMobileDrawer.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import {
1313
useTheme,
1414
Link,
1515
} from '@mui/material';
16-
import { useSelector } from 'react-redux';
1716
import { useRouter } from 'next/navigation';
1817
import {
1918
ACCOUNT_TARGET,
@@ -22,7 +21,6 @@ import {
2221
SIGN_IN_TARGET,
2322
} from '../constants/Navigation';
2423
import type NavigationItem from '../interface/Navigation';
25-
import { selectIsAuthenticated } from '../store/profile-selectors';
2624
import { fontFamily } from '../Theme';
2725
import { mobileNavElementStyle } from './Header.style';
2826
import ExpandMoreIcon from '@mui/icons-material/ExpandMore';
@@ -32,17 +30,18 @@ import { useTranslations } from 'next-intl';
3230
const websiteTile = 'Mobility Database';
3331

3432
interface DrawerContentProps {
33+
isAuthenticated: boolean;
3534
onLogoutClick: React.MouseEventHandler;
3635
navigationItems: NavigationItem[];
3736
metricsOptionsEnabled: boolean;
3837
}
3938

4039
export default function DrawerContent({
40+
isAuthenticated,
4141
onLogoutClick,
4242
navigationItems,
4343
metricsOptionsEnabled,
4444
}: DrawerContentProps): React.ReactElement {
45-
const isAuthenticated = useSelector(selectIsAuthenticated);
4645
const router = useRouter();
4746
const { config } = useRemoteConfig();
4847
const t = useTranslations('common');

0 commit comments

Comments
 (0)