@trackunit/react-core-hooks 1.12.67 → 1.12.69

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.
Files changed (3) hide show
  1. package/index.cjs.js +72 -41
  2. package/index.esm.js +72 -41
  3. package/package.json +4 -4
package/index.cjs.js CHANGED
@@ -396,7 +396,7 @@ const resolveAccess = async (context, options) => {
396
396
  }
397
397
  return context.hasAccessTo(options);
398
398
  };
399
- const INITIAL_STATE = {
399
+ const INITIAL_STATE$1 = {
400
400
  status: "loading",
401
401
  hasAccess: undefined,
402
402
  error: undefined,
@@ -404,7 +404,7 @@ const INITIAL_STATE = {
404
404
  const accessReducer = (_state, action) => {
405
405
  switch (action.type) {
406
406
  case "FETCH_START":
407
- return INITIAL_STATE;
407
+ return INITIAL_STATE$1;
408
408
  case "FETCH_SUCCESS":
409
409
  return { status: "success", hasAccess: action.hasAccess, error: undefined };
410
410
  case "FETCH_ERROR":
@@ -427,7 +427,7 @@ const accessReducer = (_state, action) => {
427
427
  const useHasAccessTo = (options) => {
428
428
  const context = react.useContext(reactCoreContextsApi.NavigationContext);
429
429
  const [stableOptions, setStableOptions] = react.useState(options);
430
- const [state, dispatch] = react.useReducer(accessReducer, INITIAL_STATE);
430
+ const [state, dispatch] = react.useReducer(accessReducer, INITIAL_STATE$1);
431
431
  if (!esToolkit.isEqual(stableOptions, options)) {
432
432
  setStableOptions(options);
433
433
  }
@@ -576,6 +576,17 @@ const useCustomerRuntime = () => {
576
576
  return { customerInfo, loading, error };
577
577
  };
578
578
 
579
+ const eventRuntimeReducer = (_state, action) => {
580
+ switch (action.type) {
581
+ case "success":
582
+ return { status: "success", eventInfo: action.eventInfo };
583
+ case "error":
584
+ return { status: "error", error: action.error };
585
+ default:
586
+ return _state;
587
+ }
588
+ };
589
+ const INITIAL_STATE = { status: "loading" };
579
590
  /**
580
591
  * A hook to expose event runtime for React components
581
592
  *
@@ -594,25 +605,17 @@ const useCustomerRuntime = () => {
594
605
  * }, [getEventQuery, eventInfo]);
595
606
  */
596
607
  const useEventRuntime = () => {
597
- const [eventInfo, setEventInfo] = react.useState();
598
- const [loading, setLoading] = react.useState(true);
599
- const [error, setError] = react.useState();
608
+ const [state, dispatch] = react.useReducer(eventRuntimeReducer, INITIAL_STATE);
600
609
  react.useEffect(() => {
601
- const getEventInfo = async () => {
602
- setLoading(true);
603
- try {
604
- const updatedEventInfo = await irisAppRuntimeCore.EventRuntime.getEventInfo();
605
- setLoading(false);
606
- setEventInfo(updatedEventInfo);
607
- }
608
- catch (e) {
609
- setLoading(false);
610
- setError(new Error("Failed to get event info"));
611
- }
612
- };
613
- void getEventInfo();
610
+ void irisAppRuntimeCore.EventRuntime.getEventInfo()
611
+ .then(eventInfo => dispatch({ type: "success", eventInfo }))
612
+ .catch(() => dispatch({ type: "error", error: new Error("Failed to get event info") }));
614
613
  }, []);
615
- return { eventInfo, loading, error };
614
+ return {
615
+ eventInfo: state.status === "success" ? state.eventInfo : undefined,
616
+ loading: state.status === "loading",
617
+ error: state.status === "error" ? state.error : undefined,
618
+ };
616
619
  };
617
620
 
618
621
  /**
@@ -981,6 +984,28 @@ const useWidgetConfigAsync = () => {
981
984
  }
982
985
  return context;
983
986
  };
987
+ const widgetDataReducer = (state, action) => {
988
+ switch (action.type) {
989
+ case "dataLoaded":
990
+ return { ...state, data: action.data, loadingData: false };
991
+ case "dataVersionLoaded":
992
+ return { ...state, dataVersion: action.dataVersion };
993
+ case "titleLoaded":
994
+ return { ...state, title: action.title };
995
+ case "dataUpdated":
996
+ return { ...state, data: action.data, dataVersion: action.dataVersion };
997
+ case "titleUpdated":
998
+ return { ...state, title: action.title };
999
+ default:
1000
+ return state;
1001
+ }
1002
+ };
1003
+ const INITIAL_WIDGET_DATA_STATE = {
1004
+ data: null,
1005
+ loadingData: true,
1006
+ dataVersion: null,
1007
+ title: null,
1008
+ };
984
1009
  /**
985
1010
  * This is a hook to use the WidgetConfigContext.
986
1011
  *
@@ -996,13 +1021,9 @@ const useWidgetConfigAsync = () => {
996
1021
  */
997
1022
  const useWidgetConfig = () => {
998
1023
  const widgetConfigContext = useWidgetConfigAsync();
999
- const [data, setData] = react.useState(null);
1000
- const [loadingData, setLoadingData] = react.useState(true);
1001
- const [dataVersion, setDataVersion] = react.useState(null);
1002
- const [title, setTitle] = react.useState(null);
1024
+ const [widgetData, dispatch] = react.useReducer(widgetDataReducer, INITIAL_WIDGET_DATA_STATE);
1003
1025
  const filters = useFilterBarContext();
1004
1026
  const { timeRange } = useTimeRange();
1005
- // use window.location.hash directly to avoid depending on tanstack router in core-hooks
1006
1027
  const [edit, setEdit] = react.useState(() => window.location.hash.includes("edit=true"));
1007
1028
  react.useEffect(() => {
1008
1029
  const handleHashChange = () => {
@@ -1013,29 +1034,27 @@ const useWidgetConfig = () => {
1013
1034
  }, []);
1014
1035
  const widgetConfigContextRef = react.useRef(widgetConfigContext);
1015
1036
  react.useEffect(() => {
1016
- void widgetConfigContextRef.current.getData().then(d => {
1017
- setData(d);
1018
- setLoadingData(false);
1019
- });
1020
- void widgetConfigContextRef.current.getDataVersion().then(dv => setDataVersion(dv));
1021
- void widgetConfigContextRef.current.getTitle().then(t => setTitle(t));
1037
+ void widgetConfigContextRef.current.getData().then(d => dispatch({ type: "dataLoaded", data: d }));
1038
+ void widgetConfigContextRef.current
1039
+ .getDataVersion()
1040
+ .then(dv => dispatch({ type: "dataVersionLoaded", dataVersion: dv }));
1041
+ void widgetConfigContextRef.current.getTitle().then(t => dispatch({ type: "titleLoaded", title: t }));
1022
1042
  }, []);
1023
1043
  const result = react.useMemo(() => ({
1024
- data,
1044
+ data: widgetData.data,
1025
1045
  setData: async (newData, newDataVersion) => {
1026
1046
  await widgetConfigContext.setData(newData, newDataVersion);
1027
- setData(newData);
1028
- setDataVersion(newDataVersion);
1047
+ dispatch({ type: "dataUpdated", data: newData, dataVersion: newDataVersion });
1029
1048
  },
1030
- dataVersion,
1031
- loadingData,
1049
+ dataVersion: widgetData.dataVersion,
1050
+ loadingData: widgetData.loadingData,
1032
1051
  setLoadingState: async (newLoadingState) => {
1033
1052
  await widgetConfigContext.setLoadingState(newLoadingState);
1034
1053
  },
1035
- title,
1054
+ title: widgetData.title,
1036
1055
  setTitle: async (newTitle) => {
1037
1056
  await widgetConfigContext.setTitle(newTitle);
1038
- setTitle(newTitle);
1057
+ dispatch({ type: "titleUpdated", title: newTitle });
1039
1058
  },
1040
1059
  filters,
1041
1060
  timeRange,
@@ -1048,13 +1067,25 @@ const useWidgetConfig = () => {
1048
1067
  if (props) {
1049
1068
  if (props.newData && props.newData.data) {
1050
1069
  await irisAppRuntimeCore.WidgetConfigRuntime.setWidgetData(props.newData.data, props.newData.dataVersion ?? 1, props.newTitle ?? undefined);
1051
- setData(props.newData.data);
1052
- setDataVersion(props.newData.dataVersion ?? 1);
1070
+ dispatch({
1071
+ type: "dataUpdated",
1072
+ data: props.newData.data,
1073
+ dataVersion: props.newData.dataVersion ?? 1,
1074
+ });
1053
1075
  }
1054
1076
  }
1055
1077
  await widgetConfigContext.closeEditMode();
1056
1078
  },
1057
- }), [data, dataVersion, title, filters, timeRange, edit, loadingData, widgetConfigContext]);
1079
+ }), [
1080
+ widgetData.data,
1081
+ widgetData.dataVersion,
1082
+ widgetData.title,
1083
+ filters,
1084
+ timeRange,
1085
+ edit,
1086
+ widgetData.loadingData,
1087
+ widgetConfigContext,
1088
+ ]);
1058
1089
  return result;
1059
1090
  };
1060
1091
 
package/index.esm.js CHANGED
@@ -394,7 +394,7 @@ const resolveAccess = async (context, options) => {
394
394
  }
395
395
  return context.hasAccessTo(options);
396
396
  };
397
- const INITIAL_STATE = {
397
+ const INITIAL_STATE$1 = {
398
398
  status: "loading",
399
399
  hasAccess: undefined,
400
400
  error: undefined,
@@ -402,7 +402,7 @@ const INITIAL_STATE = {
402
402
  const accessReducer = (_state, action) => {
403
403
  switch (action.type) {
404
404
  case "FETCH_START":
405
- return INITIAL_STATE;
405
+ return INITIAL_STATE$1;
406
406
  case "FETCH_SUCCESS":
407
407
  return { status: "success", hasAccess: action.hasAccess, error: undefined };
408
408
  case "FETCH_ERROR":
@@ -425,7 +425,7 @@ const accessReducer = (_state, action) => {
425
425
  const useHasAccessTo = (options) => {
426
426
  const context = useContext(NavigationContext);
427
427
  const [stableOptions, setStableOptions] = useState(options);
428
- const [state, dispatch] = useReducer(accessReducer, INITIAL_STATE);
428
+ const [state, dispatch] = useReducer(accessReducer, INITIAL_STATE$1);
429
429
  if (!isEqual(stableOptions, options)) {
430
430
  setStableOptions(options);
431
431
  }
@@ -574,6 +574,17 @@ const useCustomerRuntime = () => {
574
574
  return { customerInfo, loading, error };
575
575
  };
576
576
 
577
+ const eventRuntimeReducer = (_state, action) => {
578
+ switch (action.type) {
579
+ case "success":
580
+ return { status: "success", eventInfo: action.eventInfo };
581
+ case "error":
582
+ return { status: "error", error: action.error };
583
+ default:
584
+ return _state;
585
+ }
586
+ };
587
+ const INITIAL_STATE = { status: "loading" };
577
588
  /**
578
589
  * A hook to expose event runtime for React components
579
590
  *
@@ -592,25 +603,17 @@ const useCustomerRuntime = () => {
592
603
  * }, [getEventQuery, eventInfo]);
593
604
  */
594
605
  const useEventRuntime = () => {
595
- const [eventInfo, setEventInfo] = useState();
596
- const [loading, setLoading] = useState(true);
597
- const [error, setError] = useState();
606
+ const [state, dispatch] = useReducer(eventRuntimeReducer, INITIAL_STATE);
598
607
  useEffect(() => {
599
- const getEventInfo = async () => {
600
- setLoading(true);
601
- try {
602
- const updatedEventInfo = await EventRuntime.getEventInfo();
603
- setLoading(false);
604
- setEventInfo(updatedEventInfo);
605
- }
606
- catch (e) {
607
- setLoading(false);
608
- setError(new Error("Failed to get event info"));
609
- }
610
- };
611
- void getEventInfo();
608
+ void EventRuntime.getEventInfo()
609
+ .then(eventInfo => dispatch({ type: "success", eventInfo }))
610
+ .catch(() => dispatch({ type: "error", error: new Error("Failed to get event info") }));
612
611
  }, []);
613
- return { eventInfo, loading, error };
612
+ return {
613
+ eventInfo: state.status === "success" ? state.eventInfo : undefined,
614
+ loading: state.status === "loading",
615
+ error: state.status === "error" ? state.error : undefined,
616
+ };
614
617
  };
615
618
 
616
619
  /**
@@ -979,6 +982,28 @@ const useWidgetConfigAsync = () => {
979
982
  }
980
983
  return context;
981
984
  };
985
+ const widgetDataReducer = (state, action) => {
986
+ switch (action.type) {
987
+ case "dataLoaded":
988
+ return { ...state, data: action.data, loadingData: false };
989
+ case "dataVersionLoaded":
990
+ return { ...state, dataVersion: action.dataVersion };
991
+ case "titleLoaded":
992
+ return { ...state, title: action.title };
993
+ case "dataUpdated":
994
+ return { ...state, data: action.data, dataVersion: action.dataVersion };
995
+ case "titleUpdated":
996
+ return { ...state, title: action.title };
997
+ default:
998
+ return state;
999
+ }
1000
+ };
1001
+ const INITIAL_WIDGET_DATA_STATE = {
1002
+ data: null,
1003
+ loadingData: true,
1004
+ dataVersion: null,
1005
+ title: null,
1006
+ };
982
1007
  /**
983
1008
  * This is a hook to use the WidgetConfigContext.
984
1009
  *
@@ -994,13 +1019,9 @@ const useWidgetConfigAsync = () => {
994
1019
  */
995
1020
  const useWidgetConfig = () => {
996
1021
  const widgetConfigContext = useWidgetConfigAsync();
997
- const [data, setData] = useState(null);
998
- const [loadingData, setLoadingData] = useState(true);
999
- const [dataVersion, setDataVersion] = useState(null);
1000
- const [title, setTitle] = useState(null);
1022
+ const [widgetData, dispatch] = useReducer(widgetDataReducer, INITIAL_WIDGET_DATA_STATE);
1001
1023
  const filters = useFilterBarContext();
1002
1024
  const { timeRange } = useTimeRange();
1003
- // use window.location.hash directly to avoid depending on tanstack router in core-hooks
1004
1025
  const [edit, setEdit] = useState(() => window.location.hash.includes("edit=true"));
1005
1026
  useEffect(() => {
1006
1027
  const handleHashChange = () => {
@@ -1011,29 +1032,27 @@ const useWidgetConfig = () => {
1011
1032
  }, []);
1012
1033
  const widgetConfigContextRef = useRef(widgetConfigContext);
1013
1034
  useEffect(() => {
1014
- void widgetConfigContextRef.current.getData().then(d => {
1015
- setData(d);
1016
- setLoadingData(false);
1017
- });
1018
- void widgetConfigContextRef.current.getDataVersion().then(dv => setDataVersion(dv));
1019
- void widgetConfigContextRef.current.getTitle().then(t => setTitle(t));
1035
+ void widgetConfigContextRef.current.getData().then(d => dispatch({ type: "dataLoaded", data: d }));
1036
+ void widgetConfigContextRef.current
1037
+ .getDataVersion()
1038
+ .then(dv => dispatch({ type: "dataVersionLoaded", dataVersion: dv }));
1039
+ void widgetConfigContextRef.current.getTitle().then(t => dispatch({ type: "titleLoaded", title: t }));
1020
1040
  }, []);
1021
1041
  const result = useMemo(() => ({
1022
- data,
1042
+ data: widgetData.data,
1023
1043
  setData: async (newData, newDataVersion) => {
1024
1044
  await widgetConfigContext.setData(newData, newDataVersion);
1025
- setData(newData);
1026
- setDataVersion(newDataVersion);
1045
+ dispatch({ type: "dataUpdated", data: newData, dataVersion: newDataVersion });
1027
1046
  },
1028
- dataVersion,
1029
- loadingData,
1047
+ dataVersion: widgetData.dataVersion,
1048
+ loadingData: widgetData.loadingData,
1030
1049
  setLoadingState: async (newLoadingState) => {
1031
1050
  await widgetConfigContext.setLoadingState(newLoadingState);
1032
1051
  },
1033
- title,
1052
+ title: widgetData.title,
1034
1053
  setTitle: async (newTitle) => {
1035
1054
  await widgetConfigContext.setTitle(newTitle);
1036
- setTitle(newTitle);
1055
+ dispatch({ type: "titleUpdated", title: newTitle });
1037
1056
  },
1038
1057
  filters,
1039
1058
  timeRange,
@@ -1046,13 +1065,25 @@ const useWidgetConfig = () => {
1046
1065
  if (props) {
1047
1066
  if (props.newData && props.newData.data) {
1048
1067
  await WidgetConfigRuntime.setWidgetData(props.newData.data, props.newData.dataVersion ?? 1, props.newTitle ?? undefined);
1049
- setData(props.newData.data);
1050
- setDataVersion(props.newData.dataVersion ?? 1);
1068
+ dispatch({
1069
+ type: "dataUpdated",
1070
+ data: props.newData.data,
1071
+ dataVersion: props.newData.dataVersion ?? 1,
1072
+ });
1051
1073
  }
1052
1074
  }
1053
1075
  await widgetConfigContext.closeEditMode();
1054
1076
  },
1055
- }), [data, dataVersion, title, filters, timeRange, edit, loadingData, widgetConfigContext]);
1077
+ }), [
1078
+ widgetData.data,
1079
+ widgetData.dataVersion,
1080
+ widgetData.title,
1081
+ filters,
1082
+ timeRange,
1083
+ edit,
1084
+ widgetData.loadingData,
1085
+ widgetConfigContext,
1086
+ ]);
1056
1087
  return result;
1057
1088
  };
1058
1089
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trackunit/react-core-hooks",
3
- "version": "1.12.67",
3
+ "version": "1.12.69",
4
4
  "repository": "https://github.com/Trackunit/manager",
5
5
  "license": "SEE LICENSE IN LICENSE.txt",
6
6
  "engines": {
@@ -9,9 +9,9 @@
9
9
  "dependencies": {
10
10
  "react": "19.0.0",
11
11
  "es-toolkit": "^1.39.10",
12
- "@trackunit/iris-app-runtime-core": "1.13.60",
13
- "@trackunit/iris-app-runtime-core-api": "1.12.57",
14
- "@trackunit/react-core-contexts-api": "1.13.58"
12
+ "@trackunit/iris-app-runtime-core": "1.13.61",
13
+ "@trackunit/iris-app-runtime-core-api": "1.12.58",
14
+ "@trackunit/react-core-contexts-api": "1.13.59"
15
15
  },
16
16
  "module": "./index.esm.js",
17
17
  "main": "./index.cjs.js",