@trackunit/iris-app 2.11.10 → 2.12.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,32 +3,13 @@ name: widget-extensions
3
3
  description: Use when creating dashboard widgets, KPI displays, configurable widgets with edit dialogs, or data visualizations for dashboards.
4
4
  ---
5
5
 
6
- # Widget Extensions
6
+ # Widget extensions
7
7
 
8
- ## Overview
8
+ Widgets are React components on Trackunit Manager dashboards. For full-page extensions, use `FLEET_EXTENSION` or `ASSET_HOME_EXTENSION` instead.
9
9
 
10
- Widget extensions are React components that integrate into Trackunit Manager dashboards. Use `useWidgetConfig` for data, filters, and edit mode. Supports KPI, CHART, LIST, MAP, and OTHER types.
10
+ Create one with the extension command in `AGENTS.md` and `--type=WIDGET_EXTENSION`. It generates `extension-manifest.ts` and `src/app.tsx`, which renders both display and edit mode.
11
11
 
12
- ## When to Use
13
-
14
- - Creating dashboard KPIs or metrics
15
- - Building data visualization widgets
16
- - Adding configurable widgets with edit dialogs
17
- - Displaying filtered/time-ranged data in dashboards
18
-
19
- **Not for:** Full-page extensions (use `FLEET_EXTENSION` or `ASSET_HOME_EXTENSION`).
20
-
21
- ## Creating Widget Extensions
22
-
23
- ```bash
24
- npx nx g @trackunit/iris-app:extend --name=[my-widget-name] --app=[app-name] --directory=[feature] --type=WIDGET_EXTENSION
25
- ```
26
-
27
- This creates:
28
- - `extension-manifest.ts` - Widget configuration and metadata
29
- - `src/app.tsx` - Main widget component (handles both display and edit mode)
30
-
31
- ## Widget Manifest
12
+ ## Manifest
32
13
 
33
14
  ```typescript
34
15
  import { WidgetExtensionManifest } from '@trackunit/iris-app-api';
@@ -37,32 +18,32 @@ const widgetExtensionManifest: WidgetExtensionManifest = {
37
18
  id: "my-widget-id",
38
19
  type: "WIDGET_EXTENSION",
39
20
  sourceRoot: "libs/[feature]/[widget-name]/src",
40
-
41
- widgetType: "CHART", // CHART | KPI | LIST | MAP | OTHER
42
-
21
+
22
+ widgetType: "CHART", // KPI (single metric, typically 1x1), CHART, LIST, MAP, or OTHER
23
+
43
24
  size: {
44
25
  default: { width: 2, height: 2 }, // 1-6 grid units
45
26
  allowFullWidth: false,
46
27
  },
47
-
28
+
48
29
  header: {
49
30
  name: "My Widget",
50
31
  image: { name: "ChartBar" }, // IconByName or IconByPath
51
32
  hasEditDialog: true,
52
33
  },
53
-
34
+
54
35
  footer: {
55
36
  linkDescription: "View details",
56
37
  link: "/my-app/details",
57
38
  poweredByImage: { path: "/assets/logo.svg" },
58
39
  },
59
-
40
+
60
41
  preview: {
61
42
  description: "Displays chart data for analysis",
62
43
  },
63
-
64
- supportedLocations: ["MY_HOME", "PLAYGROUND"],
65
-
44
+
45
+ supportedLocations: ["MY_HOME", "PLAYGROUND"], // also SITE_HOME
46
+
66
47
  supportedFilters: {
67
48
  TIME_RANGE: { include: ["ALL"] },
68
49
  ASSETS: { include: ["assetType", "brands", "models"] },
@@ -72,174 +53,66 @@ const widgetExtensionManifest: WidgetExtensionManifest = {
72
53
  };
73
54
  ```
74
55
 
75
- ## Widget Types
76
-
77
- | Type | Use Case |
78
- |------|----------|
79
- | `KPI` | Single metric displays (typically 1x1) |
80
- | `CHART` | Data visualization (charts, graphs) |
81
- | `LIST` | Tabular or list-based data |
82
- | `MAP` | Geographic or spatial data |
83
- | `OTHER` | Custom or specialized widgets |
56
+ ## useWidgetConfig
84
57
 
85
- ## Supported Locations
58
+ `useWidgetConfig` from `@trackunit/react-core-hooks` provides:
86
59
 
