client-ts: Port services/toast

This commit is contained in:
Elian Doran 2024-07-25 00:26:27 +03:00
parent 78f929ee69
commit cf57819b22
No known key found for this signature in database

View File

@ -1,7 +1,17 @@
import ws from "./ws.js"; import ws from "./ws.js";
import utils from "./utils.js"; import utils from "./utils.js";
function toast(options) { interface ToastOptions {
id?: string;
icon: string;
title: string;
message: string;
delay?: number;
autohide?: boolean;
closeAfter?: number;
}
function toast(options: ToastOptions) {
const $toast = $(`<div class="toast" role="alert" aria-live="assertive" aria-atomic="true"> const $toast = $(`<div class="toast" role="alert" aria-live="assertive" aria-atomic="true">
<div class="toast-header"> <div class="toast-header">
<strong class="mr-auto"><span class="bx bx-${options.icon}"></span> <span class="toast-title"></span></strong> <strong class="mr-auto"><span class="bx bx-${options.icon}"></span> <span class="toast-title"></span></strong>
@ -33,7 +43,7 @@ function toast(options) {
return $toast; return $toast;
} }
function showPersistent(options) { function showPersistent(options: ToastOptions) {
let $toast = $(`#toast-${options.id}`); let $toast = $(`#toast-${options.id}`);
if ($toast.length > 0) { if ($toast.length > 0) {
@ -50,11 +60,11 @@ function showPersistent(options) {
} }
} }
function closePersistent(id) { function closePersistent(id: string) {
$(`#toast-${id}`).remove(); $(`#toast-${id}`).remove();
} }
function showMessage(message, delay = 2000) { function showMessage(message: string, delay = 2000) {
console.debug(utils.now(), "message:", message); console.debug(utils.now(), "message:", message);
toast({ toast({
@ -66,13 +76,13 @@ function showMessage(message, delay = 2000) {
}); });
} }
function showAndLogError(message, delay = 10000) { function showAndLogError(message: string, delay = 10000) {
showError(message, delay); showError(message, delay);
ws.logError(message); ws.logError(message);
} }
function showError(message, delay = 10000) { function showError(message: string, delay = 10000) {
console.log(utils.now(), "error: ", message); console.log(utils.now(), "error: ", message);
toast({ toast({
@ -84,7 +94,7 @@ function showError(message, delay = 10000) {
}); });
} }
function showErrorTitleAndMessage(title, message, delay = 10000) { function showErrorTitleAndMessage(title: string, message: string, delay = 10000) {
console.log(utils.now(), "error: ", message); console.log(utils.now(), "error: ", message);
toast({ toast({
@ -96,7 +106,7 @@ function showErrorTitleAndMessage(title, message, delay = 10000) {
}); });
} }
function throwError(message) { function throwError(message: string) {
ws.logError(message); ws.logError(message);
throw new Error(message); throw new Error(message);