Skip to content
Open
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
24 changes: 17 additions & 7 deletions src/gui/src/IPC.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,14 +160,24 @@ const ipc_listener = async (event, handled) => {
// ALERT
//--------------------------------------------------------
else if(event.data.msg === 'ALERT' && event.data.message !== undefined){
if (event.data.message === undefined || event.data.message === null) {
console.error('Alert message is undefined or null', event.data);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: we can remove this log as doesn't bring much value here.

event.data.message = 'Alert'; // Provide a default message
}
// Normalize message format - handle both string and object
const msgData = typeof event.data.message === 'string'
? { message: event.data.message }
: event.data.message;
Comment on lines +168 to +170
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This logic is discarding some information. If event.data.message is an object, does it mean it will contain the properties below? Because in line 173-176 we referring to those properties from msgData.

Now, if event.data.message === 'string', then the values from line 174 to 176 will be undefined.

{
message,
buttons,
type,
customUI
}


const alert_resp = await UIAlert({
message: event.data.message,
buttons: event.data.buttons,
type: event.data.options?.type,
window_options: {
parent_uuid: event.data.appInstanceID,
disable_parent_window: true,
}
message: msgData.message,
buttons: msgData.buttons,
type: msgData.type,
customUI: msgData.customUI,
window_options: {
parent_uuid: event.data.appInstanceID,
disable_parent_window: true,
}
})

target_iframe.contentWindow.postMessage({
Expand Down
84 changes: 70 additions & 14 deletions src/gui/src/UI/UIAlert.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,33 +34,89 @@ function UIAlert(options){
}

return new Promise(async (resolve) => {
// provide an 'OK' button if no buttons are provided
// Provide type-specific default buttons if no buttons are provided
if(!options.buttons || options.buttons.length === 0){
options.buttons = [
{label: i18n('ok'), value: true, type: 'primary'}
]
switch (options.type) {
case 'question':
options.buttons = [
{ label: i18n('yes'), value: 'yes', type: 'primary' },
{ label: i18n('no'), value: 'no', type: 'default' }
];
break;
case 'warning':
options.buttons = [
{ label: i18n('ok'), value: true, type: 'primary' },
{ label: i18n('cancel'), value: false, type: 'default' }
];
break;
case 'error':
case 'info':
case 'success':
default:
options.buttons = [
{ label: i18n('ok'), value: true, type: 'primary' }
];
break;
}
}

// Convert string array to button objects
if (options.buttons && options.buttons.length > 0 &&
typeof options.buttons[0] === 'string') {
options.buttons = options.buttons.map((label, index) => ({
label: label,
value: label,
type: index === 0 ? 'primary' : 'default'
}));
Comment on lines +64 to +70
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This logic is inconsistent because it is assuming that all elements in the array are of type string.

Is there a case where options.buttons contain both string and non-string values? if so how do we handle the array mapping?

}

// set body icon
options.body_icon = options.body_icon ?? window.icons['warning-sign.svg'];
if(options.type === 'success')
options.body_icon = window.icons['c-check.svg'];
// Icon mapping for all alert types
const iconMap = {
'warning': window.icons['warning-sign.svg'],
'success': window.icons['c-check.svg'],
'error': window.icons['danger.svg'],
'info': window.icons['reminder.svg'],
'question': window.icons['reminder.svg'],
};

let santized_message = html_encode(options.message);
// Set body icon based on type, or use custom override
options.body_icon = options.body_icon ??
(options.type ? iconMap[options.type] : iconMap['warning']);
let message = options.message;
if (typeof message !== 'string') {
message = message != null ? String(message) : '';
}

let sanitized_message = html_encode(message);

// replace sanitized <strong> with <strong>
santized_message = santized_message.replace(/&lt;strong&gt;/g, '<strong>');
santized_message = santized_message.replace(/&lt;\/strong&gt;/g, '</strong>');
sanitized_message = sanitized_message.replace(/&lt;strong&gt;/g, '<strong>');
sanitized_message = sanitized_message.replace(/&lt;\/strong&gt;/g, '</strong>');

// replace sanitized <p> with <p>
santized_message = santized_message.replace(/&lt;p&gt;/g, '<p>');
santized_message = santized_message.replace(/&lt;\/p&gt;/g, '</p>');
sanitized_message = sanitized_message.replace(/&lt;p&gt;/g, '<p>');
sanitized_message = sanitized_message.replace(/&lt;\/p&gt;/g, '</p>');

let h = '';
// icon
h += `<img class="window-alert-icon" src="${html_encode(options.body_icon)}">`;
// message
h += `<div class="window-alert-message">${santized_message}</div>`;
h += `<div class="window-alert-message">${sanitized_message}</div>`;

// Custom UI content (if provided)
if (options.customUI) {
let sanitized_custom = html_encode(options.customUI);
// Allow safe tags
sanitized_custom = sanitized_custom.replace(/&lt;strong&gt;/g, '<strong>');
sanitized_custom = sanitized_custom.replace(/&lt;\/strong&gt;/g, '</strong>');
sanitized_custom = sanitized_custom.replace(/&lt;p&gt;/g, '<p>');
sanitized_custom = sanitized_custom.replace(/&lt;\/p&gt;/g, '</p>');
sanitized_custom = sanitized_custom.replace(/&lt;br&gt;/g, '<br>');
sanitized_custom = sanitized_custom.replace(/&lt;br\/&gt;/g, '<br/>');

h += `<div class="window-alert-custom" style="margin-top: 15px;">${sanitized_custom}</div>`;
}

// buttons
if(options.buttons && options.buttons.length > 0){
h += `<div style="overflow:hidden; margin-top:20px;">`;
Expand Down
33 changes: 32 additions & 1 deletion src/puter-js/src/modules/UI.js
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,38 @@ class UI extends EventListener {

alert = function(message, buttons, options, callback) {
return new Promise((resolve) => {
this.#postMessageWithCallback('ALERT', resolve, { message, buttons, options });
let messagePayload;

// Support object-based API: alert({ type, message, buttons, ... })
if (typeof message === 'object' && message !== null && !Array.isArray(message)) {
// New API - first argument is an options object
messagePayload = {
message: message.message,
type: message.type,
buttons: message.buttons,
customUI: message.customUI,
body_icon: message.body_icon,
backdrop: message.backdrop,
stay_on_top: message.stay_on_top,
draggable_body: message.draggable_body,
window_options: message.window_options
};
} else {
// Legacy API: alert(message, buttons, options)
messagePayload = {
message: message,
buttons: buttons,
type: options?.type,
customUI: options?.customUI,
body_icon: options?.body_icon,
backdrop: options?.backdrop,
stay_on_top: options?.stay_on_top,
draggable_body: options?.draggable_body,
window_options: options?.window_options
};
}

this.#postMessageWithCallback('ALERT', resolve, { message: messagePayload });
})
}

Expand Down