import { create } from 'zustand'; import { devtools, persist } from 'zustand/middleware'; import { immer } from 'zustand/middleware/immer'; import { ChatHistoryItemType } from '@/types/chat'; import type { InitChatResponse } from '@/api/response/chat'; import { delChatHistoryById, getChatHistory } from '@/api/chat'; type State = { history: ChatHistoryItemType[]; loadHistory: (data: { appId?: string }) => Promise; delHistory(history: string): Promise; updateHistory: (history: ChatHistoryItemType) => void; chatData: InitChatResponse; setChatData: (e: InitChatResponse | ((e: InitChatResponse) => InitChatResponse)) => void; lastChatAppId: string; setLastChatAppId: (id: string) => void; lastChatId: string; setLastChatId: (id: string) => void; }; const defaultChatData: InitChatResponse = { chatId: '', appId: '', app: { name: '', avatar: '/icon/logo.png', intro: '', canUse: false }, title: '新对话', variables: {}, history: [] }; export const useChatStore = create()( devtools( persist( immer((set, get) => ({ lastChatAppId: '', setLastChatAppId(id: string) { set((state) => { state.lastChatAppId = id; }); }, lastChatId: '', setLastChatId(id: string) { set((state) => { state.lastChatId = id; }); }, history: [], async loadHistory({ appId }) { const oneHistory = get().history[0]; if (oneHistory && oneHistory.appId === appId) return null; const data = await getChatHistory({ appId, pageNum: 1, pageSize: 20 }); set((state) => { state.history = data; }); return null; }, async delHistory(chatId) { set((state) => { state.history = state.history.filter((item) => item._id !== chatId); }); await delChatHistoryById(chatId); }, updateHistory(history) { const index = get().history.findIndex((item) => item._id === history._id); set((state) => { const newHistory = (() => { if (index > -1) { return [ history, ...get().history.slice(0, index), ...get().history.slice(index + 1) ]; } else { return [history, ...state.history]; } })(); // newHistory.sort(function (a, b) { // if (a.top === true && b.top === false) { // return -1; // } else if (a.top === false && b.top === true) { // return 1; // } else { // return 0; // } // }); state.history = newHistory; }); }, chatData: defaultChatData, setChatData(e = defaultChatData) { if (typeof e === 'function') { set((state) => { state.chatData = e(state.chatData); }); } else { set((state) => { state.chatData = e; }); } } })), { name: 'chatStore', partialize: (state) => ({ lastChatAppId: state.lastChatAppId, lastChatId: state.lastChatId }) } ) ) );