87
- - `MY_HOME` - User's personal dashboard
88
- - `SITE_HOME` - Site-specific dashboards
89
- - `PLAYGROUND` - Widget testing playground
60
+ - `data` (`Record<string, unknown> | null`), `setData(data, version)`, `dataVersion`, and `loadingData`. Save configuration with `setData()`, and increment `dataVersion` when the data shape changes.
61
+ - `title` and `setTitle(title)`.
62
+ - `filters` and `timeRange` (`startMsInEpoch`, `endMsInEpoch`). Apply them to your queries. Filter values include `filters.assetsFilterBarValues?.assetIds`, `filters.assetsFilterBarValues?.assetType`, `filters.customersFilterBarValues?.customerType`, and `filters.sitesFilterBarValues?.siteType`.
63
+ - `isEditMode`, `openEditMode()`, and `closeEditMode()`. Render the edit dialog when `isEditMode` is true.
64
+ - `pollInterval` and `setLoadingState`. Call `setLoadingState` so the dashboard shows loading indicators.
90
65
 
91
- ## Widget Development
92
-
93
- ### Basic Widget Component
66
+ Keep `src/app.tsx` thin. Put the widget config, queries and loading state in a hook next to it, and let `App` call the hook and render display or edit mode.
94
67
 
95
68
  ```typescript
69
+ // useKpiWidget.ts
70
+ import { useCallback, useEffect } from 'react';
96
71
  import { useWidgetConfig } from '@trackunit/react-core-hooks';
72
+ import { useQuery } from '@trackunit/react-graphql-hooks';
73
+ import { GetKpiDocument } from './generated/graphql-api/graphql';
97
74
 
98
- export const App = () => {
99
- const {
100
- data, setData, dataVersion, loadingData,
101
- title, setTitle,
102
- filters, timeRange,
103
- isEditMode, openEditMode, closeEditMode,
104
- pollInterval, setLoadingState
105
- } = useWidgetConfig();
106
-
107
- if (isEditMode) {
108
- return <EditDialog />;
109
- }
110
-
111
- return (
112
- <WidgetBody>
113
- <h2>{title}</h2>
114
- {/* Widget content */}
115
- </WidgetBody>
116
- );
117
- };
118
- ```
119
-
120
- ### useWidgetConfig Hook
121
-
122
- **Data Management:**
123
- - `data: Record<string, unknown> | null` - Widget configuration data
124
- - `setData(data, version)` - Update widget data
125
- - `dataVersion: number | null` - Current data version for migrations
126
- - `loadingData: boolean` - Whether widget data is loading
127
-
128
- **Title Management:**
129
- - `title: string` - Widget title
130
- - `setTitle(title)` - Update widget title
75
+ export const useKpiWidget = () => {
76
+ const { isEditMode, data, closeEditMode, filters, timeRange, pollInterval, setLoadingState } = useWidgetConfig();
131
77
 
132
- **Context:**
133
- - `filters` - Applied filters (assets, customers, sites)
134
- - `timeRange` - Selected time range (startMsInEpoch, endMsInEpoch)
135
- - `isEditMode: boolean` - Whether widget is in edit mode
78
+ const { data: kpiData, loading } = useQuery(GetKpiDocument, {
79
+ variables: {
80
+ filters: {
81
+ assetIds: filters.assetsFilterBarValues?.assetIds,
82
+ timeRange: { start: timeRange?.startMsInEpoch, end: timeRange?.endMsInEpoch },
83
+ },
84
+ },
85
+ pollInterval,
86
+ });
136
87
 
137
- **Edit Mode:**
138
- - `openEditMode()` - Enter edit mode
139
- - `closeEditMode()` - Exit edit mode
88
+ useEffect(() => {
89
+ void setLoadingState({ hasData: !!kpiData, isLoading: loading });
90
+ }, [kpiData, loading, setLoadingState]);
140
91
 
141
- ### Widget with Edit Dialog
92
+ const saveConfig = useCallback(async () => {
93
+ await closeEditMode({ newData: { data: { demo: "yourDataHere" }, dataVersion: 1 } });
94
+ }, [closeEditMode]);
142
95
 
143
- ```typescript
144
- import { useWidgetConfig } from '@trackunit/react-core-hooks';
145
- import { WidgetEditBody } from '@trackunit/react-widgets';
96
+ const cancelEdit = useCallback(() => void closeEditMode(), [closeEditMode]);
146
97
 
147
- export const App = () => {
148
- const { isEditMode, data, closeEditMode } = useWidgetConfig();
149
-
150
- if (isEditMode) {
151
- return (
152
- <WidgetEditBody
153
- onSave={async () => {
154
- await closeEditMode({ newData: { data: { demo: "yourDataHere"}, dataVersion: 1 }});
155
- }}
156
- onCancel={closeEditMode}
157
- initialData={data}
158
- />
159
- );
160
- }
161
-
162
- return <MainWidgetContent />;
98
+ return { isEditMode, data, totalCount: kpiData?.totalCount, saveConfig, cancelEdit };
163
99
  };
164
100
  ```
165
101
 
166
- ## Filter Support
167
-
168
- ### Using Filters
169
-
170
- ```typescript
171
- const { filters, timeRange } = useWidgetConfig();
172
-
173
- const selectedAssets = filters.assetsFilterBarValues?.assetType;
174
- const selectedCustomers = filters.customersFilterBarValues?.customerType;
175
- const selectedSites = filters.sitesFilterBarValues?.siteType;
176
-
177
- const startTime = timeRange?.startMsInEpoch;
178
- const endTime = timeRange?.endMsInEpoch;
179
- ```
180
-
181
- ## KPI Widget Example
182
-
183
102
  ```typescript
184
- import { useWidgetConfig } from '@trackunit/react-core-hooks';
185
- import { useQuery } from '@trackunit/react-graphql-hooks';
103
+ // app.tsx
104
+ import { WidgetEditBody } from '@trackunit/react-widgets';
105
+ import { useKpiWidget } from './useKpiWidget';
186
106
 
187
107
  export const App = () => {
188
- const { filters, timeRange, pollInterval, setLoadingState } = useWidgetConfig();
189
-
190
- const { data: kpiData, loading } = useQuery(MY_KPI_QUERY, {
191
- variables: {
192
- filters: {
193
- assetIds: filters.assetsFilterBarValues?.assetIds,
194
- timeRange: {
195
- start: timeRange?.startMsInEpoch,
196
- end: timeRange?.endMsInEpoch,
197
- },
198
- },
199
- },
200
- pollInterval
201
- });
108
+ const { isEditMode, data, totalCount, saveConfig, cancelEdit } = useKpiWidget();
202
109
 
203
- useEffect(() => {
204
- void setLoadingState({ hasData: !!kpiData, isLoading: loading });
205
- }, [kpiData, loading, setLoadingState]);
206
-
207
- return (
208
- <div className="kpi-widget">
209
- <div className="kpi-value">{kpiData?.totalCount}</div>
210
- <div className="kpi-label">Total Assets</div>
211
- </div>
110
+ return isEditMode ? (
111
+ <WidgetEditBody initialData={data} onCancel={cancelEdit} onSave={saveConfig} />
112
+ ) : (
113
+ <div>{totalCount}</div>
212
114
  );
213
115
  };
214
116
  ```
215
117
 
216
- ## Best Practices
217
-
218
- ### Widget Design
219
- 1. **Responsive Layout** - Design for different grid sizes
220
- 2. **Loading States** - Show loading indicators for async operations
221
- 3. **Error Handling** - Gracefully handle data loading errors
222
- 4. **Empty States** - Provide meaningful empty state messages
223
-
224
- ### Data Management
225
- 1. **Use Real Data** - Always fetch from Trackunit GraphQL API
226
- 2. **Respect Filters** - Apply user-selected filters to data queries
227
- 3. **Efficient Updates** - Only update widget data when necessary
228
- 4. **Version Control** - Use `dataVersion` for configuration migrations
229
-
230
- ### Configuration
231
- 1. **Meaningful Defaults** - Provide sensible default configurations
232
- 2. **Validation** - Validate user input in edit dialogs
233
- 3. **Persistence** - Use `setData()` to save configuration changes
234
- 4. **User Experience** - Keep configuration UI simple and intuitive
235
-
236
- ## Common Mistakes
237
-
238
- | Mistake | Fix |
239
- |---------|-----|
240
- | Using generic widget libraries | Use Trackunit IrisX App SDK patterns |
241
- | Not handling `isEditMode` | Check `isEditMode` and render edit dialog |
242
- | Missing `setLoadingState` call | Call `setLoadingState` to show loading indicators |
243
- | Ignoring `filters` from context | Apply filters to data queries |
244
- | Not using `dataVersion` | Increment version when changing data schema |
245
- | Missing empty/error states | Handle no data and error conditions gracefully |
118
+ Fetch real data from the Trackunit GraphQL API. Design for different grid sizes, handle empty and error states, provide sensible default configuration, and validate input in the edit dialog. Call `setData()` only when the configuration actually changes.