Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './selectors'; | ||
export { createUserStore, UserStoreProvider, useUserStore, useUserStoreApi } from './store'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { UserAuthState, initialUserState } from './slices/auth'; | ||
|
||
export type UserStoreState = UserAuthState; | ||
|
||
export const initialState: UserStoreState = { | ||
...initialUserState, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './slices/auth/selectors'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { StateCreator } from 'zustand/vanilla'; | ||
|
||
import { setNamespace } from '@/utils/storeDebug'; | ||
|
||
import { UserStore } from '../../store'; | ||
|
||
const n = setNamespace('auth'); | ||
|
||
export interface AuthAction { | ||
getUserConfig: () => void; | ||
} | ||
|
||
export const createFileSlice: StateCreator< | ||
UserStore, | ||
[['zustand/devtools', never]], | ||
[], | ||
AuthAction | ||
> = () => ({ | ||
getUserConfig: () => { | ||
console.log(n('userconfig')); | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from './action'; | ||
export * from './initialState'; | ||
export * from './selectors'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
export interface LobeUser { | ||
avatar?: string; | ||
firstName?: string | null; | ||
fullName?: string | null; | ||
id: string; | ||
latestName?: string | null; | ||
username?: string | null; | ||
} | ||
|
||
export interface UserAuthState { | ||
isLoaded?: boolean; | ||
isSignedIn?: boolean; | ||
user?: LobeUser; | ||
userId?: string | null; | ||
} | ||
|
||
export const initialUserState: UserAuthState = {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const userSelectors = {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { StoreApi } from 'zustand'; | ||
import { createContext } from 'zustand-utils'; | ||
import { devtools } from 'zustand/middleware'; | ||
import { shallow } from 'zustand/shallow'; | ||
import { createWithEqualityFn } from 'zustand/traditional'; | ||
import { StateCreator } from 'zustand/vanilla'; | ||
|
||
import { isDev } from '@/utils/env'; | ||
|
||
import { UserStoreState, initialState } from './initialState'; | ||
import { AuthAction, createFileSlice } from './slices/auth'; | ||
|
||
// =============== 鑱氬悎 createStoreFn ============ // | ||
|
||
export type UserStore = UserStoreState & AuthAction; | ||
|
||
const createStore: StateCreator<UserStore, [['zustand/devtools', never]]> = (...parameters) => ({ | ||
...initialState, | ||
...createFileSlice(...parameters), | ||
}); | ||
|
||
// =============== 瀹炶 useStore ============ // | ||
let store: any; | ||
|
||
export const createUserStore = () => { | ||
if (store) return store; | ||
|
||
store = createWithEqualityFn<UserStore>()( | ||
devtools(createStore, { | ||
name: 'LobeChat_User' + (isDev ? '_DEV' : ''), | ||
}), | ||
shallow, | ||
); | ||
|
||
return store; | ||
}; | ||
|
||
export const { | ||
useStore: useUserStore, | ||
useStoreApi: useUserStoreApi, | ||
Provider: UserStoreProvider, | ||
} = createContext<StoreApi<UserStore>>(); |