2023-03-04 17:39:06 -05:00
|
|
|
import { createSlice } from '@reduxjs/toolkit';
|
2023-02-06 21:17:46 -05:00
|
|
|
|
|
|
|
|
const initialState = {
|
2023-02-08 10:49:38 -05:00
|
|
|
error: false,
|
2023-02-11 10:22:15 -05:00
|
|
|
title: 'ChatGPT Clone',
|
2023-02-06 21:17:46 -05:00
|
|
|
conversationId: null,
|
|
|
|
|
parentMessageId: null,
|
2023-02-19 21:06:21 -05:00
|
|
|
conversationSignature: null,
|
|
|
|
|
clientId: null,
|
|
|
|
|
invocationId: null,
|
2023-03-04 17:39:06 -05:00
|
|
|
chatGptLabel: null,
|
|
|
|
|
promptPrefix: null,
|
2023-03-05 14:41:50 -05:00
|
|
|
convosLoading: false,
|
|
|
|
|
pageNumber: 1,
|
2023-03-06 12:49:22 -05:00
|
|
|
convos: []
|
2023-02-06 21:17:46 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const currentSlice = createSlice({
|
|
|
|
|
name: 'convo',
|
|
|
|
|
initialState,
|
|
|
|
|
reducers: {
|
|
|
|
|
setConversation: (state, action) => {
|
2023-02-07 16:22:35 -05:00
|
|
|
return { ...state, ...action.payload };
|
2023-02-06 21:17:46 -05:00
|
|
|
},
|
2023-02-08 10:49:38 -05:00
|
|
|
setError: (state, action) => {
|
|
|
|
|
state.error = action.payload;
|
2023-03-05 14:41:50 -05:00
|
|
|
},
|
|
|
|
|
incrementPage: (state) => {
|
|
|
|
|
state.pageNumber = state.pageNumber + 1;
|
2023-03-06 08:58:52 -05:00
|
|
|
},
|
|
|
|
|
setConvos: (state, action) => {
|
2023-03-06 10:15:07 -05:00
|
|
|
const newConvos = action.payload.filter((convo) => {
|
|
|
|
|
return !state.convos.some((c) => c.conversationId === convo.conversationId);
|
|
|
|
|
});
|
2023-03-06 12:49:22 -05:00
|
|
|
state.convos = [...state.convos, ...newConvos].sort(
|
|
|
|
|
(a, b) => new Date(b.created) - new Date(a.created)
|
|
|
|
|
);
|
2023-03-07 13:53:23 -05:00
|
|
|
},
|
|
|
|
|
removeConvo: (state, action) => {
|
|
|
|
|
state.convos = state.convos.filter((convo) => convo.conversationId !== action.payload);
|
2023-03-06 12:49:22 -05:00
|
|
|
}
|
2023-02-06 21:17:46 -05:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2023-03-07 13:53:23 -05:00
|
|
|
export const { setConversation, setConvos, setError, incrementPage, removeConvo } =
|
|
|
|
|
currentSlice.actions;
|
2023-02-06 21:17:46 -05:00
|
|
|
|
|
|
|
|
export default currentSlice.reducer;
|