Skip to content

Commit

Permalink
Merge pull request #1490 from cgjgh/UI-Base-Notification-Config
Browse files Browse the repository at this point in the history
Configurable Connection Notifications
  • Loading branch information
joepavitt authored Dec 4, 2024
2 parents e477c5d + 13357b9 commit 9bac4c2
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 13 deletions.
6 changes: 5 additions & 1 deletion nodes/config/locales/en-US/ui_base.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@
"navigationStyleFixed": "Fixed",
"navigationStyleIcon": "Collapse to icons",
"navigationStyleTemporary": "Appear over content",
"navigationStyleNone": "Always hide"
"navigationStyleNone": "Always hide",
"connectionNotifications": "Connection Notifications",
"showReconnect": "Show reconnect notification",
"showDisconnect": "Show disconnect notification",
"notificationDisplayTime": "notification display time (seconds)"
},
"layout": {
"pages": "Pages",
Expand Down
43 changes: 43 additions & 0 deletions nodes/config/ui_base.html
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,15 @@
},
titleBarStyle: {
value: 'default'
},
showReconnectNotification: {
value: true
},
notificationDisplayTime: {
value: 1
},
showDisconnectNotification: {
value: true
}
},
label: function () {
Expand All @@ -372,6 +381,24 @@
if (this.appIcon) {
$('#node-config-input-appIcon').val(this.appIcon)
}

// backward compatibility for reconnect notification
if (!('showReconnectNotification' in this)) {
this.showReconnectNotification = true
$('#node-config-input-showReconnectNotification').prop('checked', true)
}

// backward compatibility for reconnect notification display time
if (!this.notificationDisplayTime) {
this.notificationDisplayTime = 5
$('#node-config-input-notificationDisplayTime').val(this.notificationDisplayTime)
}

// backward compatibility for disconnect notification
if (!('showDisconnectNotification' in this)) {
this.showDisconnectNotification = true
$('#node-config-input-showDisconnectNotification').prop('checked', true)
}
},
onpaletteadd: function () {
// add the Dashboard 2.0 sidebar
Expand Down Expand Up @@ -2429,4 +2456,20 @@
<input style="margin: 8px 0 10px 16px; width:20px;" type="checkbox" id="node-config-input-showPathInSidebar">
<label style="width:auto" for="node-config-input-showPathInSidebar"><span data-i18n="ui-base.label.showPath"></span></label>
</div>
<div class="form-row" style="margin-bottom: 0;">
<label style="font-weight: 600; width: auto;" data-i18n="ui-base.label.connectionNotifications"></label>
</div>
<div class="form-row form-row-flex" style="align-items: center;">
<input style="margin: 8px 0 10px 16px; width:20px;" type="checkbox" id="node-config-input-showReconnectNotification">
<label style="width:auto" for="node-config-input-showReconnectNotification"><span data-i18n="ui-base.label.showReconnect"></span></label>
</div>
<div class="form-row form-row-flex" style="align-items: center;">
<input style="width: 50px; margin-left: 75px;" type="number" id="node-config-input-notificationDisplayTime">
<label style="width:auto" for="node-config-input-notificationDisplayTime"><span data-i18n="ui-base.label.notificationDisplayTime"></span></label>
</div>

<div class="form-row form-row-flex" style="align-items: center;">
<input style="margin: 8px 0 10px 16px; width:20px;" type="checkbox" id="node-config-input-showDisconnectNotification">
<label style="width:auto" for="node-config-input-showDisconnectNotification"><span data-i18n="ui-base.label.showDisconnect"></span></label>
</div>
</script>
14 changes: 13 additions & 1 deletion nodes/config/ui_base.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,23 @@ module.exports = function (RED) {
config.acceptsClientConfig = ['ui-control', 'ui-notification']
}

// for those upgrading, we need these for backwards compatibility
if (!('includeClientData' in config)) {
// for those upgrading, we need this for backwards compatibility
config.includeClientData = true
}

if (!('showReconnectNotification' in config)) {
config.showReconnectNotification = true
}

if (!('showDisconnectNotification' in config)) {
config.showDisconnectNotification = true
}

if (!('notificationDisplayTime' in config)) {
config.notificationDisplayTime = 5 // Show for 5 seconds
}

// expose these properties at runtime
node.acceptsClientConfig = config.acceptsClientConfig // which node types can be scoped to a specific client
node.includeClientData = config.includeClientData // whether to include client data in msg payloads
Expand Down
37 changes: 26 additions & 11 deletions ui/src/main.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,16 @@ fetch('_setup')
retryCount = 0
disconnected = true
}
// tell the user we're trying to connect
Alerts.emit('Connection Lost', 'Attempting to reconnect to server...', 'red', {
displayTime: 0, // displayTime 0 persists notifications until another notification closes it
allowDismiss: false,
showCountdown: false
})

const dashboard = store.getters['ui/dashboard']
if (dashboard?.showDisconnectNotification) {
// tell the user we're trying to connect
Alerts.emit('Connection Lost', 'Attempting to reconnect to server...', 'red', {
displayTime: 0, // displayTime 0 persists notifications until another notification closes it
allowDismiss: false,
showCountdown: false
})
}
// attempt to reconnect
reconnect()
})
Expand All @@ -171,12 +175,23 @@ fetch('_setup')
console.log('SIO connected')
// if we've just disconnected (i.e. aren't connecting for the first time)
if (disconnected) {
// check vuex store here
const dashboard = store.getters['ui/dashboard']
if (dashboard?.showReconnectNotification) {
// send a notification/alert to the user to let them know the connection is live again
Alerts.emit('Connected', 'Connection re-established.', '#1BC318', {
displayTime: 1,
allowDismiss: true,
showCountdown: true
})
Alerts.emit('Connected', 'Connection re-established.', '#1BC318', {
displayTime: dashboard?.notificationDisplayTime || 5, // default: 5 seconds
allowDismiss: true,
showCountdown: true
})
} else {
//, send a notification for 1 ms to close the disconnected notification
Alerts.emit('Connected', 'Connection re-established.', '#1BC318', {
displayTime: 0.001, // 1 ms
allowDismiss: false,
showCountdown: false
})
}
}
disconnected = false
clearTimeout(reconnectTO)
Expand Down
5 changes: 5 additions & 0 deletions ui/src/store/ui.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ const getters = {
dashboards (state) {
return state.dashboards
},
dashboard (state) {
const dashboards = Object.keys(state.dashboards)
const id = dashboards.length ? dashboards[0] : undefined
return id ? state.dashboards[id] : undefined
},
pages (state) {
return state.pages
},
Expand Down

0 comments on commit 9bac4c2

Please sign in to comment.