mirror of https://github.com/usememos/memos
refactor: user v1 store (#2047)
parent
f5793c142c
commit
a6a1898c41
@ -0,0 +1,4 @@
|
|||||||
|
import useMemoCacheStore from "./memo";
|
||||||
|
import useUserV1Store from "./user";
|
||||||
|
|
||||||
|
export { useUserV1Store, useMemoCacheStore };
|
@ -0,0 +1,31 @@
|
|||||||
|
import { create } from "zustand";
|
||||||
|
import * as api from "@/helpers/api";
|
||||||
|
import { convertResponseModelUser } from "../module";
|
||||||
|
|
||||||
|
interface UserV1Store {
|
||||||
|
userMapByUsername: Record<string, User>;
|
||||||
|
getOrFetchUserByUsername: (username: string) => Promise<User>;
|
||||||
|
getUserByUsername: (username: string) => User;
|
||||||
|
}
|
||||||
|
|
||||||
|
const useUserV1Store = create<UserV1Store>()((set, get) => ({
|
||||||
|
userMapByUsername: {},
|
||||||
|
getOrFetchUserByUsername: async (username: string) => {
|
||||||
|
const userMap = get().userMapByUsername;
|
||||||
|
if (userMap[username]) {
|
||||||
|
return userMap[username] as User;
|
||||||
|
}
|
||||||
|
|
||||||
|
const { data } = await api.getUserByUsername(username);
|
||||||
|
const user = convertResponseModelUser(data);
|
||||||
|
userMap[username] = user;
|
||||||
|
set(userMap);
|
||||||
|
return user;
|
||||||
|
},
|
||||||
|
getUserByUsername: (username: string) => {
|
||||||
|
const userMap = get().userMapByUsername;
|
||||||
|
return userMap[username] as User;
|
||||||
|
},
|
||||||
|
}));
|
||||||
|
|
||||||
|
export default useUserV1Store;
|
@ -1 +0,0 @@
|
|||||||
export { useMemoCacheStore } from "./memo";
|
|
Loading…
Reference in New Issue