Skip to content

Commit 005b348

Browse files
committed
feat: Move auth modal state handle to AuthStore.
1 parent c7b0bae commit 005b348

5 files changed

Lines changed: 18 additions & 16 deletions

File tree

src/App.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { useEffect, useLayoutEffect, useState } from 'react'
22
import { DNDLayout } from 'src/components/Layout'
3+
import { AuthModal, useAuth } from 'src/features/auth'
34
import { setupAnalytics, setupIdentification, trackPageView } from 'src/lib/analytics'
45
import { useUserPreferences } from 'src/stores/preferences'
56
import { AppContentLayout } from './components/Layout'
@@ -21,6 +22,7 @@ export const App = () => {
2122
const [showOnboarding, setShowOnboarding] = useState(true)
2223
const { onboardingCompleted, maxVisibleCards, isDNDModeActive, DNDDuration, setDNDDuration } =
2324
useUserPreferences()
25+
const { isAuthShowing, setIsAuthShowing, isConnected } = useAuth()
2426

2527
useLayoutEffect(() => {
2628
document.documentElement.style.setProperty('--max-visible-cards', maxVisibleCards.toString())
@@ -62,6 +64,8 @@ export const App = () => {
6264
<OnboardingModal showOnboarding={showOnboarding} setShowOnboarding={setShowOnboarding} />
6365
)}
6466

67+
{!isConnected() && <AuthModal showAuth={isAuthShowing} setShowAuth={setIsAuthShowing} />}
68+
6569
<div className="layoutLayers hideScrollBar">
6670
{isDNDModeActive() && <DNDLayout />}
6771
<AppContentLayout />

src/assets/App.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ a {
183183
.profileImage {
184184
height: 40px;
185185
width: 40px;
186+
margin-left: 8px;
186187
border-radius: 20px;
187188
}
188189

src/components/Layout/Header.tsx

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,14 @@ import { Link, useLocation, useNavigate } from 'react-router-dom'
99
import { ReactComponent as HackertabLogo } from 'src/assets/logo.svg'
1010
import { SearchBar } from 'src/components/Elements/SearchBar'
1111
import { UserTags } from 'src/components/Elements/UserTags'
12-
import { AuthModal, useAuth } from 'src/features/auth'
12+
import { useAuth } from 'src/features/auth'
1313
import { Changelog } from 'src/features/changelog'
1414
import { identifyUserTheme, trackDNDDisable, trackThemeSelect } from 'src/lib/analytics'
1515
import { useBookmarks } from 'src/stores/bookmarks'
1616
import { useUserPreferences } from 'src/stores/preferences'
1717

1818
export const Header = () => {
19-
const { user } = useAuth()
20-
const [showAuth, setshowAuth] = useState(false)
21-
useEffect(() => {
22-
if (user != null) {
23-
setshowAuth(false)
24-
}
25-
})
19+
const { setIsAuthShowing, user, isConnected } = useAuth()
2620

2721
const [themeIcon, setThemeIcon] = useState(<BsMoonFill />)
2822
const { theme, setTheme, setDNDDuration, isDNDModeActive } = useUserPreferences()
@@ -73,8 +67,6 @@ export const Header = () => {
7367

7468
return (
7569
<>
76-
{<AuthModal showAuth={showAuth} setShowAuth={setshowAuth} />}
77-
7870
<header className="AppHeader">
7971
<span className="AppName">
8072
<i className="logo">
@@ -108,16 +100,16 @@ export const Header = () => {
108100
<BookmarksBadgeCount />
109101
</>
110102
</Link>
111-
{user != null ? (
112-
<Link to="/settings/profile" className="extraBtn" aria-label="Open profile">
113-
<img className="profileImage" src={user.imageURL} />
103+
{isConnected() ? (
104+
<Link to="/settings/profile" aria-label="Open profile">
105+
<img className="profileImage" src={user?.imageURL} />
114106
</Link>
115107
) : (
116108
<button
117109
aria-label="open login"
118110
className="extraBtn"
119111
onClick={() => {
120-
setshowAuth(true)
112+
setIsAuthShowing(true)
121113
}}>
122114
<FaUserLarge />
123115
</button>

src/features/auth/hooks/useAuth.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ import { useBookmarks } from 'src/stores/bookmarks'
44
export const useAuth = () => {
55
const authStore = AuthStore()
66
const bookmarksStore = useBookmarks()
7-
const { initState, accessToken, user } = authStore
7+
const { isAuthShowing, accessToken, user, setIsAuthShowing, initState } = authStore
8+
const isConnected = () => user != null
89
const logout = () => {
910
bookmarksStore.clear()
1011
authStore.clear()
1112
}
1213

13-
return { initState, logout, accessToken, user }
14+
return { setIsAuthShowing, initState, isConnected, logout, isAuthShowing, accessToken, user }
1415
}

src/features/auth/stores/authStore.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,24 @@ import { create } from 'zustand'
33
import { persist } from 'zustand/middleware'
44

55
type AuthState = {
6+
isAuthShowing: boolean
67
accessToken: string | null
78
user: User | null
89
}
910

1011
type AuthActions = {
12+
setIsAuthShowing: (showing: boolean) => void
1113
initState: (state: AuthState) => void
1214
clear: () => void
1315
}
1416

1517
export const AuthStore = create(
1618
persist<AuthState & AuthActions>(
1719
(set) => ({
20+
isAuthShowing: true,
1821
accessToken: null,
1922
user: null,
23+
setIsAuthShowing: (showing: boolean) => set({ isAuthShowing: showing }),
2024
initState: (newState: AuthState) =>
2125
set({
2226
accessToken: newState.accessToken,

0 commit comments

Comments
 (0)