Skip to content

Commit

Permalink
feat: add error toasts
Browse files Browse the repository at this point in the history
Signed-off-by: Lin Wang <[email protected]>
  • Loading branch information
wanglam committed Dec 6, 2023
1 parent 3937bb6 commit 6c5f87d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
8 changes: 6 additions & 2 deletions public/components/edit_conversation_name_modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const EditConversationNameModal = ({
},
} = useCore();
const titleInputRef = useRef<HTMLInputElement>(null);
const { loading, abort, patchSession } = usePatchSession();
const { loading, abort, patchSession, isAborted } = usePatchSession();

const handleCancel = useCallback(() => {
abort();
Expand All @@ -41,11 +41,15 @@ export const EditConversationNameModal = ({
await patchSession(sessionId, title);
toasts.addSuccess('This conversation was successfully updated.');
} 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, toasts.addSuccess]);
}, [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,
};
};
8 changes: 6 additions & 2 deletions public/tabs/history/delete_conversation_confirm_modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const DeleteConversationConfirmModal = ({
notifications: { toasts },
},
} = useCore();
const { loading, deleteSession, abort } = useDeleteSession();
const { loading, deleteSession, abort, isAborted } = useDeleteSession();

const handleCancel = useCallback(() => {
abort();
Expand All @@ -35,11 +35,15 @@ export const DeleteConversationConfirmModal = ({
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, toasts.addSuccess]);
}, [onClose, deleteSession, sessionId, toasts.addSuccess, toasts.addDanger, isAborted]);

return (
<EuiConfirmModal
Expand Down

0 comments on commit 6c5f87d

Please sign in to comment.