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: Toast notification timeout settings #3441

Merged
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
12 changes: 12 additions & 0 deletions src/assets/app.scss
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,18 @@ $shadow-box-padding: 20px;
}
}

@media (max-width: 770px) {
.toast-container {
margin-bottom: 100px !important;
}
}

@media (max-width: 550px) {
.toast-container {
margin-bottom: 126px !important;
}
}

// Localization

@import "localization.scss";
76 changes: 76 additions & 0 deletions src/components/settings/Notifications.vue
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,39 @@
</button>
</div>

<div class="my-4 pt-4">
<h5 class="my-4 settings-subheading">{{ $t("monitorToastMessagesLabel") }}</h5>
<p>{{ $t("monitorToastMessagesDescription") }}</p>

<div class="my-4">
<label for="toastErrorTimeoutSecs" class="form-label">
{{ $t("toastErrorTimeout") }}
</label>
<input
id="toastErrorTimeoutSecs"
v-model="toastErrorTimeoutSecs"
type="number"
class="form-control"
min="-1"
step="1"
/>
</div>

<div class="my-4">
<label for="toastSuccessTimeoutSecs" class="form-label">
{{ $t("toastSuccessTimeout") }}
</label>
<input
id="toastSuccessTimeoutSecs"
v-model="toastSuccessTimeoutSecs"
type="number"
class="form-control"
min="-1"
step="1"
/>
</div>
</div>

