Skip to content

Commit a05af28

Browse files
committed
refactor: update streak handling in AppLayout and useAuth for improved logic and state management
1 parent b44f786 commit a05af28

3 files changed

Lines changed: 39 additions & 8 deletions

File tree

src/components/Layout/AppLayout.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,17 @@ import { AuthProvider } from 'src/providers/AuthProvider'
1111
import { Header } from './Header'
1212

1313
export const AppLayout = () => {
14-
const { isAuthModalOpen, setStreak, isConnected } = useAuth()
14+
const { isAuthModalOpen, setStreak, shouldCountStreak } = useAuth()
1515
const postStreakMutation = usePostStreak()
1616

1717
useEffect(() => {
18-
if (isConnected) {
18+
if (shouldCountStreak()) {
1919
postStreakMutation.mutateAsync(undefined).then((data) => {
2020
setStreak(data.streak)
2121
identifyUserStreak(data.streak)
2222
})
2323
}
24-
}, [isConnected])
24+
}, [shouldCountStreak])
2525

2626
return (
2727
<AuthProvider>

src/features/auth/hooks/useAuth.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { signOut } from 'firebase/auth/web-extension'
2+
import { useCallback } from 'react'
23
import { AuthModalStore, AuthStore } from 'src/features/auth'
34
import { trackUserDisconnect } from 'src/lib/analytics'
45
import { firebaseAuth } from 'src/lib/firebase'
@@ -9,17 +10,30 @@ export const useAuth = () => {
910

1011
const isConnected = authStore.user != null
1112

12-
const logout = async () => {
13+
const shouldCountStreak = useCallback(() => {
14+
if (!isConnected) return false
15+
16+
const last = authStore.lastStreakUpdate
17+
if (!last) return true
18+
19+
const today = new Date().toDateString()
20+
const lastDay = new Date(last).toDateString()
21+
22+
return today !== lastDay
23+
}, [isConnected, authStore.lastStreakUpdate])
24+
25+
const logout = useCallback(async () => {
1326
trackUserDisconnect()
1427
signOut(firebaseAuth)
1528
authStore.clear()
1629
return await firebaseAuth.signOut()
17-
}
30+
}, [authStore])
1831

1932
return {
2033
...authModalStore,
2134
...authStore,
2235
isConnected,
36+
shouldCountStreak,
2337
logout,
2438
}
2539
}

src/features/auth/stores/authStore.ts

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

55
type AuthState = {
66
user: User | null
7+
lastStreakUpdate?: number
78
providerId: string | null
89
}
910

1011
type AuthActions = {
1112
initState: (state: AuthState) => void
1213
setStreak: (streak: number) => void
14+
setLastStreakUpdate: (timestamp: number) => void
1315
clear: () => void
1416
}
1517

18+
type AuthStoreType = AuthState & AuthActions
1619
export const AuthStore = create(
17-
persist<AuthState & AuthActions>(
20+
persist<AuthStoreType>(
1821
(set) => ({
1922
user: null,
2023
providerId: null,
24+
lastStreakUpdate: undefined,
25+
setLastStreakUpdate: (timestamp: number) => set({ lastStreakUpdate: timestamp }),
2126
initState: (newState: AuthState) =>
2227
set({
2328
user: newState.user,
2429
providerId: newState.providerId,
2530
}),
2631
setStreak: (streak: number) =>
2732
set((state) => ({
33+
lastStreakUpdate: Date.now(),
2834
user: {
2935
...state.user!,
3036
streak,
3137
},
3238
})),
33-
clear: () => set({ user: null }),
39+
clear: () => set({ user: null, lastStreakUpdate: undefined }),
3440
}),
3541
{
36-
name: 'auth-storage', // key in localStorage
42+
version: 1,
43+
name: 'auth-storage',
44+
migrate: (persistedState, version) => {
45+
const typedPersistedState = persistedState as unknown as AuthStoreType
46+
if (version === 0) {
47+
return {
48+
...typedPersistedState,
49+
lastStreakUpdate: undefined,
50+
}
51+
}
52+
return typedPersistedState
53+
},
3754
}
3855
)
3956
)

0 commit comments

Comments
 (0)