@trackunit/react-core-hooks 1.15.26 → 1.15.28
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.
- package/index.cjs.js +136 -30
- package/index.esm.js +136 -30
- package/package.json +4 -4
- package/src/confirmationDialog/useConfirmationDialog.d.ts +42 -1
- package/src/environment/useEnvironment.d.ts +8 -3
- package/src/navigation/useHasAccessTo.d.ts +10 -0
- package/src/navigation/useNavigateInHost.d.ts +16 -2
- package/src/toast/useToast.d.ts +7 -9
- package/src/token/useToken.d.ts +7 -2
- package/src/user/useCurrentUser.d.ts +12 -3
- package/src/user/useUserPermission.d.ts +24 -1
- package/src/widgetConfig/useWidgetConfig.d.ts +10 -9
package/index.cjs.js
CHANGED
|
@@ -91,7 +91,48 @@ const useAssetSorting = () => {
|
|
|
91
91
|
};
|
|
92
92
|
|
|
93
93
|
/**
|
|
94
|
-
*
|
|
94
|
+
* Shows a modal confirmation dialog and returns the user's choice as a Promise.
|
|
95
|
+
*
|
|
96
|
+
* Resolves to `"PRIMARY"`, `"SECONDARY"`, or `"CLOSE"` depending on which button
|
|
97
|
+
* (or dismiss action) the user selects.
|
|
98
|
+
*
|
|
99
|
+
* **When to use:**
|
|
100
|
+
* - Use `useConfirmationDialog` for destructive or irreversible actions (delete, discard
|
|
101
|
+
* changes, revoke access) where you need explicit user consent before proceeding.
|
|
102
|
+
* - Use `useToast` instead when you only need to *inform* the user — toasts are
|
|
103
|
+
* non-blocking and don't require a decision.
|
|
104
|
+
* - Do **not** build custom confirmation modals — this hook connects to the host-provided
|
|
105
|
+
* dialog so the UX is consistent across the platform.
|
|
106
|
+
*
|
|
107
|
+
* @example
|
|
108
|
+
* ```tsx
|
|
109
|
+
* import { Button } from "@trackunit/react-components";
|
|
110
|
+
* import { useConfirmationDialog } from "@trackunit/react-core-hooks";
|
|
111
|
+
*
|
|
112
|
+
* const DeleteButton = ({ onDelete }: { onDelete: () => void }) => {
|
|
113
|
+
* const { confirm } = useConfirmationDialog();
|
|
114
|
+
*
|
|
115
|
+
* return (
|
|
116
|
+
* <Button
|
|
117
|
+
* onClick={async () => {
|
|
118
|
+
* const response = await confirm({
|
|
119
|
+
* title: "Delete Item",
|
|
120
|
+
* message: "Are you sure? This action cannot be undone.",
|
|
121
|
+
* primaryActionLabel: "Delete",
|
|
122
|
+
* secondaryActionLabel: "Cancel",
|
|
123
|
+
* primaryActionType: "primary-danger",
|
|
124
|
+
* });
|
|
125
|
+
*
|
|
126
|
+
* if (response === "PRIMARY") {
|
|
127
|
+
* onDelete();
|
|
128
|
+
* }
|
|
129
|
+
* }}
|
|
130
|
+
* >
|
|
131
|
+
* Delete
|
|
132
|
+
* </Button>
|
|
133
|
+
* );
|
|
134
|
+
* };
|
|
135
|
+
* ```
|
|
95
136
|
*/
|
|
96
137
|
const useConfirmationDialog = () => {
|
|
97
138
|
const confirmationDialogContext = react.useContext(reactCoreContextsApi.ConfirmationDialogContext);
|
|
@@ -102,14 +143,19 @@ const useConfirmationDialog = () => {
|
|
|
102
143
|
};
|
|
103
144
|
|
|
104
145
|
/**
|
|
105
|
-
*
|
|
146
|
+
* Returns the host environment configuration (API keys, service URLs, build metadata).
|
|
147
|
+
*
|
|
148
|
+
* **When to use:**
|
|
149
|
+
* - Use `useEnvironment` to access platform-level configuration such as the Google Maps
|
|
150
|
+
* API key, GraphQL endpoint URLs, auth issuer, or build version.
|
|
151
|
+
* - Use `useWidgetConfig` instead for per-widget configuration inside dashboards.
|
|
152
|
+
* - Do **not** hardcode service URLs or API keys — always read them from this hook.
|
|
106
153
|
*
|
|
107
154
|
* @requires EnvironmentContext
|
|
108
155
|
* @example
|
|
109
156
|
* import { useEnvironment } from "@trackunit/react-core-hooks";
|
|
110
157
|
* const { googleMapsApiKey } = useEnvironment();
|
|
111
|
-
*
|
|
112
|
-
* @see (@link EnvironmentState)
|
|
158
|
+
* @see EnvironmentState
|
|
113
159
|
*/
|
|
114
160
|
const useEnvironment = () => {
|
|
115
161
|
const context = react.useContext(reactCoreContextsApi.EnvironmentContext);
|
|
@@ -325,9 +371,14 @@ const useGeolocation = (options) => {
|
|
|
325
371
|
};
|
|
326
372
|
|
|
327
373
|
/**
|
|
328
|
-
*
|
|
374
|
+
* Returns the current authentication token from the TokenContext.
|
|
375
|
+
*
|
|
376
|
+
* **When to use:**
|
|
377
|
+
* - Use `useToken` when you need the raw JWT bearer token for authenticated API calls
|
|
378
|
+
* outside the GraphQL layer (e.g. REST endpoints, file uploads, WebSockets).
|
|
379
|
+
* - Use `useCurrentUser` instead when you need the user's identity or account info.
|
|
380
|
+
* - Do **not** use for GraphQL requests — the Apollo client already attaches the token.
|
|
329
381
|
*
|
|
330
|
-
* @requires TokenProvider
|
|
331
382
|
* @example
|
|
332
383
|
* import { useToken } from "@trackunit/react-core-hooks";
|
|
333
384
|
* const { token } = useToken();
|
|
@@ -505,6 +556,16 @@ const accessReducer = (_state, action) => {
|
|
|
505
556
|
* Hook to check if the current user has access to a navigation target.
|
|
506
557
|
* Useful for conditionally rendering links based on user permissions.
|
|
507
558
|
*
|
|
559
|
+
* Supports single checks, `oneOf` (any match), and `requireAll` (every match) criteria.
|
|
560
|
+
*
|
|
561
|
+
* **When to use:**
|
|
562
|
+
* - Use `useHasAccessTo` to conditionally show/hide navigation links or action buttons
|
|
563
|
+
* based on whether the user can reach the target page or entity.
|
|
564
|
+
* - Use `useUserPermission` instead when you need a synchronous check against a
|
|
565
|
+
* permission string on the user object rather than a navigation-target check.
|
|
566
|
+
* - Use the `hasAccessTo` method from `useNavigateInHost` for a one-off imperative
|
|
567
|
+
* check (e.g. inside an event handler) rather than a reactive/rendered result.
|
|
568
|
+
*
|
|
508
569
|
* @requires NavigationContext
|
|
509
570
|
* @example
|
|
510
571
|
* import { useHasAccessTo } from "@trackunit/react-core-hooks";
|
|
@@ -553,12 +614,26 @@ const useHasAccessTo = (options) => {
|
|
|
553
614
|
};
|
|
554
615
|
|
|
555
616
|
/**
|
|
556
|
-
*
|
|
617
|
+
* Hook to navigate programmatically within the Trackunit Manager or between Iris Apps.
|
|
618
|
+
*
|
|
619
|
+
* ### When to use
|
|
620
|
+
* Use `useNavigateInHost` when you need to navigate between Manager pages, entity home
|
|
621
|
+
* pages, or Iris Apps from component logic (e.g. after a mutation, on row click, in a
|
|
622
|
+
* redirect guard). Use the `get*Url` methods when you need an href for an `<a>` tag.
|
|
623
|
+
* Use `hasAccessTo` before showing navigation options the user may not have access to.
|
|
624
|
+
*
|
|
625
|
+
* ### When not to use
|
|
626
|
+
* Do not use for in-page anchor scrolling or query-param changes within the same
|
|
627
|
+
* Iris App — use your router directly.
|
|
557
628
|
*
|
|
558
629
|
* @requires NavigationContext
|
|
559
630
|
* @example
|
|
560
631
|
* import { useNavigateInHost } from "@trackunit/react-core-hooks";
|
|
561
|
-
*
|
|
632
|
+
* const { gotoAssetHome, gotoFleetApp } = useNavigateInHost();
|
|
633
|
+
*
|
|
634
|
+
* gotoAssetHome("abc-123", { page: "status" });
|
|
635
|
+
* gotoFleetApp({ page: "assets" });
|
|
636
|
+
* @see {@link NavigationRuntimeApi}
|
|
562
637
|
*/
|
|
563
638
|
const useNavigateInHost = () => {
|
|
564
639
|
const context = react.useContext(reactCoreContextsApi.NavigationContext);
|
|
@@ -863,20 +938,18 @@ const useTimeRange = () => {
|
|
|
863
938
|
};
|
|
864
939
|
|
|
865
940
|
/**
|
|
866
|
-
*
|
|
941
|
+
* Hook to show toast notifications via the ToastContext.
|
|
867
942
|
*
|
|
868
943
|
* @requires ToastProvider
|
|
869
944
|
* @example
|
|
870
945
|
* import { useToast } from "@trackunit/react-core-hooks";
|
|
871
946
|
* const { addToast } = useToast();
|
|
872
|
-
*
|
|
873
|
-
*
|
|
874
|
-
*
|
|
875
|
-
*
|
|
876
|
-
*
|
|
877
|
-
*
|
|
878
|
-
* duration: 3000,
|
|
879
|
-
* });
|
|
947
|
+
*
|
|
948
|
+
* addToast({
|
|
949
|
+
* intent: "success",
|
|
950
|
+
* title: "Asset deleted",
|
|
951
|
+
* id: "deletion-status",
|
|
952
|
+
* });
|
|
880
953
|
* @see { ToastContextValue }
|
|
881
954
|
*/
|
|
882
955
|
const useToast = () => {
|
|
@@ -888,14 +961,23 @@ const useToast = () => {
|
|
|
888
961
|
};
|
|
889
962
|
|
|
890
963
|
/**
|
|
891
|
-
*
|
|
964
|
+
* Hook providing the authenticated user's profile, account, and permission data
|
|
965
|
+
* from the CurrentUserContext.
|
|
966
|
+
*
|
|
967
|
+
* ### When to use
|
|
968
|
+
* Use `useCurrentUser` when you need the user's identity or account context — displaying
|
|
969
|
+
* the user's name, resolving the effective account ID, or branching logic based on
|
|
970
|
+
* `isAssuming` or `isTrackunitUser`.
|
|
971
|
+
*
|
|
972
|
+
* ### When not to use
|
|
973
|
+
* - If you only need a boolean check for a single permission — use `useUserPermission`.
|
|
974
|
+
* - To gate entire routes — prefer server-side access control or `hasAccessTo` from
|
|
975
|
+
* `useNavigateInHost`.
|
|
892
976
|
*
|
|
893
|
-
* @requires CurrentUserProvider
|
|
894
977
|
* @example
|
|
895
978
|
* import { useCurrentUser } from "@trackunit/react-core-hooks";
|
|
896
979
|
* const { assumedUser, accountId } = useCurrentUser();
|
|
897
980
|
*
|
|
898
|
-
* //use it for something
|
|
899
981
|
* const { data, loading } = useGetAccountByIdQuery({
|
|
900
982
|
* variables: { accountId: assumedUser?.accountId || accountId || "" },
|
|
901
983
|
* });
|
|
@@ -1041,8 +1123,31 @@ const setGlobalLanguage = (language) => {
|
|
|
1041
1123
|
/**
|
|
1042
1124
|
* Hook that checks if the current user has a given permission.
|
|
1043
1125
|
*
|
|
1126
|
+
* ### When to use
|
|
1127
|
+
* Use `useUserPermission` to conditionally show or hide UI based on a single permission —
|
|
1128
|
+
* delete buttons, admin panels, export actions, or any feature behind an access check.
|
|
1129
|
+
*
|
|
1130
|
+
* ### When not to use
|
|
1131
|
+
* - When you need multiple user fields (name, account, assuming state) or need to check
|
|
1132
|
+
* several permissions at once — use `useCurrentUser`.
|
|
1133
|
+
* - To gate navigation targets — use `hasAccessTo` from `useNavigateInHost`.
|
|
1134
|
+
*
|
|
1044
1135
|
* @param requiredPermission - The permission to check for typesafe string.
|
|
1045
|
-
* @returns {
|
|
1136
|
+
* @returns {boolean} `true` if the user has the permission, `false` otherwise (including while permissions are still loading).
|
|
1137
|
+
* @example
|
|
1138
|
+
* ```tsx
|
|
1139
|
+
* import { useUserPermission } from "@trackunit/react-core-hooks";
|
|
1140
|
+
*
|
|
1141
|
+
* const DeleteButton = ({ assetId }: { assetId: string }) => {
|
|
1142
|
+
* const canDelete = useUserPermission("asset:delete");
|
|
1143
|
+
*
|
|
1144
|
+
* if (!canDelete) {
|
|
1145
|
+
* return null;
|
|
1146
|
+
* }
|
|
1147
|
+
*
|
|
1148
|
+
* return <Button onClick={() => deleteAsset(assetId)}>Delete</Button>;
|
|
1149
|
+
* };
|
|
1150
|
+
* ```
|
|
1046
1151
|
*/
|
|
1047
1152
|
const useUserPermission = (requiredPermission) => {
|
|
1048
1153
|
const { permissions } = useCurrentUser();
|
|
@@ -1150,7 +1255,6 @@ const useSettingsUtils = () => {
|
|
|
1150
1255
|
/**
|
|
1151
1256
|
* This is a hook to use the WidgetConfigProvider.
|
|
1152
1257
|
*
|
|
1153
|
-
* @requires WidgetConfigProvider
|
|
1154
1258
|
* @example
|
|
1155
1259
|
* import { useWidgetConfigAsync } from "@trackunit/react-core-hooks";
|
|
1156
1260
|
*
|
|
@@ -1191,17 +1295,19 @@ const INITIAL_WIDGET_DATA_STATE = {
|
|
|
1191
1295
|
title: null,
|
|
1192
1296
|
};
|
|
1193
1297
|
/**
|
|
1194
|
-
*
|
|
1298
|
+
* Manages a dashboard widget's configuration, data, title, edit mode, filters, and time range.
|
|
1299
|
+
*
|
|
1300
|
+
* **When to use:**
|
|
1301
|
+
* - Use `useWidgetConfig` inside any dashboard widget to read/write its persisted
|
|
1302
|
+
* configuration, respond to dashboard-level filter and time-range changes, and
|
|
1303
|
+
* manage the widget's edit-mode lifecycle.
|
|
1304
|
+
* - Must be rendered inside a `WidgetConfigProvider` (provided automatically by the
|
|
1305
|
+
* dashboard host).
|
|
1195
1306
|
*
|
|
1196
1307
|
* @example
|
|
1197
1308
|
* import { useWidgetConfig } from "@trackunit/react-core-hooks";
|
|
1198
|
-
*
|
|
1199
|
-
*
|
|
1200
|
-
* const { ... } = useWidgetConfig();
|
|
1201
|
-
*
|
|
1202
|
-
* ...
|
|
1203
|
-
* };
|
|
1204
|
-
* @see { WidgetConfigContext}
|
|
1309
|
+
* const { data, setData, isEditMode, filters, timeRange } = useWidgetConfig();
|
|
1310
|
+
* @see {WidgetConfigContext}
|
|
1205
1311
|
*/
|
|
1206
1312
|
const useWidgetConfig = () => {
|
|
1207
1313
|
const widgetConfigContext = useWidgetConfigAsync();
|
package/index.esm.js
CHANGED
|
@@ -89,7 +89,48 @@ const useAssetSorting = () => {
|
|
|
89
89
|
};
|
|
90
90
|
|
|
91
91
|
/**
|
|
92
|
-
*
|
|
92
|
+
* Shows a modal confirmation dialog and returns the user's choice as a Promise.
|
|
93
|
+
*
|
|
94
|
+
* Resolves to `"PRIMARY"`, `"SECONDARY"`, or `"CLOSE"` depending on which button
|
|
95
|
+
* (or dismiss action) the user selects.
|
|
96
|
+
*
|
|
97
|
+
* **When to use:**
|
|
98
|
+
* - Use `useConfirmationDialog` for destructive or irreversible actions (delete, discard
|
|
99
|
+
* changes, revoke access) where you need explicit user consent before proceeding.
|
|
100
|
+
* - Use `useToast` instead when you only need to *inform* the user — toasts are
|
|
101
|
+
* non-blocking and don't require a decision.
|
|
102
|
+
* - Do **not** build custom confirmation modals — this hook connects to the host-provided
|
|
103
|
+
* dialog so the UX is consistent across the platform.
|
|
104
|
+
*
|
|
105
|
+
* @example
|
|
106
|
+
* ```tsx
|
|
107
|
+
* import { Button } from "@trackunit/react-components";
|
|
108
|
+
* import { useConfirmationDialog } from "@trackunit/react-core-hooks";
|
|
109
|
+
*
|
|
110
|
+
* const DeleteButton = ({ onDelete }: { onDelete: () => void }) => {
|
|
111
|
+
* const { confirm } = useConfirmationDialog();
|
|
112
|
+
*
|
|
113
|
+
* return (
|
|
114
|
+
* <Button
|
|
115
|
+
* onClick={async () => {
|
|
116
|
+
* const response = await confirm({
|
|
117
|
+
* title: "Delete Item",
|
|
118
|
+
* message: "Are you sure? This action cannot be undone.",
|
|
119
|
+
* primaryActionLabel: "Delete",
|
|
120
|
+
* secondaryActionLabel: "Cancel",
|
|
121
|
+
* primaryActionType: "primary-danger",
|
|
122
|
+
* });
|
|
123
|
+
*
|
|
124
|
+
* if (response === "PRIMARY") {
|
|
125
|
+
* onDelete();
|
|
126
|
+
* }
|
|
127
|
+
* }}
|
|
128
|
+
* >
|
|
129
|
+
* Delete
|
|
130
|
+
* </Button>
|
|
131
|
+
* );
|
|
132
|
+
* };
|
|
133
|
+
* ```
|
|
93
134
|
*/
|
|
94
135
|
const useConfirmationDialog = () => {
|
|
95
136
|
const confirmationDialogContext = useContext(ConfirmationDialogContext);
|
|
@@ -100,14 +141,19 @@ const useConfirmationDialog = () => {
|
|
|
100
141
|
};
|
|
101
142
|
|
|
102
143
|
/**
|
|
103
|
-
*
|
|
144
|
+
* Returns the host environment configuration (API keys, service URLs, build metadata).
|
|
145
|
+
*
|
|
146
|
+
* **When to use:**
|
|
147
|
+
* - Use `useEnvironment` to access platform-level configuration such as the Google Maps
|
|
148
|
+
* API key, GraphQL endpoint URLs, auth issuer, or build version.
|
|
149
|
+
* - Use `useWidgetConfig` instead for per-widget configuration inside dashboards.
|
|
150
|
+
* - Do **not** hardcode service URLs or API keys — always read them from this hook.
|
|
104
151
|
*
|
|
105
152
|
* @requires EnvironmentContext
|
|
106
153
|
* @example
|
|
107
154
|
* import { useEnvironment } from "@trackunit/react-core-hooks";
|
|
108
155
|
* const { googleMapsApiKey } = useEnvironment();
|
|
109
|
-
*
|
|
110
|
-
* @see (@link EnvironmentState)
|
|
156
|
+
* @see EnvironmentState
|
|
111
157
|
*/
|
|
112
158
|
const useEnvironment = () => {
|
|
113
159
|
const context = useContext(EnvironmentContext);
|
|
@@ -323,9 +369,14 @@ const useGeolocation = (options) => {
|
|
|
323
369
|
};
|
|
324
370
|
|
|
325
371
|
/**
|
|
326
|
-
*
|
|
372
|
+
* Returns the current authentication token from the TokenContext.
|
|
373
|
+
*
|
|
374
|
+
* **When to use:**
|
|
375
|
+
* - Use `useToken` when you need the raw JWT bearer token for authenticated API calls
|
|
376
|
+
* outside the GraphQL layer (e.g. REST endpoints, file uploads, WebSockets).
|
|
377
|
+
* - Use `useCurrentUser` instead when you need the user's identity or account info.
|
|
378
|
+
* - Do **not** use for GraphQL requests — the Apollo client already attaches the token.
|
|
327
379
|
*
|
|
328
|
-
* @requires TokenProvider
|
|
329
380
|
* @example
|
|
330
381
|
* import { useToken } from "@trackunit/react-core-hooks";
|
|
331
382
|
* const { token } = useToken();
|
|
@@ -503,6 +554,16 @@ const accessReducer = (_state, action) => {
|
|
|
503
554
|
* Hook to check if the current user has access to a navigation target.
|
|
504
555
|
* Useful for conditionally rendering links based on user permissions.
|
|
505
556
|
*
|
|
557
|
+
* Supports single checks, `oneOf` (any match), and `requireAll` (every match) criteria.
|
|
558
|
+
*
|
|
559
|
+
* **When to use:**
|
|
560
|
+
* - Use `useHasAccessTo` to conditionally show/hide navigation links or action buttons
|
|
561
|
+
* based on whether the user can reach the target page or entity.
|
|
562
|
+
* - Use `useUserPermission` instead when you need a synchronous check against a
|
|
563
|
+
* permission string on the user object rather than a navigation-target check.
|
|
564
|
+
* - Use the `hasAccessTo` method from `useNavigateInHost` for a one-off imperative
|
|
565
|
+
* check (e.g. inside an event handler) rather than a reactive/rendered result.
|
|
566
|
+
*
|
|
506
567
|
* @requires NavigationContext
|
|
507
568
|
* @example
|
|
508
569
|
* import { useHasAccessTo } from "@trackunit/react-core-hooks";
|
|
@@ -551,12 +612,26 @@ const useHasAccessTo = (options) => {
|
|
|
551
612
|
};
|
|
552
613
|
|
|
553
614
|
/**
|
|
554
|
-
*
|
|
615
|
+
* Hook to navigate programmatically within the Trackunit Manager or between Iris Apps.
|
|
616
|
+
*
|
|
617
|
+
* ### When to use
|
|
618
|
+
* Use `useNavigateInHost` when you need to navigate between Manager pages, entity home
|
|
619
|
+
* pages, or Iris Apps from component logic (e.g. after a mutation, on row click, in a
|
|
620
|
+
* redirect guard). Use the `get*Url` methods when you need an href for an `<a>` tag.
|
|
621
|
+
* Use `hasAccessTo` before showing navigation options the user may not have access to.
|
|
622
|
+
*
|
|
623
|
+
* ### When not to use
|
|
624
|
+
* Do not use for in-page anchor scrolling or query-param changes within the same
|
|
625
|
+
* Iris App — use your router directly.
|
|
555
626
|
*
|
|
556
627
|
* @requires NavigationContext
|
|
557
628
|
* @example
|
|
558
629
|
* import { useNavigateInHost } from "@trackunit/react-core-hooks";
|
|
559
|
-
*
|
|
630
|
+
* const { gotoAssetHome, gotoFleetApp } = useNavigateInHost();
|
|
631
|
+
*
|
|
632
|
+
* gotoAssetHome("abc-123", { page: "status" });
|
|
633
|
+
* gotoFleetApp({ page: "assets" });
|
|
634
|
+
* @see {@link NavigationRuntimeApi}
|
|
560
635
|
*/
|
|
561
636
|
const useNavigateInHost = () => {
|
|
562
637
|
const context = useContext(NavigationContext);
|
|
@@ -861,20 +936,18 @@ const useTimeRange = () => {
|
|
|
861
936
|
};
|
|
862
937
|
|
|
863
938
|
/**
|
|
864
|
-
*
|
|
939
|
+
* Hook to show toast notifications via the ToastContext.
|
|
865
940
|
*
|
|
866
941
|
* @requires ToastProvider
|
|
867
942
|
* @example
|
|
868
943
|
* import { useToast } from "@trackunit/react-core-hooks";
|
|
869
944
|
* const { addToast } = useToast();
|
|
870
|
-
*
|
|
871
|
-
*
|
|
872
|
-
*
|
|
873
|
-
*
|
|
874
|
-
*
|
|
875
|
-
*
|
|
876
|
-
* duration: 3000,
|
|
877
|
-
* });
|
|
945
|
+
*
|
|
946
|
+
* addToast({
|
|
947
|
+
* intent: "success",
|
|
948
|
+
* title: "Asset deleted",
|
|
949
|
+
* id: "deletion-status",
|
|
950
|
+
* });
|
|
878
951
|
* @see { ToastContextValue }
|
|
879
952
|
*/
|
|
880
953
|
const useToast = () => {
|
|
@@ -886,14 +959,23 @@ const useToast = () => {
|
|
|
886
959
|
};
|
|
887
960
|
|
|
888
961
|
/**
|
|
889
|
-
*
|
|
962
|
+
* Hook providing the authenticated user's profile, account, and permission data
|
|
963
|
+
* from the CurrentUserContext.
|
|
964
|
+
*
|
|
965
|
+
* ### When to use
|
|
966
|
+
* Use `useCurrentUser` when you need the user's identity or account context — displaying
|
|
967
|
+
* the user's name, resolving the effective account ID, or branching logic based on
|
|
968
|
+
* `isAssuming` or `isTrackunitUser`.
|
|
969
|
+
*
|
|
970
|
+
* ### When not to use
|
|
971
|
+
* - If you only need a boolean check for a single permission — use `useUserPermission`.
|
|
972
|
+
* - To gate entire routes — prefer server-side access control or `hasAccessTo` from
|
|
973
|
+
* `useNavigateInHost`.
|
|
890
974
|
*
|
|
891
|
-
* @requires CurrentUserProvider
|
|
892
975
|
* @example
|
|
893
976
|
* import { useCurrentUser } from "@trackunit/react-core-hooks";
|
|
894
977
|
* const { assumedUser, accountId } = useCurrentUser();
|
|
895
978
|
*
|
|
896
|
-
* //use it for something
|
|
897
979
|
* const { data, loading } = useGetAccountByIdQuery({
|
|
898
980
|
* variables: { accountId: assumedUser?.accountId || accountId || "" },
|
|
899
981
|
* });
|
|
@@ -1039,8 +1121,31 @@ const setGlobalLanguage = (language) => {
|
|
|
1039
1121
|
/**
|
|
1040
1122
|
* Hook that checks if the current user has a given permission.
|
|
1041
1123
|
*
|
|
1124
|
+
* ### When to use
|
|
1125
|
+
* Use `useUserPermission` to conditionally show or hide UI based on a single permission —
|
|
1126
|
+
* delete buttons, admin panels, export actions, or any feature behind an access check.
|
|
1127
|
+
*
|
|
1128
|
+
* ### When not to use
|
|
1129
|
+
* - When you need multiple user fields (name, account, assuming state) or need to check
|
|
1130
|
+
* several permissions at once — use `useCurrentUser`.
|
|
1131
|
+
* - To gate navigation targets — use `hasAccessTo` from `useNavigateInHost`.
|
|
1132
|
+
*
|
|
1042
1133
|
* @param requiredPermission - The permission to check for typesafe string.
|
|
1043
|
-
* @returns {
|
|
1134
|
+
* @returns {boolean} `true` if the user has the permission, `false` otherwise (including while permissions are still loading).
|
|
1135
|
+
* @example
|
|
1136
|
+
* ```tsx
|
|
1137
|
+
* import { useUserPermission } from "@trackunit/react-core-hooks";
|
|
1138
|
+
*
|
|
1139
|
+
* const DeleteButton = ({ assetId }: { assetId: string }) => {
|
|
1140
|
+
* const canDelete = useUserPermission("asset:delete");
|
|
1141
|
+
*
|
|
1142
|
+
* if (!canDelete) {
|
|
1143
|
+
* return null;
|
|
1144
|
+
* }
|
|
1145
|
+
*
|
|
1146
|
+
* return <Button onClick={() => deleteAsset(assetId)}>Delete</Button>;
|
|
1147
|
+
* };
|
|
1148
|
+
* ```
|
|
1044
1149
|
*/
|
|
1045
1150
|
const useUserPermission = (requiredPermission) => {
|
|
1046
1151
|
const { permissions } = useCurrentUser();
|
|
@@ -1148,7 +1253,6 @@ const useSettingsUtils = () => {
|
|
|
1148
1253
|
/**
|
|
1149
1254
|
* This is a hook to use the WidgetConfigProvider.
|
|
1150
1255
|
*
|
|
1151
|
-
* @requires WidgetConfigProvider
|
|
1152
1256
|
* @example
|
|
1153
1257
|
* import { useWidgetConfigAsync } from "@trackunit/react-core-hooks";
|
|
1154
1258
|
*
|
|
@@ -1189,17 +1293,19 @@ const INITIAL_WIDGET_DATA_STATE = {
|
|
|
1189
1293
|
title: null,
|
|
1190
1294
|
};
|
|
1191
1295
|
/**
|
|
1192
|
-
*
|
|
1296
|
+
* Manages a dashboard widget's configuration, data, title, edit mode, filters, and time range.
|
|
1297
|
+
*
|
|
1298
|
+
* **When to use:**
|
|
1299
|
+
* - Use `useWidgetConfig` inside any dashboard widget to read/write its persisted
|
|
1300
|
+
* configuration, respond to dashboard-level filter and time-range changes, and
|
|
1301
|
+
* manage the widget's edit-mode lifecycle.
|
|
1302
|
+
* - Must be rendered inside a `WidgetConfigProvider` (provided automatically by the
|
|
1303
|
+
* dashboard host).
|
|
1193
1304
|
*
|
|
1194
1305
|
* @example
|
|
1195
1306
|
* import { useWidgetConfig } from "@trackunit/react-core-hooks";
|
|
1196
|
-
*
|
|
1197
|
-
*
|
|
1198
|
-
* const { ... } = useWidgetConfig();
|
|
1199
|
-
*
|
|
1200
|
-
* ...
|
|
1201
|
-
* };
|
|
1202
|
-
* @see { WidgetConfigContext}
|
|
1307
|
+
* const { data, setData, isEditMode, filters, timeRange } = useWidgetConfig();
|
|
1308
|
+
* @see {WidgetConfigContext}
|
|
1203
1309
|
*/
|
|
1204
1310
|
const useWidgetConfig = () => {
|
|
1205
1311
|
const widgetConfigContext = useWidgetConfigAsync();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@trackunit/react-core-hooks",
|
|
3
|
-
"version": "1.15.
|
|
3
|
+
"version": "1.15.28",
|
|
4
4
|
"repository": "https://github.com/Trackunit/manager",
|
|
5
5
|
"license": "SEE LICENSE IN LICENSE.txt",
|
|
6
6
|
"engines": {
|
|
@@ -8,9 +8,9 @@
|
|
|
8
8
|
},
|
|
9
9
|
"dependencies": {
|
|
10
10
|
"es-toolkit": "^1.39.10",
|
|
11
|
-
"@trackunit/iris-app-runtime-core": "1.15.
|
|
12
|
-
"@trackunit/iris-app-runtime-core-api": "1.14.
|
|
13
|
-
"@trackunit/react-core-contexts-api": "1.15.
|
|
11
|
+
"@trackunit/iris-app-runtime-core": "1.15.27",
|
|
12
|
+
"@trackunit/iris-app-runtime-core-api": "1.14.27",
|
|
13
|
+
"@trackunit/react-core-contexts-api": "1.15.27",
|
|
14
14
|
"zod": "^3.25.76"
|
|
15
15
|
},
|
|
16
16
|
"peerDependencies": {
|
|
@@ -1,5 +1,46 @@
|
|
|
1
1
|
import { ConfirmationDialogRuntimeApi } from "@trackunit/iris-app-runtime-core-api";
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
3
|
+
* Shows a modal confirmation dialog and returns the user's choice as a Promise.
|
|
4
|
+
*
|
|
5
|
+
* Resolves to `"PRIMARY"`, `"SECONDARY"`, or `"CLOSE"` depending on which button
|
|
6
|
+
* (or dismiss action) the user selects.
|
|
7
|
+
*
|
|
8
|
+
* **When to use:**
|
|
9
|
+
* - Use `useConfirmationDialog` for destructive or irreversible actions (delete, discard
|
|
10
|
+
* changes, revoke access) where you need explicit user consent before proceeding.
|
|
11
|
+
* - Use `useToast` instead when you only need to *inform* the user — toasts are
|
|
12
|
+
* non-blocking and don't require a decision.
|
|
13
|
+
* - Do **not** build custom confirmation modals — this hook connects to the host-provided
|
|
14
|
+
* dialog so the UX is consistent across the platform.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```tsx
|
|
18
|
+
* import { Button } from "@trackunit/react-components";
|
|
19
|
+
* import { useConfirmationDialog } from "@trackunit/react-core-hooks";
|
|
20
|
+
*
|
|
21
|
+
* const DeleteButton = ({ onDelete }: { onDelete: () => void }) => {
|
|
22
|
+
* const { confirm } = useConfirmationDialog();
|
|
23
|
+
*
|
|
24
|
+
* return (
|
|
25
|
+
* <Button
|
|
26
|
+
* onClick={async () => {
|
|
27
|
+
* const response = await confirm({
|
|
28
|
+
* title: "Delete Item",
|
|
29
|
+
* message: "Are you sure? This action cannot be undone.",
|
|
30
|
+
* primaryActionLabel: "Delete",
|
|
31
|
+
* secondaryActionLabel: "Cancel",
|
|
32
|
+
* primaryActionType: "primary-danger",
|
|
33
|
+
* });
|
|
34
|
+
*
|
|
35
|
+
* if (response === "PRIMARY") {
|
|
36
|
+
* onDelete();
|
|
37
|
+
* }
|
|
38
|
+
* }}
|
|
39
|
+
* >
|
|
40
|
+
* Delete
|
|
41
|
+
* </Button>
|
|
42
|
+
* );
|
|
43
|
+
* };
|
|
44
|
+
* ```
|
|
4
45
|
*/
|
|
5
46
|
export declare const useConfirmationDialog: () => ConfirmationDialogRuntimeApi;
|
|
@@ -1,11 +1,16 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* Returns the host environment configuration (API keys, service URLs, build metadata).
|
|
3
|
+
*
|
|
4
|
+
* **When to use:**
|
|
5
|
+
* - Use `useEnvironment` to access platform-level configuration such as the Google Maps
|
|
6
|
+
* API key, GraphQL endpoint URLs, auth issuer, or build version.
|
|
7
|
+
* - Use `useWidgetConfig` instead for per-widget configuration inside dashboards.
|
|
8
|
+
* - Do **not** hardcode service URLs or API keys — always read them from this hook.
|
|
3
9
|
*
|
|
4
10
|
* @requires EnvironmentContext
|
|
5
11
|
* @example
|
|
6
12
|
* import { useEnvironment } from "@trackunit/react-core-hooks";
|
|
7
13
|
* const { googleMapsApiKey } = useEnvironment();
|
|
8
|
-
*
|
|
9
|
-
* @see (@link EnvironmentState)
|
|
14
|
+
* @see EnvironmentState
|
|
10
15
|
*/
|
|
11
16
|
export declare const useEnvironment: () => import("@trackunit/iris-app-runtime-core-api").EnvironmentState;
|
|
@@ -15,6 +15,16 @@ type UseHasAccessToResult = {
|
|
|
15
15
|
* Hook to check if the current user has access to a navigation target.
|
|
16
16
|
* Useful for conditionally rendering links based on user permissions.
|
|
17
17
|
*
|
|
18
|
+
* Supports single checks, `oneOf` (any match), and `requireAll` (every match) criteria.
|
|
19
|
+
*
|
|
20
|
+
* **When to use:**
|
|
21
|
+
* - Use `useHasAccessTo` to conditionally show/hide navigation links or action buttons
|
|
22
|
+
* based on whether the user can reach the target page or entity.
|
|
23
|
+
* - Use `useUserPermission` instead when you need a synchronous check against a
|
|
24
|
+
* permission string on the user object rather than a navigation-target check.
|
|
25
|
+
* - Use the `hasAccessTo` method from `useNavigateInHost` for a one-off imperative
|
|
26
|
+
* check (e.g. inside an event handler) rather than a reactive/rendered result.
|
|
27
|
+
*
|
|
18
28
|
* @requires NavigationContext
|
|
19
29
|
* @example
|
|
20
30
|
* import { useHasAccessTo } from "@trackunit/react-core-hooks";
|
|
@@ -1,10 +1,24 @@
|
|
|
1
1
|
import { NavigationRuntimeApi } from "@trackunit/iris-app-runtime-core-api";
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
3
|
+
* Hook to navigate programmatically within the Trackunit Manager or between Iris Apps.
|
|
4
|
+
*
|
|
5
|
+
* ### When to use
|
|
6
|
+
* Use `useNavigateInHost` when you need to navigate between Manager pages, entity home
|
|
7
|
+
* pages, or Iris Apps from component logic (e.g. after a mutation, on row click, in a
|
|
8
|
+
* redirect guard). Use the `get*Url` methods when you need an href for an `<a>` tag.
|
|
9
|
+
* Use `hasAccessTo` before showing navigation options the user may not have access to.
|
|
10
|
+
*
|
|
11
|
+
* ### When not to use
|
|
12
|
+
* Do not use for in-page anchor scrolling or query-param changes within the same
|
|
13
|
+
* Iris App — use your router directly.
|
|
4
14
|
*
|
|
5
15
|
* @requires NavigationContext
|
|
6
16
|
* @example
|
|
7
17
|
* import { useNavigateInHost } from "@trackunit/react-core-hooks";
|
|
8
|
-
*
|
|
18
|
+
* const { gotoAssetHome, gotoFleetApp } = useNavigateInHost();
|
|
19
|
+
*
|
|
20
|
+
* gotoAssetHome("abc-123", { page: "status" });
|
|
21
|
+
* gotoFleetApp({ page: "assets" });
|
|
22
|
+
* @see {@link NavigationRuntimeApi}
|
|
9
23
|
*/
|
|
10
24
|
export declare const useNavigateInHost: () => NavigationRuntimeApi;
|
package/src/toast/useToast.d.ts
CHANGED
|
@@ -1,18 +1,16 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* Hook to show toast notifications via the ToastContext.
|
|
3
3
|
*
|
|
4
4
|
* @requires ToastProvider
|
|
5
5
|
* @example
|
|
6
6
|
* import { useToast } from "@trackunit/react-core-hooks";
|
|
7
7
|
* const { addToast } = useToast();
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
* duration: 3000,
|
|
15
|
-
* });
|
|
8
|
+
*
|
|
9
|
+
* addToast({
|
|
10
|
+
* intent: "success",
|
|
11
|
+
* title: "Asset deleted",
|
|
12
|
+
* id: "deletion-status",
|
|
13
|
+
* });
|
|
16
14
|
* @see { ToastContextValue }
|
|
17
15
|
*/
|
|
18
16
|
export declare const useToast: () => import("@trackunit/react-core-contexts-api").ToastContextValue;
|
package/src/token/useToken.d.ts
CHANGED
|
@@ -1,7 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* Returns the current authentication token from the TokenContext.
|
|
3
|
+
*
|
|
4
|
+
* **When to use:**
|
|
5
|
+
* - Use `useToken` when you need the raw JWT bearer token for authenticated API calls
|
|
6
|
+
* outside the GraphQL layer (e.g. REST endpoints, file uploads, WebSockets).
|
|
7
|
+
* - Use `useCurrentUser` instead when you need the user's identity or account info.
|
|
8
|
+
* - Do **not** use for GraphQL requests — the Apollo client already attaches the token.
|
|
3
9
|
*
|
|
4
|
-
* @requires TokenProvider
|
|
5
10
|
* @example
|
|
6
11
|
* import { useToken } from "@trackunit/react-core-hooks";
|
|
7
12
|
* const { token } = useToken();
|
|
@@ -1,13 +1,22 @@
|
|
|
1
1
|
import { CurrentUserState } from "@trackunit/iris-app-runtime-core-api";
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
3
|
+
* Hook providing the authenticated user's profile, account, and permission data
|
|
4
|
+
* from the CurrentUserContext.
|
|
5
|
+
*
|
|
6
|
+
* ### When to use
|
|
7
|
+
* Use `useCurrentUser` when you need the user's identity or account context — displaying
|
|
8
|
+
* the user's name, resolving the effective account ID, or branching logic based on
|
|
9
|
+
* `isAssuming` or `isTrackunitUser`.
|
|
10
|
+
*
|
|
11
|
+
* ### When not to use
|
|
12
|
+
* - If you only need a boolean check for a single permission — use `useUserPermission`.
|
|
13
|
+
* - To gate entire routes — prefer server-side access control or `hasAccessTo` from
|
|
14
|
+
* `useNavigateInHost`.
|
|
4
15
|
*
|
|
5
|
-
* @requires CurrentUserProvider
|
|
6
16
|
* @example
|
|
7
17
|
* import { useCurrentUser } from "@trackunit/react-core-hooks";
|
|
8
18
|
* const { assumedUser, accountId } = useCurrentUser();
|
|
9
19
|
*
|
|
10
|
-
* //use it for something
|
|
11
20
|
* const { data, loading } = useGetAccountByIdQuery({
|
|
12
21
|
* variables: { accountId: assumedUser?.accountId || accountId || "" },
|
|
13
22
|
* });
|
|
@@ -2,7 +2,30 @@ export type HasAccess = boolean | null;
|
|
|
2
2
|
/**
|
|
3
3
|
* Hook that checks if the current user has a given permission.
|
|
4
4
|
*
|
|
5
|
+
* ### When to use
|
|
6
|
+
* Use `useUserPermission` to conditionally show or hide UI based on a single permission —
|
|
7
|
+
* delete buttons, admin panels, export actions, or any feature behind an access check.
|
|
8
|
+
*
|
|
9
|
+
* ### When not to use
|
|
10
|
+
* - When you need multiple user fields (name, account, assuming state) or need to check
|
|
11
|
+
* several permissions at once — use `useCurrentUser`.
|
|
12
|
+
* - To gate navigation targets — use `hasAccessTo` from `useNavigateInHost`.
|
|
13
|
+
*
|
|
5
14
|
* @param requiredPermission - The permission to check for typesafe string.
|
|
6
|
-
* @returns {
|
|
15
|
+
* @returns {boolean} `true` if the user has the permission, `false` otherwise (including while permissions are still loading).
|
|
16
|
+
* @example
|
|
17
|
+
* ```tsx
|
|
18
|
+
* import { useUserPermission } from "@trackunit/react-core-hooks";
|
|
19
|
+
*
|
|
20
|
+
* const DeleteButton = ({ assetId }: { assetId: string }) => {
|
|
21
|
+
* const canDelete = useUserPermission("asset:delete");
|
|
22
|
+
*
|
|
23
|
+
* if (!canDelete) {
|
|
24
|
+
* return null;
|
|
25
|
+
* }
|
|
26
|
+
*
|
|
27
|
+
* return <Button onClick={() => deleteAsset(assetId)}>Delete</Button>;
|
|
28
|
+
* };
|
|
29
|
+
* ```
|
|
7
30
|
*/
|
|
8
31
|
export declare const useUserPermission: <TPermission extends string>(requiredPermission: TPermission) => HasAccess;
|
|
@@ -2,7 +2,6 @@ import { LoadingState } from "@trackunit/iris-app-runtime-core-api";
|
|
|
2
2
|
/**
|
|
3
3
|
* This is a hook to use the WidgetConfigProvider.
|
|
4
4
|
*
|
|
5
|
-
* @requires WidgetConfigProvider
|
|
6
5
|
* @example
|
|
7
6
|
* import { useWidgetConfigAsync } from "@trackunit/react-core-hooks";
|
|
8
7
|
*
|
|
@@ -15,17 +14,19 @@ import { LoadingState } from "@trackunit/iris-app-runtime-core-api";
|
|
|
15
14
|
*/
|
|
16
15
|
export declare const useWidgetConfigAsync: () => import("@trackunit/iris-app-runtime-core-api").WidgetConfigContext;
|
|
17
16
|
/**
|
|
18
|
-
*
|
|
17
|
+
* Manages a dashboard widget's configuration, data, title, edit mode, filters, and time range.
|
|
18
|
+
*
|
|
19
|
+
* **When to use:**
|
|
20
|
+
* - Use `useWidgetConfig` inside any dashboard widget to read/write its persisted
|
|
21
|
+
* configuration, respond to dashboard-level filter and time-range changes, and
|
|
22
|
+
* manage the widget's edit-mode lifecycle.
|
|
23
|
+
* - Must be rendered inside a `WidgetConfigProvider` (provided automatically by the
|
|
24
|
+
* dashboard host).
|
|
19
25
|
*
|
|
20
26
|
* @example
|
|
21
27
|
* import { useWidgetConfig } from "@trackunit/react-core-hooks";
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
* const { ... } = useWidgetConfig();
|
|
25
|
-
*
|
|
26
|
-
* ...
|
|
27
|
-
* };
|
|
28
|
-
* @see { WidgetConfigContext}
|
|
28
|
+
* const { data, setData, isEditMode, filters, timeRange } = useWidgetConfig();
|
|
29
|
+
* @see {WidgetConfigContext}
|
|
29
30
|
*/
|
|
30
31
|
export declare const useWidgetConfig: () => {
|
|
31
32
|
data: Record<string, unknown> | null;
|