Skip to content

Commit

Permalink
feature: prevent stacking of duplicate notifications (#2502)
Browse files Browse the repository at this point in the history
### TL;DR

Removed unnecessary console log statements and improved notification handling by adding a unique key parameter to avoid duplicate notifications.

### What changed?

- Removed `console.log` statements in `backend-ai-session-list.ts` and `lablup-notification.ts` files.
- Enhanced the `show` method in `LablupNotification` component to include a `key` parameter for uniquely identifying notifications.
- Added logic to prevent dispatching duplicate notifications and handled the 'Network disconnected' message specifically.

### How to test?

1. Trigger an error in the application and verify that console log statements no longer appear.
2. Check that notifications are correctly displayed without duplication.
3. Ensure the 'Network disconnected' message is handled by the `NetworkStatusBanner` component.

### Why make this change?

To clean up the console output and improve the notification system by preventing duplicate messages and enhancing user experience.

---

<!--
Please precisely, concisely, and concretely describe what this PR changes, the rationale behind codes,
and how it affects the users and other developers.
-->

**Checklist:** (if applicable)

- [ ] Mention to the original issue
- [ ] Documentation
- [ ] Minium required manager version
- [ ] Specific setting for review (eg., KB link, endpoint or how to setup)
- [ ] Minimum requirements to check during review
- [ ] Test case(s) to demonstrate the difference of before/after
  • Loading branch information
yomybaby committed Jul 3, 2024
1 parent 142e0d3 commit e4145c4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 13 deletions.
1 change: 0 additions & 1 deletion src/components/backend-ai-session-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1271,7 +1271,6 @@ export default class BackendAISessionList extends BackendAIPage {
}, refreshTime);
}
this._listStatus?.hide();
console.log(err);
if (err && err.message) {
this.notification.text = PainKiller.relieve(err.title);
this.notification.detail = err.message;
Expand Down
39 changes: 27 additions & 12 deletions src/components/lablup-notification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,30 +177,46 @@ export default class LablupNotification extends LitElement {
*
* @param {boolean} persistent - if persistent is false, the snackbar is hidden automatically after 3000ms
* @param {object} log - Log object that contains detail information
* @param {string} key - notification key. If it already exists, the notification will be updated.
* */
show(persistent = false, log: Record<string, unknown> = Object()) {
show(
persistent = false,
log: Record<string, unknown> = Object(),
key: string,
) {
if (this.text === '_DISCONNECTED') {
return;
}
const shouldSaveLog = Object.keys(log).length !== 0;
if (shouldSaveLog) {
console.log(log);
this._saveToLocalStorage('backendaiwebui.logs', log);
}

const messageDetail = {
open: true,
type: shouldSaveLog ? 'error' : null,
message: this.text,
description: this.text ? undefined : this.detail,
to: shouldSaveLog ? '/usersettings?tab=logs' : this.url,
duration: persistent ? 0 : undefined,
// closeIcon: persistent,
};

const event: CustomEvent = new CustomEvent('add-bai-notification', {
detail: {
open: true,
type: shouldSaveLog ? 'error' : null,
message: this.text,
description: this.text ? undefined : this.detail,
to: shouldSaveLog ? '/usersettings?tab=logs' : this.url,
duration: persistent ? 0 : undefined,
// closeIcon: persistent,
key:
typeof key === 'undefined' && messageDetail.type === 'error'
? `_no_key_from_lablup_notification:${JSON.stringify(messageDetail)}`
: key,
...messageDetail,
},
});
document.dispatchEvent(event);
this._spawnDesktopNotification('Backend.AI', this.text, '');

// Ignore the event if the message is 'Network disconnected because it is handled by `NetworkStatusBanner` component.
if (messageDetail.message !== 'Network disconnected.') {
document.dispatchEvent(event);
this._spawnDesktopNotification('Backend.AI', this.text, '');
}
}

// /**
Expand Down Expand Up @@ -325,7 +341,6 @@ export default class LablupNotification extends LitElement {
* @param {string} logMessages - Message to save
* */
_saveToLocalStorage(key, logMessages) {
console.log(logMessages);
const previous_log = JSON.parse(localStorage.getItem(key) || '{}');
let current_log: Array<any> = [];

Expand Down

0 comments on commit e4145c4

Please sign in to comment.