Skip to content

Commit c7b0bae

Browse files
committed
feat: Separate useAuth and AuthStore.
1 parent 7a7a22e commit c7b0bae

7 files changed

Lines changed: 51 additions & 24 deletions

File tree

src/components/Layout/Header.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,11 @@ 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 } from 'src/features/auth'
12+
import { AuthModal, 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'
17-
import { useAuth } from 'src/stores/user'
1817

1918
export const Header = () => {
2019
const { user } = useAuth()

src/features/auth/components/AuthModal.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { AuthProvider, OAuthProvider, signInWithPopup } from 'firebase/auth'
22
import { FaGithub, FaGoogle } from 'react-icons/fa'
33
import ReactModal from 'react-modal'
44
import toast from 'react-simple-toasts'
5-
import { useAuth } from 'src/stores/user'
5+
import { useAuth } from 'src/features/auth'
66
import { auth, githubProvider, googleProvider } from '../api/Config'
77

88
type AuthModalProps = {

src/features/auth/hooks/useAuth.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { AuthStore } from 'src/features/auth'
2+
import { useBookmarks } from 'src/stores/bookmarks'
3+
4+
export const useAuth = () => {
5+
const authStore = AuthStore()
6+
const bookmarksStore = useBookmarks()
7+
const { initState, accessToken, user } = authStore
8+
const logout = () => {
9+
bookmarksStore.clear()
10+
authStore.clear()
11+
}
12+
13+
return { initState, logout, accessToken, user }
14+
}

src/features/auth/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
export * from './api/Config'
22
export * from './components/AuthModal'
3+
export * from './hooks/useAuth'
4+
export * from './stores/authStore'
35
export * from './types'
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ type AuthState = {
99

1010
type AuthActions = {
1111
initState: (state: AuthState) => void
12-
logout: () => void
12+
clear: () => void
1313
}
1414

15-
export const useAuth = create(
15+
export const AuthStore = create(
1616
persist<AuthState & AuthActions>(
1717
(set) => ({
1818
accessToken: null,
@@ -22,7 +22,7 @@ export const useAuth = create(
2222
accessToken: newState.accessToken,
2323
user: newState.user,
2424
}),
25-
logout: () => set({ user: null }),
25+
clear: () => set({ user: null }),
2626
}),
2727
{
2828
name: 'auth-storage', // key in localStorage

src/features/settings/components/ProfileSettings.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@ import { useRef } from 'react'
22
import { RiFileDownloadFill, RiFileUploadFill } from 'react-icons/ri'
33
import toast from 'react-simple-toasts'
44
import { SettingsContentLayout } from 'src/components/Layout/SettingsContentLayout'
5-
import { User } from 'src/features/auth'
5+
import { useAuth, User } from 'src/features/auth'
66
import { BookmarkedPost } from 'src/features/bookmarks'
77
import { useBookmarks } from 'src/stores/bookmarks'
8-
import { useAuth } from 'src/stores/user'
98
import { BookmarkItem } from './BookmarkSettings'
109
import './bookmarkSettings/bookmarkSettings.css'
1110

src/stores/bookmarks.ts

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,37 @@
1-
import { create } from 'zustand';
1+
import { create } from 'zustand'
22

3-
import { BookmarkedPost } from "src/features/bookmarks";
4-
import { persist } from 'zustand/middleware';
3+
import { BookmarkedPost } from 'src/features/bookmarks'
4+
import { persist } from 'zustand/middleware'
55

66
type BookmarksState = {
77
userBookmarks: BookmarkedPost[]
8-
};
8+
}
99

1010
type BookmarksActions = {
11-
bookmarkPost: (post: BookmarkedPost) => void;
12-
unbookmarkPost: (post: BookmarkedPost) => void;
13-
initState: (state: BookmarksState) => void;
11+
bookmarkPost: (post: BookmarkedPost) => void
12+
unbookmarkPost: (post: BookmarkedPost) => void
13+
initState: (state: BookmarksState) => void
14+
clear: () => void
1415
}
1516

16-
17-
export const useBookmarks = create(persist<BookmarksState & BookmarksActions>((set) => ({
18-
userBookmarks: [],
19-
bookmarkPost: (post: BookmarkedPost) => set((state) => ({ userBookmarks: [post, ...state.userBookmarks] })),
20-
unbookmarkPost: (post: BookmarkedPost) => set((state) => ({ userBookmarks: state.userBookmarks.filter((bookmarkedPost) => bookmarkedPost.url !== post.url), })),
21-
initState: (newState: BookmarksState) => set(() => ({ userBookmarks: newState.userBookmarks }))
22-
}), {
23-
name: 'bookmarks_storage'
24-
}));
17+
export const useBookmarks = create(
18+
persist<BookmarksState & BookmarksActions>(
19+
(set) => ({
20+
userBookmarks: [],
21+
bookmarkPost: (post: BookmarkedPost) =>
22+
set((state) => ({ userBookmarks: [post, ...state.userBookmarks] })),
23+
unbookmarkPost: (post: BookmarkedPost) =>
24+
set((state) => ({
25+
userBookmarks: state.userBookmarks.filter(
26+
(bookmarkedPost) => bookmarkedPost.url !== post.url
27+
),
28+
})),
29+
initState: (newState: BookmarksState) =>
30+
set(() => ({ userBookmarks: newState.userBookmarks })),
31+
clear: () => set({ userBookmarks: [] }),
32+
}),
33+
{
34+
name: 'bookmarks_storage',
35+
}
36+
)
37+
)

0 commit comments

Comments
 (0)