Skip to content

Commit

Permalink
馃帹 wip: add user store
Browse files Browse the repository at this point in the history
  • Loading branch information
arvinxx committed Apr 26, 2024
1 parent 2a72d1f commit 75041f2
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/store/user/index.ts
@@ -0,0 +1,2 @@
export * from './selectors';
export { createUserStore, UserStoreProvider, useUserStore, useUserStoreApi } from './store';
7 changes: 7 additions & 0 deletions src/store/user/initialState.ts
@@ -0,0 +1,7 @@
import { UserAuthState, initialUserState } from './slices/auth';

export type UserStoreState = UserAuthState;

export const initialState: UserStoreState = {
...initialUserState,
};
1 change: 1 addition & 0 deletions src/store/user/selectors.ts
@@ -0,0 +1 @@
export * from './slices/auth/selectors';
22 changes: 22 additions & 0 deletions src/store/user/slices/auth/action.ts
@@ -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'));
},
});
3 changes: 3 additions & 0 deletions src/store/user/slices/auth/index.ts
@@ -0,0 +1,3 @@
export * from './action';
export * from './initialState';
export * from './selectors';
17 changes: 17 additions & 0 deletions src/store/user/slices/auth/initialState.ts
@@ -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 = {};
1 change: 1 addition & 0 deletions src/store/user/slices/auth/selectors.ts
@@ -0,0 +1 @@
export const userSelectors = {};
42 changes: 42 additions & 0 deletions src/store/user/store.ts
@@ -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>>();

0 comments on commit 75041f2

Please sign in to comment.