From 360d790282dd17d59fdf7604168024f7ff8e455b Mon Sep 17 00:00:00 2001 From: ManishMadan2882 Date: Sun, 28 Jul 2024 00:58:55 +0530 Subject: [PATCH] save further queries to localstorage --- .../src/conversation/SharedConversation.tsx | 5 ++- .../conversation/sharedConversationSlice.ts | 41 +++++++++---------- .../src/modals/ShareConversationModal.tsx | 1 - 3 files changed, 24 insertions(+), 23 deletions(-) diff --git a/frontend/src/conversation/SharedConversation.tsx b/frontend/src/conversation/SharedConversation.tsx index 4b73579b..78d49a79 100644 --- a/frontend/src/conversation/SharedConversation.tsx +++ b/frontend/src/conversation/SharedConversation.tsx @@ -139,6 +139,7 @@ export const SharedConversation = () => { if (query.response) { responseView = ( { } else if (query.error) { responseView = ( { 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 ( ( '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( 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) { 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; diff --git a/frontend/src/modals/ShareConversationModal.tsx b/frontend/src/modals/ShareConversationModal.tsx index 0015402e..870c032c 100644 --- a/frontend/src/modals/ShareConversationModal.tsx +++ b/frontend/src/modals/ShareConversationModal.tsx @@ -105,7 +105,6 @@ export const ShareConversationModal = ({ conversationService .shareConversation(isPromptable, payload) .then((res) => { - console.log(res.status); return res.json(); }) .then((data) => {