This commit is contained in:
archer
2023-07-18 11:50:16 +08:00
parent f9d83c481f
commit 505aff3dbf
26 changed files with 216 additions and 210 deletions

View File

@@ -15,12 +15,12 @@ type State = {
setChatData: (e: InitChatResponse | ((e: InitChatResponse) => InitChatResponse)) => void;
lastChatAppId: string;
setLastChatAppId: (id: string) => void;
lastHistoryId: string;
setLastHistoryId: (id: string) => void;
lastChatId: string;
setLastChatId: (id: string) => void;
};
const defaultChatData: InitChatResponse = {
historyId: '',
chatId: '',
appId: '',
app: {
name: '',
@@ -43,10 +43,10 @@ export const useChatStore = create<State>()(
state.lastChatAppId = id;
});
},
lastHistoryId: '',
setLastHistoryId(id: string) {
lastChatId: '',
setLastChatId(id: string) {
set((state) => {
state.lastHistoryId = id;
state.lastChatId = id;
});
},
history: [],
@@ -63,25 +63,36 @@ export const useChatStore = create<State>()(
});
return null;
},
async delHistory(historyId) {
async delHistory(chatId) {
set((state) => {
state.history = state.history.filter((item) => item._id !== historyId);
state.history = state.history.filter((item) => item._id !== chatId);
});
await delChatHistoryById(historyId);
await delChatHistoryById(chatId);
},
updateHistory(history) {
const index = get().history.findIndex((item) => item._id === history._id);
set((state) => {
if (index > -1) {
const newHistory = [
history,
...get().history.slice(0, index),
...get().history.slice(index + 1)
];
state.history = newHistory;
} else {
state.history = [history, ...state.history];
}
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,
@@ -101,7 +112,7 @@ export const useChatStore = create<State>()(
name: 'chatStore',
partialize: (state) => ({
lastChatAppId: state.lastChatAppId,
lastHistoryId: state.lastHistoryId
lastChatId: state.lastChatId
})
}
)

View File

@@ -12,13 +12,13 @@ type State = {
setShareChatData: (e: ShareChatType | ((e: ShareChatType) => ShareChatType)) => void;
shareChatHistory: ShareChatHistoryItemType[];
saveChatResponse: (e: {
historyId: string;
chatId: string;
prompts: ChatSiteItemType[];
variables: Record<string, any>;
shareId: string;
}) => { newChatId: string };
delOneShareHistoryByHistoryId: (historyId: string) => void;
delShareChatHistoryItemById: (e: { historyId: string; index: number }) => void;
delOneShareHistoryByChatId: (chatId: string) => void;
delShareChatHistoryItemById: (e: { chatId: string; index: number }) => void;
delManyShareChatHistoryByShareId: (shareId?: string) => void;
};
@@ -62,15 +62,15 @@ export const useShareChatStore = create<State>()(
});
},
shareChatHistory: [],
saveChatResponse({ historyId, prompts, variables, shareId }) {
const history = get().shareChatHistory.find((item) => item._id === historyId);
saveChatResponse({ chatId, prompts, variables, shareId }) {
const history = get().shareChatHistory.find((item) => item._id === chatId);
const newChatId = history ? '' : nanoid();
const historyList = (() => {
if (history) {
return get().shareChatHistory.map((item) =>
item._id === historyId
item._id === chatId
? {
...item,
title: prompts[prompts.length - 2]?.value,
@@ -102,18 +102,16 @@ export const useShareChatStore = create<State>()(
newChatId
};
},
delOneShareHistoryByHistoryId(historyId: string) {
delOneShareHistoryByChatId(chatId: string) {
set((state) => {
state.shareChatHistory = state.shareChatHistory.filter(
(item) => item._id !== historyId
);
state.shareChatHistory = state.shareChatHistory.filter((item) => item._id !== chatId);
});
},
delShareChatHistoryItemById({ historyId, index }) {
delShareChatHistoryItemById({ chatId, index }) {
set((state) => {
// update history store
const newHistoryList = state.shareChatHistory.map((item) =>
item._id === historyId
item._id === chatId
? {
...item,
chats: [...item.chats.slice(0, index), ...item.chats.slice(index + 1)]