<div class="my-4 pt-4">
<h5 class="my-4 settings-subheading">{{ $t("settingsCertificateExpiry") }}</h5>
<p>{{ $t("certificationExpiryDescription") }}</p>
Expand Down Expand Up @@ -58,6 +91,8 @@ export default {

data() {
return {
toastSuccessTimeoutSecs: 20,
toastErrorTimeoutSecs: -1,
/**
* Variable to store the input for new certificate expiry day.
*/
Expand All @@ -77,6 +112,26 @@ export default {
},
},

watch: {
// Parse, store and apply new timeout settings.
toastSuccessTimeoutSecs(newTimeout) {
const parsedTimeout = parseInt(newTimeout);
if (parsedTimeout != null && !Number.isNaN(parsedTimeout)) {
localStorage.toastSuccessTimeout = newTimeout > 0 ? newTimeout * 1000 : newTimeout;
}
},
toastErrorTimeoutSecs(newTimeout) {
const parsedTimeout = parseInt(newTimeout);
if (parsedTimeout != null && !Number.isNaN(parsedTimeout)) {
localStorage.toastErrorTimeout = newTimeout > 0 ? newTimeout * 1000 : newTimeout;
}
}
},

mounted() {
this.loadToastTimeoutSettings();
},

methods: {
/**
* Remove a day from expiry notification days.
Expand Down Expand Up @@ -108,6 +163,27 @@ export default {
}
}
},

/**
* Loads toast timeout settings from storage to component data.
*/
loadToastTimeoutSettings() {
const successTimeout = localStorage.toastSuccessTimeout;
if (successTimeout !== undefined) {
const parsedTimeout = parseInt(successTimeout);
if (parsedTimeout != null && !Number.isNaN(parsedTimeout)) {
this.toastSuccessTimeoutSecs = parsedTimeout > 0 ? parsedTimeout / 1000 : parsedTimeout;
}
}

const errorTimeout = localStorage.toastErrorTimeout;
if (errorTimeout !== undefined) {
const parsedTimeout = parseInt(errorTimeout);
if (parsedTimeout != null && !Number.isNaN(parsedTimeout)) {
this.toastErrorTimeoutSecs = parsedTimeout > 0 ? parsedTimeout / 1000 : parsedTimeout;
}
}
},
},
};
</script>
Expand Down
4 changes: 4 additions & 0 deletions src/lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -784,6 +784,10 @@
"Badge URL": "Badge URL",
"Group": "Group",
"Monitor Group": "Monitor Group",
"monitorToastMessagesLabel": "Monitor Toast notifications",
"monitorToastMessagesDescription": "Toast notifications for monitors disappear after given time in seconds. Set to -1 disables timeout. Set to 0 disables toast notifications.",
"toastErrorTimeout": "Timeout for Error Notifications",
"toastSuccessTimeout": "Timeout for Success Notifications",
"Kafka Brokers": "Kafka Brokers",
"Enter the list of brokers": "Enter the list of brokers",
"Press Enter to add broker": "Press Enter to add broker",
Expand Down
59 changes: 57 additions & 2 deletions src/layouts/Layout.vue
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,23 @@
{{ $t("Settings") }}
</router-link>
</nav>

<button
v-if="numActiveToasts != 0"
type="button"
class="btn btn-normal clear-all-toast-btn"
@click="clearToasts"
>
<font-awesome-icon icon="times" />
</button>
</div>
</template>

<script>
import Login from "../components/Login.vue";
import compareVersions from "compare-versions";
import { useToast } from "vue-toastification";
const toast = useToast();

export default {

Expand All @@ -131,7 +142,11 @@ export default {
},

data() {
return {};
return {
toastContainer: null,
numActiveToasts: 0,
toastContainerObserver: null,
};
},

computed: {
Expand Down Expand Up @@ -159,11 +174,33 @@ export default {
},

mounted() {
this.toastContainer = document.querySelector(".bottom-right.toast-container");

// Watch the number of active toasts
this.toastContainerObserver = new MutationObserver((mutations) => {
for (const mutation of mutations) {
if (mutation.type === "childList") {
this.numActiveToasts = mutation.target.children.length;
}
}
});

if (this.toastContainer != null) {
this.toastContainerObserver.observe(this.toastContainer, { childList: true });
}
},

methods: {
beforeUnmount() {
this.toastContainerObserver.disconnect();
},

methods: {
/**
* Clear all toast notifications.
*/
clearToasts() {
toast.clear();
}
},

};
Expand Down Expand Up @@ -323,4 +360,22 @@ main {
background-color: $dark-bg;
}
}

.clear-all-toast-btn {
position: fixed;
right: 1em;
bottom: 1em;
font-size: 1.2em;
padding: 9px 15px;
width: 48px;
box-shadow: 2px 2px 30px rgba(0, 0, 0, 0.2);
}

@media (max-width: 770px) {
.clear-all-toast-btn {
bottom: 72px;
z-index: 100;
}
}

</style>
7 changes: 2 additions & 5 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import dayjs from "dayjs";
import timezone from "./modules/dayjs/plugin/timezone";
import utc from "dayjs/plugin/utc";
import relativeTime from "dayjs/plugin/relativeTime";
import { loadToastSettings } from "./util-frontend";
dayjs.extend(utc);
dayjs.extend(timezone);
dayjs.extend(relativeTime);
Expand All @@ -44,11 +45,7 @@ const app = createApp({
app.use(router);
app.use(i18n);

const options = {
position: "bottom-right",
};

app.use(Toast, options);
app.use(Toast, loadToastSettings());
app.component("Editable", contenteditable);
app.component("FontAwesomeIcon", FontAwesomeIcon);

Expand Down
8 changes: 4 additions & 4 deletions src/mixins/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import jwtDecode from "jwt-decode";
import Favico from "favico.js";
import dayjs from "dayjs";
import { DOWN, MAINTENANCE, PENDING, UP } from "../util.ts";
import { getDevContainerServerHostname, isDevContainer } from "../util-frontend.js";
import { getDevContainerServerHostname, isDevContainer, getToastSuccessTimeout, getToastErrorTimeout } from "../util-frontend.js";
const toast = useToast();

let socket;
Expand Down Expand Up @@ -190,11 +190,11 @@ export default {
if (this.monitorList[data.monitorID] !== undefined) {
if (data.status === 0) {
toast.error(`[${this.monitorList[data.monitorID].name}] [DOWN] ${data.msg}`, {
timeout: false,
timeout: getToastErrorTimeout(),
});
} else if (data.status === 1) {
toast.success(`[${this.monitorList[data.monitorID].name}] [Up] ${data.msg}`, {
timeout: 20000,
timeout: getToastSuccessTimeout(),
});
} else {
toast(`[${this.monitorList[data.monitorID].name}] ${data.msg}`);
Expand Down Expand Up @@ -674,7 +674,7 @@ export default {
*/
getMonitorBeats(monitorID, period, callback) {
socket.emit("getMonitorBeats", monitorID, period, callback);
}
},
},

computed: {
Expand Down
63 changes: 63 additions & 0 deletions src/util-frontend.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import dayjs from "dayjs";
import timezones from "timezones-list";
import { localeDirection, currentLocale } from "./i18n";
import { POSITION } from "vue-toastification";

/**
* Returns the offset from UTC in hours for the current locale.
Expand Down Expand Up @@ -149,3 +150,65 @@ export function colorOptions(self) {
color: "#DB2777" },
];
}

/**
* Loads the toast timeout settings from storage.
* @returns {object} The toast plugin options object.
*/
export function loadToastSettings() {
return {
position: POSITION.BOTTOM_RIGHT,
containerClassName: "toast-container mb-5",
showCloseButtonOnHover: true,

filterBeforeCreate: (toast, toasts) => {
if (toast.timeout === 0) {
return false;
} else {
return toast;
}
},
};
}

/**
* Get timeout for success toasts
* @returns {(number|boolean)} Timeout in ms. If false timeout disabled.
*/
export function getToastSuccessTimeout() {
chakflying marked this conversation as resolved.
Show resolved Hide resolved
let successTimeout = 20000;

if (localStorage.toastSuccessTimeout !== undefined) {
const parsedTimeout = parseInt(localStorage.toastSuccessTimeout);
if (parsedTimeout != null && !Number.isNaN(parsedTimeout)) {
successTimeout = parsedTimeout;
}
}

if (successTimeout === -1) {
successTimeout = false;
}

return successTimeout;
}

/**
* Get timeout for error toasts
* @returns {(number|boolean)} Timeout in ms. If false timeout disabled.
*/
export function getToastErrorTimeout() {
chakflying marked this conversation as resolved.
Show resolved Hide resolved
let errorTimeout = -1;

if (localStorage.toastErrorTimeout !== undefined) {
const parsedTimeout = parseInt(localStorage.toastErrorTimeout);
if (parsedTimeout != null && !Number.isNaN(parsedTimeout)) {
errorTimeout = parsedTimeout;
}
}

if (errorTimeout === -1) {
errorTimeout = false;
}

return errorTimeout;
}
Loading