Skip to content

Commit 9cd9297

Browse files
committed
feat: Create AuthModalStore without persist.
1 parent 005b348 commit 9cd9297

5 files changed

Lines changed: 34 additions & 20 deletions

File tree

src/App.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export const App = () => {
2222
const [showOnboarding, setShowOnboarding] = useState(true)
2323
const { onboardingCompleted, maxVisibleCards, isDNDModeActive, DNDDuration, setDNDDuration } =
2424
useUserPreferences()
25-
const { isAuthShowing, setIsAuthShowing, isConnected } = useAuth()
25+
const { isAuthModalOpen, closeAuthModal } = useAuth()
2626

2727
useLayoutEffect(() => {
2828
document.documentElement.style.setProperty('--max-visible-cards', maxVisibleCards.toString())
@@ -64,7 +64,7 @@ export const App = () => {
6464
<OnboardingModal showOnboarding={showOnboarding} setShowOnboarding={setShowOnboarding} />
6565
)}
6666

67-
{!isConnected() && <AuthModal showAuth={isAuthShowing} setShowAuth={setIsAuthShowing} />}
67+
{<AuthModal showAuth={isAuthModalOpen} closeModal={closeAuthModal} />}
6868

6969
<div className="layoutLayers hideScrollBar">
7070
{isDNDModeActive() && <DNDLayout />}

src/components/Layout/Header.tsx

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { useBookmarks } from 'src/stores/bookmarks'
1616
import { useUserPreferences } from 'src/stores/preferences'
1717

1818
export const Header = () => {
19-
const { setIsAuthShowing, user, isConnected } = useAuth()
19+
const { openAuthModal, user, isConnected } = useAuth()
2020

2121
const [themeIcon, setThemeIcon] = useState(<BsMoonFill />)
2222
const { theme, setTheme, setDNDDuration, isDNDModeActive } = useUserPreferences()
@@ -105,12 +105,7 @@ export const Header = () => {
105105
<img className="profileImage" src={user?.imageURL} />
106106
</Link>
107107
) : (
108-
<button
109-
aria-label="open login"
110-
className="extraBtn"
111-
onClick={() => {
112-
setIsAuthShowing(true)
113-
}}>
108+
<button aria-label="open login" className="extraBtn" onClick={openAuthModal}>
114109
<FaUserLarge />
115110
</button>
116111
)}

src/features/auth/components/AuthModal.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ import { auth, githubProvider, googleProvider } from '../api/Config'
77

88
type AuthModalProps = {
99
showAuth: boolean
10-
setShowAuth: (show: boolean) => void
10+
closeModal: () => void
1111
}
1212

13-
export const AuthModal = ({ showAuth, setShowAuth }: AuthModalProps) => {
14-
const { initState } = useAuth()
13+
export const AuthModal = ({ showAuth, closeModal }: AuthModalProps) => {
14+
const { closeAuthModal, initState } = useAuth()
1515

1616
const signIn = (provider: AuthProvider, providerName: string) => {
1717
signInWithPopup(auth, provider)
@@ -22,6 +22,7 @@ export const AuthModal = ({ showAuth, setShowAuth }: AuthModalProps) => {
2222
const name = result.user.displayName
2323
const imageURL = result.user.photoURL
2424
if (accessToken && name && email && imageURL) {
25+
closeAuthModal()
2526
initState({
2627
accessToken: accessToken,
2728
user: {
@@ -45,7 +46,7 @@ export const AuthModal = ({ showAuth, setShowAuth }: AuthModalProps) => {
4546
shouldCloseOnEsc={true}
4647
shouldCloseOnOverlayClick={true}
4748
shouldFocusAfterRender={false}
48-
onRequestClose={() => setShowAuth(false)}
49+
onRequestClose={closeModal}
4950
contentLabel="Auth"
5051
className="Modal scrollable"
5152
style={{

src/features/auth/hooks/useAuth.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
1-
import { AuthStore } from 'src/features/auth'
1+
import { AuthModalStore, AuthStore } from 'src/features/auth'
22
import { useBookmarks } from 'src/stores/bookmarks'
33

44
export const useAuth = () => {
5+
const { isAuthModalOpen, openAuthModal, closeAuthModal } = AuthModalStore()
56
const authStore = AuthStore()
67
const bookmarksStore = useBookmarks()
7-
const { isAuthShowing, accessToken, user, setIsAuthShowing, initState } = authStore
8+
const { accessToken, user, initState } = authStore
89
const isConnected = () => user != null
910
const logout = () => {
1011
bookmarksStore.clear()
1112
authStore.clear()
1213
}
1314

14-
return { setIsAuthShowing, initState, isConnected, logout, isAuthShowing, accessToken, user }
15+
return {
16+
openAuthModal,
17+
closeAuthModal,
18+
initState,
19+
isConnected,
20+
logout,
21+
isAuthModalOpen,
22+
accessToken,
23+
user,
24+
}
1525
}

src/features/auth/stores/authStore.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,33 @@ import { User } from 'src/features/auth/types'
22
import { create } from 'zustand'
33
import { persist } from 'zustand/middleware'
44

5+
interface AuthModalState {
6+
isAuthModalOpen: boolean
7+
openAuthModal: () => void
8+
closeAuthModal: () => void
9+
}
10+
511
type AuthState = {
6-
isAuthShowing: boolean
712
accessToken: string | null
813
user: User | null
914
}
1015

1116
type AuthActions = {
12-
setIsAuthShowing: (showing: boolean) => void
1317
initState: (state: AuthState) => void
1418
clear: () => void
1519
}
1620

21+
export const AuthModalStore = create<AuthModalState>((set) => ({
22+
isAuthModalOpen: false,
23+
openAuthModal: () => set({ isAuthModalOpen: true }),
24+
closeAuthModal: () => set({ isAuthModalOpen: false }),
25+
}))
26+
1727
export const AuthStore = create(
1828
persist<AuthState & AuthActions>(
1929
(set) => ({
20-
isAuthShowing: true,
2130
accessToken: null,
2231
user: null,
23-
setIsAuthShowing: (showing: boolean) => set({ isAuthShowing: showing }),
2432
initState: (newState: AuthState) =>
2533
set({
2634
accessToken: newState.accessToken,

0 commit comments

Comments
 (0)