FastGPT/projects/app/src/web/support/user/useUserStore.ts
Archer b5c98a4f63
Plugin runtime (#2050)
* feat: plugin run (#1950)

* feat: plugin run

* fix

* ui

* fix

* change user input type

* fix

* fix

* temp

* split out plugin chat

* perf: chatbox

* perf: chatbox

* fix: plugin runtime (#2032)

* fix: plugin runtime

* fix

* fix build

* fix build

* perf: chat send prompt

* perf: chat log ux

* perf: chatbox context and share page plugin runtime

* perf: plugin run time config

* fix: ts

* feat: doc

* perf: isPc check

* perf: variable input render

* feat: app search

* fix: response box height

* fix: phone ui

* perf: lock

* perf: plugin route

* fix: chat (#2049)

---------

Co-authored-by: heheer <71265218+newfish-cmyk@users.noreply.github.com>
2024-07-15 22:50:48 +08:00

92 lines
2.5 KiB
TypeScript

import { create } from 'zustand';
import { devtools, persist } from 'zustand/middleware';
import { immer } from 'zustand/middleware/immer';
import type { UserUpdateParams } from '@/types/user';
import type { UserType } from '@fastgpt/global/support/user/type.d';
import { getTokenLogin, putUserInfo } from '@/web/support/user/api';
import { FeTeamPlanStatusType } from '@fastgpt/global/support/wallet/sub/type';
import { getTeamPlanStatus } from './team/api';
type State = {
systemMsgReadId: string;
setSysMsgReadId: (id: string) => void;
userInfo: UserType | null;
initUserInfo: () => Promise<UserType>;
setUserInfo: (user: UserType | null) => void;
updateUserInfo: (user: UserUpdateParams) => Promise<void>;
teamPlanStatus: FeTeamPlanStatusType | null;
initTeamPlanStatus: () => Promise<any>;
};
export const useUserStore = create<State>()(
devtools(
persist(
immer((set, get) => ({
systemMsgReadId: '',
setSysMsgReadId(id: string) {
set((state) => {
state.systemMsgReadId = id;
});
},
userInfo: null,
async initUserInfo() {
get().initTeamPlanStatus();
const res = await getTokenLogin();
get().setUserInfo(res);
//设置html的fontsize
const html = document?.querySelector('html');
if (html) {
// html.style.fontSize = '16px';
}
return res;
},
setUserInfo(user: UserType | null) {
set((state) => {
state.userInfo = user ? user : null;
});
},
async updateUserInfo(user: UserUpdateParams) {
const oldInfo = (get().userInfo ? { ...get().userInfo } : null) as UserType | null;
set((state) => {
if (!state.userInfo) return;
state.userInfo = {
...state.userInfo,
...user
};
});
try {
await putUserInfo(user);
} catch (error) {
set((state) => {
state.userInfo = oldInfo;
});
return Promise.reject(error);
}
},
// team
teamPlanStatus: null,
initTeamPlanStatus() {
return getTeamPlanStatus().then((res) => {
set((state) => {
state.teamPlanStatus = res;
});
return res;
});
}
})),
{
name: 'userStore',
partialize: (state) => ({
systemMsgReadId: state.systemMsgReadId
})
}
)
)
);