From b8f4a591b26971926028f2c47be01a33aed1fc5f Mon Sep 17 00:00:00 2001 From: himanshudube97 Date: Tue, 8 Oct 2024 16:26:30 +0530 Subject: [PATCH] fixed the saved session --- src/components/DataAnalysis/LLMSummary.tsx | 136 +----------------- src/components/DataAnalysis/OverwriteBox.tsx | 47 ++++++- src/pages/analysis/data-analysis.tsx | 140 ++++++++++++++++++- 3 files changed, 188 insertions(+), 135 deletions(-) diff --git a/src/components/DataAnalysis/LLMSummary.tsx b/src/components/DataAnalysis/LLMSummary.tsx index ed732fa4..70f68099 100644 --- a/src/components/DataAnalysis/LLMSummary.tsx +++ b/src/components/DataAnalysis/LLMSummary.tsx @@ -1,115 +1,36 @@ import { Box, Button, IconButton, Typography } from '@mui/material'; import Image from 'next/image'; -import { useEffect, useState, useContext } from 'react'; -import { OverWriteDialog } from './OverwriteBox'; -import { useRouter } from 'next/router'; +import { useEffect, useContext } from 'react'; import { GlobalContext } from '@/contexts/ContextProvider'; import InfoTooltip from '../UI/Tooltip/Tooltip'; import DalgoIcon from '@/assets/icons/dalgoIcon.svg'; - import { copyToClipboard } from '@/utils/common'; import { successToast, errorToast } from '../ToastMessage/ToastHelper'; -import { httpPost } from '@/helpers/http'; -import { useSession } from 'next-auth/react'; import Markdown from 'react-markdown'; import remarkGfm from 'remark-gfm'; import { ContentCopy, ThumbDownAltOutlined } from '@mui/icons-material'; - -export const MODALS = { - SAVE: 'SAVE', - OVERWRITE: 'OVERWRITE', - CONFIRM_SAVEAS: 'CONFIRM_SAVEAS', - FEEDBACK_FORM: 'FEEDBACK_FORM', - UNSAVED_CHANGES: 'UNSAVED_CHANGES', - RESET_WARNING: 'RESET_WARNING', -}; +import { MODALS } from '@/pages/analysis/data-analysis'; export const LLMSummary = ({ resetState, llmSummary, downloadCSV, + setIsBoxOpen, + setModalName, newSessionId, oldSessionMetaInfo, - handleNewSession, }: { resetState: boolean; llmSummary: string; downloadCSV: () => void; + setIsBoxOpen: (x: boolean) => void; + setModalName: (x: string) => void; newSessionId: string; oldSessionMetaInfo: any; - handleNewSession: any; + handleNewSession: (x: any) => void; }) => { - const router = useRouter(); - const { data: session } = useSession(); - const [isBoxOpen, setIsBoxOpen] = useState(false); - const [modalName, setModalName] = useState(MODALS.SAVE); - const [attemptedRoute, setAttemptedRoute] = useState(null); const globalContext = useContext(GlobalContext); - const { dispatch, state } = globalContext?.UnsavedChanges ?? {}; - - //handling save session-> - const handleSaveSession = async ( - overwrite: boolean, - old_session_id: string | null, - session_name: string - ) => { - try { - const response: { success: number } = await httpPost( - session, - `warehouse/ask/${newSessionId}/save`, - { - session_name, - overwrite, - old_session_id, - } - ); - if (response.success) { - successToast(`${session_name} saved successfully`, [], globalContext); - handleNewSession(true); - } - } catch (err: any) { - errorToast(err.message, [], globalContext); - } finally { - setIsBoxOpen(false); - } - }; - - const handleFeedback = async (session_id: string, feedback: string) => { - try { - const response: { success: number } = await httpPost( - session, - `warehouse/ask/${session_id}/feedback`, - { - feedback, - } - ); - if (response.success) { - successToast(`Feedback sent successfully`, [], globalContext); - } - } catch (err: any) { - errorToast(err.message, [], globalContext); - } finally { - setIsBoxOpen(false); - } - }; - - // submitting the session name -> - const onSubmit = (sessionName: string, overwrite: boolean) => { - const oldSessionIdToSend = overwrite ? oldSessionMetaInfo?.oldSessionId : null; - handleSaveSession(overwrite, oldSessionIdToSend, sessionName); - }; - const submitFeedback = (feedback: string) => { - let sessionIdToSend; - if (newSessionId) { - // if we have a newsession or if we have oldsession but again create a new summary (both oldsessionid and newsessionid). - sessionIdToSend = newSessionId; - } else if (oldSessionMetaInfo.oldSessionId) { - //during edit when we have a oldsession id. - sessionIdToSend = oldSessionMetaInfo.oldSessionId; - } - handleFeedback(sessionIdToSend, feedback); - }; // Function to handle copying text -> const handleCopyClick = async () => { const copyRes: boolean = await copyToClipboard(llmSummary); @@ -122,36 +43,6 @@ export const LLMSummary = ({ // checks for the route change-> //cover both cases, while editing, and the first time too wehn the user creats a analysis. - useEffect(() => { - const handleRouteChange = (url: any) => { - if ( - (oldSessionMetaInfo.oldSessionId && newSessionId && state === false) || - (newSessionId && !oldSessionMetaInfo.oldSessionId && state === false) - ) { - router.events.emit('routeChangeError'); - setModalName(MODALS.UNSAVED_CHANGES); - setIsBoxOpen(true); - dispatch({ type: 'SET_UNSAVED_CHANGES' }); - setAttemptedRoute(url); - throw 'Unsaved changes, route change aborted'; - } - }; - - router.events.on('routeChangeStart', handleRouteChange); - - return () => { - router.events.off('routeChangeStart', handleRouteChange); - dispatch({ type: 'CLEAR_UNSAVED_CHANGES' }); - }; - }, [router, oldSessionMetaInfo.oldSessionId, state, newSessionId]); - - //the unsaved modal function-> - const onConfirmNavigation = () => { - if (attemptedRoute) { - dispatch({ type: 'SET_UNSAVED_CHANGES' }); - router.push(attemptedRoute); - } - }; useEffect(() => { if (resetState && newSessionId) { @@ -274,19 +165,6 @@ export const LLMSummary = ({ {/* Modal */} - {isBoxOpen && ( - - )} ); diff --git a/src/components/DataAnalysis/OverwriteBox.tsx b/src/components/DataAnalysis/OverwriteBox.tsx index 85eb29c1..9e72b15c 100644 --- a/src/components/DataAnalysis/OverwriteBox.tsx +++ b/src/components/DataAnalysis/OverwriteBox.tsx @@ -2,8 +2,8 @@ import React, { useEffect } from 'react'; import { Box, Button, TextField, Typography, DialogActions } from '@mui/material'; import CustomDialog from '../Dialog/CustomDialog'; import { useForm, Controller } from 'react-hook-form'; -import { MODALS } from './LLMSummary'; import { useTracking } from '@/contexts/TrackingContext'; +import { MODALS } from '@/pages/analysis/data-analysis'; // Define the form data type interface FormData { sessionName: string; @@ -18,8 +18,10 @@ export const OverWriteDialog = ({ onConfirmNavigation, setModalName, submitFeedback, - oldSessionName, + oldSessionMetaInfo, handleNewSession, + handleEditSession, + selectedSession, }: { open: boolean; modalName: string; @@ -29,7 +31,9 @@ export const OverWriteDialog = ({ setIsBoxOpen: (a: boolean) => void; submitFeedback: (x: string) => void; handleNewSession: (x: boolean) => void; - oldSessionName: string; + oldSessionMetaInfo: any; + handleEditSession: (x: any, y: boolean) => void; + selectedSession: any; }) => { const trackAmplitudeEvent: any = useTracking(); const { @@ -43,7 +47,7 @@ export const OverWriteDialog = ({ feedback: '', }, }); - + const oldSessionName = oldSessionMetaInfo.session_name; const handleClose = () => { reset({ sessionName: '', @@ -249,6 +253,39 @@ export const OverWriteDialog = ({ }, ], }, + EDIT_SESSION_WARNING: { + mainheading: 'Unsaved session', + subHeading: + 'You are about to leave the session without saving your changes.\nAny unsaved work will be lost. Do you wish to continue?', + label: 'Save session', + buttons: [ + { + label: 'Save changes', + variant: 'contained', + sx: { + width: '6.75rem', + padding: '8px 0', + borderRadius: '5px', + }, + onClick: () => { + setModalName(oldSessionName ? MODALS.OVERWRITE : MODALS.SAVE); + }, + }, + { + label: 'Leave Anyway', + variant: 'contained', + sx: { + width: '6.75rem', + padding: '8px 0', + borderRadius: '5px', + }, + onClick: () => { + setIsBoxOpen(false); + handleEditSession(selectedSession, true); + }, + }, + ], + }, }; const FormContent = () => { @@ -264,7 +301,7 @@ export const OverWriteDialog = ({ > {ModalData[modalName].subHeading} - {!['UNSAVED_CHANGES', 'RESET_WARNING'].includes(modalName) && ( + {!['UNSAVED_CHANGES', 'RESET_WARNING', 'EDIT_SESSION_WARNING'].includes(modalName) && ( ; session_id?: string; @@ -22,18 +24,34 @@ interface ProgressEntry { status: 'running' | 'completed' | 'failed'; result?: ProgressResult; } +export const MODALS = { + SAVE: 'SAVE', + OVERWRITE: 'OVERWRITE', + CONFIRM_SAVEAS: 'CONFIRM_SAVEAS', + FEEDBACK_FORM: 'FEEDBACK_FORM', + UNSAVED_CHANGES: 'UNSAVED_CHANGES', + RESET_WARNING: 'RESET_WARNING', + EDIT_SESSION_WARNING: 'EDIT_SESSION_WARNING', +}; interface ProgressResponse { progress: ProgressEntry[]; } export default function DataAnalysis() { const { data: session } = useSession(); + const router = useRouter(); + const [attemptedRoute, setAttemptedRoute] = useState(null); const globalContext = useContext(GlobalContext); + const { dispatch, state } = globalContext?.UnsavedChanges ?? {}; const [loading, setLoading] = useState(false); const [openSavedSessionDialog, setOpenSavedSessionDialog] = useState(false); const [resetState, setResetState] = useState(true); const [isOpen, setIsOpen] = useState(false); + const [selectedSession, setSelectedSession] = useState(); + const [isBoxOpen, setIsBoxOpen] = useState(false); + const [modalName, setModalName] = useState(MODALS.SAVE); + //for the discalimer page. useEffect(() => { const orgSlug = localStorage.getItem('org-slug'); try { @@ -84,7 +102,14 @@ export default function DataAnalysis() { } setResetState(true); }; - const handleEditSession = (info: any) => { + const handleEditSession = (info: any, openEdit: boolean) => { + setSelectedSession(info); + //shows me a modal asking to save the generated summary. + if (newSessionId && !openEdit) { + setIsBoxOpen(true); + setModalName(MODALS.EDIT_SESSION_WARNING); + return; + } setSessionMetaInfo({ newSessionId: '', ...oldSessionMetaInfo, @@ -183,6 +208,102 @@ export default function DataAnalysis() { errorToast(err.message, [], globalContext); } }; + //handling save session-> + const handleSaveSession = async ( + overwrite: boolean, + old_session_id: string | null, + session_name: string + ) => { + try { + const response: { success: number } = await httpPost( + session, + `warehouse/ask/${newSessionId}/save`, + { + session_name, + overwrite, + old_session_id, + } + ); + if (response.success) { + successToast(`${session_name} saved successfully`, [], globalContext); + handleNewSession(true); + } + } catch (err: any) { + errorToast(err.message, [], globalContext); + } finally { + setIsBoxOpen(false); + } + }; + + const handleFeedback = async (session_id: string, feedback: string) => { + try { + const response: { success: number } = await httpPost( + session, + `warehouse/ask/${session_id}/feedback`, + { + feedback, + } + ); + if (response.success) { + successToast(`Feedback sent successfully`, [], globalContext); + } + } catch (err: any) { + errorToast(err.message, [], globalContext); + } finally { + setIsBoxOpen(false); + } + }; + + // Submitting the session name -> Caan be overwrite or new session. + const onSubmit = (sessionName: string, overwrite: boolean) => { + const oldSessionIdToSend = overwrite ? oldSessionMetaInfo?.oldSessionId : null; + handleSaveSession(overwrite, oldSessionIdToSend, sessionName); + }; + + //Submitting Feedback + const submitFeedback = (feedback: string) => { + let sessionIdToSend: any; + if (newSessionId) { + // if we have a newsession or if we have oldsession but again create a new summary (both oldsessionid and newsessionid). + sessionIdToSend = newSessionId; + } else if (oldSessionMetaInfo.oldSessionId) { + //during edit when we have a oldsession id. + sessionIdToSend = oldSessionMetaInfo.oldSessionId; + } + handleFeedback(sessionIdToSend, feedback); + }; + + //Warns user to save the session before moving to some other tab. + useEffect(() => { + const handleRouteChange = (url: any) => { + if ( + (oldSessionMetaInfo.oldSessionId && newSessionId && state === false) || + (newSessionId && !oldSessionMetaInfo.oldSessionId && state === false) + ) { + router.events.emit('routeChangeError'); + setModalName(MODALS.UNSAVED_CHANGES); + setIsBoxOpen(true); + dispatch({ type: 'SET_UNSAVED_CHANGES' }); + setAttemptedRoute(url); + throw 'Unsaved changes, route change aborted'; + } + }; + + router.events.on('routeChangeStart', handleRouteChange); + + return () => { + router.events.off('routeChangeStart', handleRouteChange); + dispatch({ type: 'CLEAR_UNSAVED_CHANGES' }); + }; + }, [router, oldSessionMetaInfo.oldSessionId, state, newSessionId]); + + //the unsaved modal function-> + const onConfirmNavigation = () => { + if (attemptedRoute) { + dispatch({ type: 'SET_UNSAVED_CHANGES' }); + router.push(attemptedRoute); + } + }; return ( <> @@ -222,6 +343,8 @@ export default function DataAnalysis() { {/* Final Summary */} )} {isOpen && } + {isBoxOpen && ( + + )} );