LibreChat/client/src/store/convoSlice.js

77 lines
2.1 KiB
JavaScript
Raw Normal View History

import { createSlice } from '@reduxjs/toolkit';
2023-02-06 21:17:46 -05:00
const initialState = {
error: false,
title: 'ChatGPT Clone',
2023-02-06 21:17:46 -05:00
conversationId: null,
parentMessageId: null,
2023-03-08 19:47:23 -05:00
jailbreakConversationId: null,
2023-02-19 21:06:21 -05:00
conversationSignature: null,
clientId: null,
invocationId: null,
chatGptLabel: null,
promptPrefix: null,
2023-03-05 14:41:50 -05:00
convosLoading: false,
pageNumber: 1,
2023-03-15 04:05:14 +08:00
pages: 1,
refreshConvoHint: 0,
2023-03-15 04:05:14 +08:00
convos: [],
2023-02-06 21:17:46 -05:00
};
const currentSlice = createSlice({
name: 'convo',
initialState,
reducers: {
refreshConversation: (state, action) => {
state.refreshConvoHint = state.refreshConvoHint + 1;
},
2023-02-06 21:17:46 -05:00
setConversation: (state, action) => {
return { ...state, ...action.payload };
2023-02-06 21:17:46 -05:00
},
setError: (state, action) => {
state.error = action.payload;
2023-03-05 14:41:50 -05:00
},
2023-03-15 04:05:14 +08:00
increasePage: (state) => {
2023-03-05 14:41:50 -05:00
state.pageNumber = state.pageNumber + 1;
2023-03-06 08:58:52 -05:00
},
2023-03-15 04:05:14 +08:00
decreasePage: (state) => {
state.pageNumber = state.pageNumber - 1;
},
setPage: (state, action) => {
state.pageNumber = action.payload;
},
setNewConvo: (state) => {
state.error = false;
2023-03-15 04:05:14 +08:00
state.title = 'ChatGPT Clone';
state.conversationId = null;
state.parentMessageId = null;
state.jailbreakConversationId = null;
state.conversationSignature = null;
state.clientId = null;
state.invocationId = null;
state.chatGptLabel = null;
state.promptPrefix = null;
state.convosLoading = false;
},
2023-03-06 08:58:52 -05:00
setConvos: (state, action) => {
state.convos = action.payload.sort(
(a, b) => new Date(b.createdAt) - new Date(a.createdAt)
2023-03-06 12:49:22 -05:00
);
2023-03-07 13:53:23 -05:00
},
2023-03-15 04:05:14 +08:00
setPages: (state, action) => {
state.pages = action.payload;
},
2023-03-07 13:53:23 -05:00
removeConvo: (state, action) => {
state.convos = state.convos.filter((convo) => convo.conversationId !== action.payload);
},
removeAll: (state) => {
state.convos = [];
2023-03-06 12:49:22 -05:00
}
2023-02-06 21:17:46 -05:00
}
});
2023-03-15 04:05:14 +08:00
export const { refreshConversation, setConversation, setPages, setConvos, setNewConvo, setError, increasePage, decreasePage, setPage, removeConvo, removeAll } =
2023-03-07 13:53:23 -05:00
currentSlice.actions;
2023-02-06 21:17:46 -05:00
export default currentSlice.reducer;