Skip to content

Commit

Permalink
fixed the saved session
Browse files Browse the repository at this point in the history
  • Loading branch information
himanshudube97 committed Oct 8, 2024
1 parent f405f64 commit b8f4a59
Show file tree
Hide file tree
Showing 3 changed files with 188 additions and 135 deletions.
136 changes: 7 additions & 129 deletions src/components/DataAnalysis/LLMSummary.tsx
Original file line number Diff line number Diff line change
@@ -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';

Check warning on line 3 in src/components/DataAnalysis/LLMSummary.tsx

View check run for this annotation

Codecov / codecov/patch

src/components/DataAnalysis/LLMSummary.tsx#L3

Added line #L3 was not covered by tests
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';

Check warning on line 12 in src/components/DataAnalysis/LLMSummary.tsx

View check run for this annotation

Codecov / codecov/patch

src/components/DataAnalysis/LLMSummary.tsx#L12

Added line #L12 was not covered by tests

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);
Expand All @@ -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) {
Expand Down Expand Up @@ -274,19 +165,6 @@ export const LLMSummary = ({
</Box>

{/* Modal */}
{isBoxOpen && (
<OverWriteDialog
open={isBoxOpen}
setIsBoxOpen={setIsBoxOpen}
modalName={modalName}
onSubmit={onSubmit}
submitFeedback={submitFeedback}
onConfirmNavigation={onConfirmNavigation}
handleNewSession={handleNewSession}
setModalName={setModalName}
oldSessionName={oldSessionMetaInfo.session_name}
/>
)}
</Box>
</Box>
);
Expand Down
47 changes: 42 additions & 5 deletions src/components/DataAnalysis/OverwriteBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Check warning on line 6 in src/components/DataAnalysis/OverwriteBox.tsx

View check run for this annotation

Codecov / codecov/patch

src/components/DataAnalysis/OverwriteBox.tsx#L6

Added line #L6 was not covered by tests
// Define the form data type
interface FormData {
sessionName: string;
Expand All @@ -18,8 +18,10 @@ export const OverWriteDialog = ({
onConfirmNavigation,
setModalName,
submitFeedback,
oldSessionName,
oldSessionMetaInfo,
handleNewSession,
handleEditSession,
selectedSession,
}: {
open: boolean;
modalName: string;
Expand All @@ -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 {
Expand All @@ -43,7 +47,7 @@ export const OverWriteDialog = ({
feedback: '',
},
});

const oldSessionName = oldSessionMetaInfo.session_name;

Check warning on line 50 in src/components/DataAnalysis/OverwriteBox.tsx

View check run for this annotation

Codecov / codecov/patch

src/components/DataAnalysis/OverwriteBox.tsx#L50

Added line #L50 was not covered by tests
const handleClose = () => {
reset({
sessionName: '',
Expand Down Expand Up @@ -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: () => {

Check warning on line 270 in src/components/DataAnalysis/OverwriteBox.tsx

View check run for this annotation

Codecov / codecov/patch

src/components/DataAnalysis/OverwriteBox.tsx#L270

Added line #L270 was not covered by tests
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);

Check warning on line 284 in src/components/DataAnalysis/OverwriteBox.tsx

View check run for this annotation

Codecov / codecov/patch

src/components/DataAnalysis/OverwriteBox.tsx#L282-L284

Added lines #L282 - L284 were not covered by tests
},
},
],
},
};

const FormContent = () => {
Expand All @@ -264,7 +301,7 @@ export const OverWriteDialog = ({
>
{ModalData[modalName].subHeading}
</Typography>
{!['UNSAVED_CHANGES', 'RESET_WARNING'].includes(modalName) && (
{!['UNSAVED_CHANGES', 'RESET_WARNING', 'EDIT_SESSION_WARNING'].includes(modalName) && (
<Box sx={{ marginTop: '1.75rem' }}>
<Controller
name={modalName === 'FEEDBACK_FORM' ? 'feedback' : 'sessionName'}
Expand Down
Loading

0 comments on commit b8f4a59

Please sign in to comment.