diff --git a/src/gui/src/UI/UIAlert.js b/src/gui/src/UI/UIAlert.js index a795e5669..ce6539382 100644 --- a/src/gui/src/UI/UIAlert.js +++ b/src/gui/src/UI/UIAlert.js @@ -19,31 +19,72 @@ import UIWindow from './UIWindow.js' -function UIAlert(options){ +function UIAlert(options) { // set sensible defaults - if(arguments.length > 0){ + if (arguments.length > 0) { // if first argument is a string, then assume it is the message - if(window.isString(arguments[0])){ + if (window.isString(arguments[0])) { options = {}; options.message = arguments[0]; } // if second argument is an array, then assume it is the buttons - if(arguments[1] && Array.isArray(arguments[1])){ + if (arguments[1] && Array.isArray(arguments[1])) { options.buttons = arguments[1]; } } return new Promise(async (resolve) => { // provide an 'OK' button if no buttons are provided - if(!options.buttons || options.buttons.length === 0){ + if (!options.buttons || options.buttons.length === 0) { options.buttons = [ - {label: i18n('ok'), value: true, type: 'primary'} + { label: i18n('ok'), value: true, type: 'primary' } ] } + // Define alert types + const alertTypes = { + error: { icon: "danger.svg", title: "Error!", color: "#D32F2F" }, + warning: { icon: "warning-sign.svg", title: "Warning!", color: "#FFA000" }, + info: { icon: "reminder.svg", title: "Info", color: "#1976D2" }, + success: { icon: "c-check.svg", title: "Success!", color: "#388E3C" }, + confirm: { icon: "question.svg", title: "Are you sure?", color: "#555555" } + }; - // set body icon - options.body_icon = options.body_icon ?? window.icons['warning-sign.svg']; - if(options.type === 'success') + // Set default values + const alertType = alertTypes[options.type] || alertTypes.warning; + options.message = options.title || alertType.title; + options.body_icon = options.body_icon ?? window.icons[alertType.icon]; + options.color = options.color ?? alertType.color; + + // Define buttons if not provided + if (!options.buttons || options.buttons.length === 0) { + switch (options.type) { + case "confirm": + options.buttons = [ + { label: "Yes", value: true, type: "primary" }, + { label: "No", value: false, type: "secondary" } + ]; + break; + case "error": + options.buttons = [ + { label: "Retry", value: "retry", type: "danger" }, + { label: "Cancel", value: "cancel", type: "secondary" } + ]; + break; + default: + options.buttons = [{ label: i18n('ok'), value: true, type: "primary" }]; + break; + } + } + // callback support with correct resolve handling + options.buttons.forEach(button => { + button.onClick = () => { + if (options.callback) { + options.callback(button.value); + } + puter.ui.closeDialog(); + }; + }); + if (options.type === 'success') options.body_icon = window.icons['c-check.svg']; let santized_message = html_encode(options.message); @@ -62,9 +103,9 @@ function UIAlert(options){ // message h += `
${santized_message}
`; // buttons - if(options.buttons && options.buttons.length > 0){ + if (options.buttons && options.buttons.length > 0) { h += `
`; - for(let y=0; y + + + + + + \ No newline at end of file