useMMAppCommands
Returns functions for all capabilities of Embeddable Zones.
Usage
import { useMMAppCommands } from '@machinemetrics/mm-react-tools';
function MyWidget() {
const {
launchModal, // open a sidesheet modal
toast, // present a toast notification to the user
popup, // mask screen and prompt user on opdash
setSummary, // set tab sub heading in opdash
confirm, // present an alert to the user with options
setLoaded, // informs framework that app/widget/input has loaded
trackEvent, // track an analytics event
} = useMMAppCommands();
const handleAction = async () => {
// Show a toast
toast({
description: 'Operation completed successfully',
status: 'success',
});
// Show a popup
popup('Please review the changes');
// Set a summary
setSummary('Last updated: ' + new Date().toLocaleString());
// Open a sidesheet modal
await launchModal(`${window.location.origin}/machine/123`, {
title: 'Machine Details',
editTitle: 'Edit Machine',
onSave: (data) => {
console.log('Saved:', data);
},
});
// Show a confirmation dialog
const result = await confirm({
header: 'Confirm Action',
body: 'Are you sure you want to proceed?',
choices: [
{ value: 'no', label: 'No' },
{ value: 'yes', label: 'Yes' },
],
});
if (result === 'yes') {
// Handle confirmation
console.log('User confirmed');
}
// Track an analytics event
trackEvent('action_confirmed', { actionType: 'example' });
};
return (
<div>
<button onClick={handleAction}>Perform Action</button>
</div>
);
}
Returns
Returns an object containing:
launchModal: Function to open a sidesheet modaltoast: Function to show a toast notificationpopup: Function to show a popup message (only available in Operator Dashboard)setSummary: Function to set the tab sub heading in opdashconfirm: Function to show a confirmation dialog with optionssetLoaded: Function to inform MachineMetrics that the app, widget, or input has loadedtrackEvent: Function to track an analytics event
launchModal Parameters
Opens a sidesheet modal that slides in from the right side of the screen.
| Parameter | Type | Required | Description |
|---|---|---|---|
path | string | Yes | Fully qualified URL to load in the modal (must include origin) |
options | object | No | Configuration options |
Options:
| Option | Type | Description |
|---|---|---|
title | string | Default title for the modal header |
editTitle | string | Title shown when in edit mode |
editOnly | boolean | Opens directly in edit mode (saves/cancels close the modal) |
onSave | function | Callback receiving data returned from useMMAppModalSave |
pagination | array | Array of { title, url } objects for navigating between items |
toast Parameters
Shows a toast notification in MachineMetrics. Pass an options object with a title or a description (or both). The web app, Operator View and the mmdev playground all read it the same way.
| Option | Type | Default | Description |
|---|---|---|---|
title | string | Bold first line of the toast | |
description | string | Message text | |
status | string | info | info, success, warning, error, or loading. Sets color and icon |
duration | number | null | 5000 | Milliseconds (above 0) before the toast closes. null keeps it open until dismissed |
isClosable | boolean | false | Shows a close button. Always true when duration is null or over a minute |
position | string | host's choice | top, top-left, top-right, bottom, bottom-left, or bottom-right |
id | string | A second toast with the same id from your app is not shown while the first one is still visible |
toast({
title: 'Work orders deleted',
description: '3 work orders were removed.',
status: 'success',
duration: 6000,
isClosable: true,
});
Only these options are read. The host ignores anything else, such as render or variant. In TypeScript, ToastOptions refuses unknown keys in an object literal. A toast with no text compiles, but warns in development and is not shown.
Plain strings are deprecated. toast('Saved') still shows, as a success toast, but editors strike it through and it logs a warning in development. Write toast({ description: 'Saved', status: 'success' }) instead. To fail lint on deprecated calls, enable @typescript-eslint/no-deprecated.
Warnings name the fix. In development, a plain string, an unknown key, a field of the wrong type, or a toast with no text logs one console warning per kind. The host logs the same warnings in its console, naming your app. Users see the toast as usual. A toast with no text is not shown.
Ids belong to your app. Hosts keep your id separate from their own toasts and other apps' toasts. Without an id, a toast with the same text and status as one still showing is not stacked.
Hosts may adapt a few options. Operator View keeps every toast at the bottom center, keeps plain-string toasts on screen for 3 seconds, colours only error (red) and warning (orange), and closes every toast on tap instead of showing a close button. A host without a spinner shows loading as info.
The contract needs @machinemetrics/mm-react-tools 5.5.0 or later for the types and warnings. Hosts read toasts with normalizeToast from @machinemetrics/mm-react-embeddable 2.1.0.
confirm Parameters
| Parameter | Description |
|---|---|
header | The header text for the confirmation dialog |
body | The body text for the confirmation dialog |
choices | Array of objects with value, label, and optional variant |
Choice Object:
| Property | Description |
|---|---|
value | Value returned when this choice is selected |
label | Button label |
variant | Optional button variant (same values as sidesheet header badges in useMMAppInfo) |
trackEvent
Tracks an analytics event. Use this to record how users interact with your embedded app.
Usage data is collected internally by MachineMetrics. Access to your app's analytics is available by request.
const { trackEvent } = useMMAppCommands();
// Track a button click
trackEvent('button_clicked', { buttonId: 'save', section: 'settings' });
// Track a feature usage
trackEvent('chart_rendered', { chartType: 'bar', dataPoints: 42 });
| Parameter | Type | Required | Description |
|---|---|---|---|
eventName | string | Yes | Name of the event to track |
properties | object | No | Additional properties to include |
Your app is automatically identified in the recorded events.