Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat add toasts for conversation actions #39

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions public/components/chat_window_header_title.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ export const ChatWindowHeaderTitle = React.memo(() => {
onClick={() => {
closePopover();
loadChat(undefined);
// Only show toast when previous session saved
if (!!chatContext.sessionId) {
core.services.notifications.toasts.addSuccess(
'A new conversation is started and the previous one is saved.'
);
}
SuZhou-Joe marked this conversation as resolved.
Show resolved Hide resolved
}}
>
New conversation
Expand Down
15 changes: 13 additions & 2 deletions public/components/edit_conversation_name_modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import React, { useCallback, useRef } from 'react';

import { EuiConfirmModal, EuiFieldText, EuiSpacer, EuiText } from '@elastic/eui';
import { usePatchSession } from '../hooks/use_sessions';
import { useCore } from '../contexts/core_context';

interface EditConversationNameModalProps {
onClose?: (status: 'updated' | 'cancelled' | 'errored', newTitle?: string) => void;
Expand All @@ -19,8 +20,13 @@ export const EditConversationNameModal = ({
sessionId,
defaultTitle,
}: EditConversationNameModalProps) => {
const {
services: {
notifications: { toasts },
},
} = useCore();
const titleInputRef = useRef<HTMLInputElement>(null);
const { loading, abort, patchSession } = usePatchSession();
const { loading, abort, patchSession, isAborted } = usePatchSession();

const handleCancel = useCallback(() => {
abort();
Expand All @@ -33,12 +39,17 @@ export const EditConversationNameModal = ({
}
try {
await patchSession(sessionId, title);
toasts.addSuccess('This conversation was successfully updated.');
Hailong-am marked this conversation as resolved.
Show resolved Hide resolved
} catch (_e) {
if (isAborted()) {
return;
}
onClose?.('errored');
toasts.addDanger('There was an error. The name failed to update.');
return;
}
onClose?.('updated', title);
}, [onClose, sessionId, patchSession]);
}, [onClose, sessionId, patchSession, toasts.addSuccess, toasts.addDanger, isAborted]);

return (
<EuiConfirmModal
Expand Down
20 changes: 17 additions & 3 deletions public/hooks/use_sessions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,13 @@ export const useDeleteSession = () => {
.delete(`${ASSISTANT_API.SESSION}/${sessionId}`, {
signal: abortControllerRef.current.signal,
})
.then((payload) => dispatch({ type: 'success', payload }))
.catch((error) => dispatch({ type: 'failure', error }));
.then((payload) => {
dispatch({ type: 'success', payload });
})
.catch((error) => {
dispatch({ type: 'failure', error });
throw error;
});
},
[core.services.http]
);
Expand All @@ -31,9 +36,12 @@ export const useDeleteSession = () => {
abortControllerRef.current?.abort();
}, []);

const isAborted = useCallback(() => !!abortControllerRef.current?.signal.aborted, []);

return {
...state,
abort,
isAborted,
deleteSession,
};
};
Expand All @@ -55,7 +63,10 @@ export const usePatchSession = () => {
signal: abortControllerRef.current.signal,
})
.then((payload) => dispatch({ type: 'success', payload }))
.catch((error) => dispatch({ type: 'failure', error }));
.catch((error) => {
dispatch({ type: 'failure', error });
throw error;
});
},
[core.services.http]
);
Expand All @@ -64,9 +75,12 @@ export const usePatchSession = () => {
abortControllerRef.current?.abort();
}, []);

const isAborted = useCallback(() => !!abortControllerRef.current?.signal.aborted, []);

return {
...state,
abort,
isAborted,
patchSession,
};
};
7 changes: 7 additions & 0 deletions public/tabs/history/__tests__/chat_history_page.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ import { ChatHistoryPage } from '../chat_history_page';
const setup = () => {
const useCoreMock = {
services: {
notifications: {
toasts: {
addSuccess: jest.fn(),
addDanger: jest.fn(),
addError: jest.fn(),
},
},
sessions: {
sessions$: new BehaviorSubject({
objects: [
Expand Down
15 changes: 13 additions & 2 deletions public/tabs/history/delete_conversation_confirm_modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import React, { useCallback } from 'react';
import { EuiConfirmModal, EuiText } from '@elastic/eui';

import { useDeleteSession } from '../../hooks/use_sessions';
import { useCore } from '../../contexts/core_context';

interface DeleteConversationConfirmModalProps {
onClose?: (status: 'canceled' | 'errored' | 'deleted') => void;
Expand All @@ -18,7 +19,12 @@ export const DeleteConversationConfirmModal = ({
onClose,
sessionId,
}: DeleteConversationConfirmModalProps) => {
const { loading, deleteSession, abort } = useDeleteSession();
const {
services: {
notifications: { toasts },
},
} = useCore();
const { loading, deleteSession, abort, isAborted } = useDeleteSession();

const handleCancel = useCallback(() => {
abort();
Expand All @@ -27,12 +33,17 @@ export const DeleteConversationConfirmModal = ({
const handleConfirm = useCallback(async () => {
try {
await deleteSession(sessionId);
toasts.addSuccess('The conversation was successfully deleted.');
} catch (_e) {
if (isAborted()) {
return;
}
onClose?.('errored');
toasts.addDanger('There was an error. The conversation failed to delete.');
return;
}
onClose?.('deleted');
}, [onClose, deleteSession, sessionId]);
}, [onClose, deleteSession, sessionId, toasts.addSuccess, toasts.addDanger, isAborted]);

return (
<EuiConfirmModal
Expand Down
Loading