save further queries to localstorage

This commit is contained in:
ManishMadan2882
2024-07-28 00:58:55 +05:30
parent a0dd8f8e0f
commit 360d790282
3 changed files with 24 additions and 23 deletions

View File

@@ -139,6 +139,7 @@ export const SharedConversation = () => {
if (query.response) {
responseView = (
<ConversationBubble
ref={endMessageRef}
className={`${index === queries.length - 1 ? 'mb-32' : 'mb-7'}`}
key={`${index}ANSWER`}
message={query.response}
@@ -148,6 +149,7 @@ export const SharedConversation = () => {
} else if (query.error) {
responseView = (
<ConversationBubble
ref={endMessageRef}
className={`${index === queries.length - 1 ? 'mb-32' : 'mb-7'} `}
key={`${index}ERROR`}
message={query.error}
@@ -191,7 +193,7 @@ export const SharedConversation = () => {
if (question === '') return;
setEventInterrupt(false);
!isRetry && dispatch(addQuery({ prompt: question })); //dispatch only new queries
dispatch(fetchSharedAnswer({ question: '' }));
dispatch(fetchSharedAnswer({ question }));
};
useEffect(() => {
fetchQueries();
@@ -220,6 +222,7 @@ export const SharedConversation = () => {
return (
<Fragment key={index}>
<ConversationBubble
ref={endMessageRef}
className={'mb-1 last:mb-28 md:mb-7'}
key={`${index}QUESTION`}
message={query.prompt}

View File

@@ -12,7 +12,7 @@ const API_STREAMING = import.meta.env.VITE_API_STREAMING === 'true';
interface SharedConversationsType {
queries: Query[];
apiKey?: string;
identifier: string | null;
identifier: string;
status: Status;
date?: string;
title?: string;
@@ -20,15 +20,15 @@ interface SharedConversationsType {
const initialState: SharedConversationsType = {
queries: [],
identifier: null,
identifier: '',
status: 'idle',
};
export const fetchSharedAnswer = createAsyncThunk<Answer, { question: string }>(
'shared/fetchAnswer',
async ({ question }, { dispatch, getState, signal }) => {
console.log('bulaya sahab ji ?');
const state = getState() as RootState;
if (state.preference && state.sharedConversation.apiKey) {
if (API_STREAMING) {
await handleFetchSharedAnswerStreaming(
@@ -43,6 +43,7 @@ export const fetchSharedAnswer = createAsyncThunk<Answer, { question: string }>(
if (data.type === 'end') {
// set status to 'idle'
dispatch(sharedConversationSlice.actions.setStatus('idle'));
dispatch(saveToLocalStorage());
} else if (data.type === 'error') {
// set status to 'failed'
dispatch(sharedConversationSlice.actions.setStatus('failed'));
@@ -137,24 +138,6 @@ export const sharedConversationSlice = createSlice({
},
addQuery(state, action: PayloadAction<Query>) {
state.queries.push(action.payload);
if (state.identifier) {
const previousQueriesStr = localStorage.getItem(state.identifier);
previousQueriesStr
? localStorage.setItem(
state.identifier,
JSON.stringify([
...JSON.parse(previousQueriesStr),
action.payload,
]),
)
: localStorage.setItem(
state.identifier,
JSON.stringify([action.payload]),
);
if (action.payload.prompt) {
fetchSharedAnswer({ question: action.payload.prompt });
}
}
},
updateStreamingQuery(
state,
@@ -188,6 +171,21 @@ export const sharedConversationSlice = createSlice({
const { index, message } = action.payload;
state.queries[index].error = message;
},
saveToLocalStorage(state) {
const previousQueriesStr = localStorage.getItem(state.identifier);
previousQueriesStr
? localStorage.setItem(
state.identifier,
JSON.stringify([
...JSON.parse(previousQueriesStr),
state.queries[state.queries.length - 1],
]),
)
: localStorage.setItem(
state.identifier,
JSON.stringify([state.queries[state.queries.length - 1]]),
);
},
},
extraReducers(builder) {
builder
@@ -214,6 +212,7 @@ export const {
updateQuery,
updateStreamingQuery,
addQuery,
saveToLocalStorage,
} = sharedConversationSlice.actions;
export const selectStatus = (state: RootState) => state.conversation.status;

View File

@@ -105,7 +105,6 @@ export const ShareConversationModal = ({
conversationService
.shareConversation(isPromptable, payload)
.then((res) => {
console.log(res.status);
return res.json();
})
.then((data) => {