agx-chat-web 1.2.10 → 1.2.11

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.
@@ -927,8 +927,16 @@ function pumpListSlaQueue() {
927
927
  }
928
928
  }
929
929
  function computeListSlaSeconds(createdAt) {
930
- var sla = new SlaDates(0);
931
- return sla.getDifferenceInSeconds(timeAsDayjs(createdAt, { server: true }), timeAsDayjs(undefined, { server: true }), SLA_WORKING_HOURS);
930
+ var start = timeAsDayjs(createdAt, { server: true });
931
+ var end = timeAsDayjs(undefined, { server: true });
932
+ if (!start.isValid() || !end.isValid() || end.isBefore(start))
933
+ return null;
934
+ return new SlaDates(0).getDifferenceInSeconds(start, end, SLA_WORKING_HOURS);
935
+ }
936
+ /** Próximo HH:MM de getDifferenceInSeconds */
937
+ function msUntilNextSlaDisplayMinute(seconds) {
938
+ var remainder = seconds % 60;
939
+ return Math.max(250, (60 - remainder) * 1000);
932
940
  }
933
941
  function MessengerListItem(_a) {
934
942
  var _b, _c;
@@ -942,38 +950,80 @@ function MessengerListItem(_a) {
942
950
  var _e = (0,external_react_namespaceObject.useState)(''), background = _e[0], setBackground = _e[1];
943
951
  var rowRef = (0,external_react_namespaceObject.useRef)(null);
944
952
  var scheduledRef = (0,external_react_namespaceObject.useRef)(false);
953
+ var slaTimeoutRef = (0,external_react_namespaceObject.useRef)(undefined);
954
+ var isSelected = currentId === item._id;
955
+ var isSelectedRef = (0,external_react_namespaceObject.useRef)(isSelected);
956
+ var secondTickActiveRef = (0,external_react_namespaceObject.useRef)(false);
957
+ isSelectedRef.current = isSelected;
958
+ var isInprogress = ((_c = (_b = item.status) === null || _b === void 0 ? void 0 : _b.current) === null || _c === void 0 ? void 0 : _c.systemicValue) === 'inprogress';
945
959
  var beSnapshot = typeof item.SLATimePassed === 'number' ? item.SLATimePassed : null;
946
960
  var _f = (0,external_react_namespaceObject.useState)(function () {
947
961
  var _a;
948
- if (beSnapshot != null)
962
+ if (!isInprogress && beSnapshot != null)
949
963
  return beSnapshot;
950
964
  return (_a = listSlaCache.get(item._id)) !== null && _a !== void 0 ? _a : null;
951
965
  }), idleSeconds = _f[0], setIdleSeconds = _f[1];
952
966
  var listSlaSeconds = canSeeDetailsChat && idleSeconds != null ? idleSeconds : null;
953
967
  var theme = useThemes().theme;
954
- var isInprogress = ((_c = (_b = item.status) === null || _b === void 0 ? void 0 : _b.current) === null || _c === void 0 ? void 0 : _c.systemicValue) === 'inprogress';
955
968
  var otherChatMember = (0,external_react_namespaceObject.useMemo)(function () {
956
969
  return (item === null || item === void 0 ? void 0 : item.users.find(function (item) { return item.uniqueCode !== currentViewerId; })) ||
957
970
  item.creator;
958
971
  }, [item === null || item === void 0 ? void 0 : item.users, currentViewerId]);
959
- // Agenda Gueff por ticket (cache). Não usa setState de visibility no scroll.
972
+ var applyListSlaSeconds = function (seconds) {
973
+ listSlaCache.set(item._id, seconds);
974
+ setIdleSeconds(function (prev) {
975
+ if (prev != null &&
976
+ formatSlaHoursMinutes(prev) === formatSlaHoursMinutes(seconds)) {
977
+ return prev;
978
+ }
979
+ return seconds;
980
+ });
981
+ };
982
+ var clearSlaMinuteTimeout = function () {
983
+ if (slaTimeoutRef.current) {
984
+ clearTimeout(slaTimeoutRef.current);
985
+ slaTimeoutRef.current = undefined;
986
+ }
987
+ };
988
+ var refreshListSla = function (continueOnSlaMinute) {
989
+ var seconds = computeListSlaSeconds(item.createdAt);
990
+ if (seconds == null) {
991
+ scheduledRef.current = false;
992
+ return;
993
+ }
994
+ applyListSlaSeconds(seconds);
995
+ // Fila atrasada: se o ticket já está aberto, o interval 1s manda.
996
+ if (!continueOnSlaMinute ||
997
+ isSelectedRef.current ||
998
+ secondTickActiveRef.current) {
999
+ clearSlaMinuteTimeout();
1000
+ return;
1001
+ }
1002
+ clearSlaMinuteTimeout();
1003
+ slaTimeoutRef.current = setTimeout(function () {
1004
+ refreshListSla(true);
1005
+ }, msUntilNextSlaDisplayMinute(seconds));
1006
+ };
960
1007
  (0,external_react_namespaceObject.useEffect)(function () {
961
1008
  var _a;
962
- if (!canSeeDetailsChat || !isInprogress || !item.createdAt)
1009
+ if (!canSeeDetailsChat) {
1010
+ setIdleSeconds(null);
963
1011
  return;
964
- if (beSnapshot != null) {
965
- if (idleSeconds !== beSnapshot)
966
- setIdleSeconds(beSnapshot);
1012
+ }
1013
+ if (!isInprogress) {
1014
+ setIdleSeconds(beSnapshot);
967
1015
  return;
968
1016
  }
1017
+ if (!item.createdAt)
1018
+ return;
1019
+ scheduledRef.current = false;
1020
+ var applyFresh = function () {
1021
+ refreshListSla(!isSelectedRef.current);
1022
+ };
969
1023
  var cached = listSlaCache.get(item._id);
970
1024
  if (cached != null) {
971
- if (idleSeconds !== cached)
972
- setIdleSeconds(cached);
973
- return;
1025
+ applyListSlaSeconds(cached);
974
1026
  }
975
- if (scheduledRef.current)
976
- return;
977
1027
  var node = rowRef.current;
978
1028
  if (!node)
979
1029
  return;
@@ -981,25 +1031,9 @@ function MessengerListItem(_a) {
981
1031
  var scheduleOnce = function () {
982
1032
  if (scheduledRef.current)
983
1033
  return;
984
- if (listSlaCache.has(item._id)) {
985
- var value = listSlaCache.get(item._id);
986
- if (value != null)
987
- setIdleSeconds(value);
988
- return;
989
- }
990
1034
  scheduledRef.current = true;
991
- enqueueListSlaCalc(function () {
992
- try {
993
- var seconds = computeListSlaSeconds(item.createdAt);
994
- listSlaCache.set(item._id, seconds);
995
- setIdleSeconds(seconds);
996
- }
997
- catch (_a) {
998
- scheduledRef.current = false;
999
- }
1000
- });
1035
+ enqueueListSlaCalc(applyFresh);
1001
1036
  };
1002
- // Já no viewport no mount → agenda; senão espera 1ª interseção (sem setState).
1003
1037
  var rect = node.getBoundingClientRect();
1004
1038
  var rootRect = root === null || root === void 0 ? void 0 : root.getBoundingClientRect();
1005
1039
  var roughlyVisible = rootRect
@@ -1007,25 +1041,58 @@ function MessengerListItem(_a) {
1007
1041
  : rect.top < window.innerHeight && rect.bottom > 0;
1008
1042
  if (roughlyVisible) {
1009
1043
  scheduleOnce();
1010
- return;
1044
+ return undefined;
1045
+ }
1046
+ else {
1047
+ var observer_1 = new IntersectionObserver(function (_a) {
1048
+ var entry = _a[0];
1049
+ if (!entry.isIntersecting)
1050
+ return;
1051
+ scheduleOnce();
1052
+ observer_1.disconnect();
1053
+ }, { root: root, rootMargin: '80px 0px', threshold: 0 });
1054
+ observer_1.observe(node);
1055
+ return function () { return observer_1.disconnect(); };
1056
+ }
1057
+ }, [canSeeDetailsChat, beSnapshot, isInprogress, item._id, item.createdAt]);
1058
+ // Chat aberto: mesma cadência do header (1s), UI só HH:MM.
1059
+ // Restantes: refreshListSla no próximo minuto de getDifferenceInSeconds.
1060
+ (0,external_react_namespaceObject.useEffect)(function () {
1061
+ if (!canSeeDetailsChat || !isInprogress || !item.createdAt) {
1062
+ return function () {
1063
+ secondTickActiveRef.current = false;
1064
+ clearSlaMinuteTimeout();
1065
+ };
1066
+ }
1067
+ if (!isSelected) {
1068
+ return function () {
1069
+ secondTickActiveRef.current = false;
1070
+ clearSlaMinuteTimeout();
1071
+ };
1011
1072
  }
1012
- var observer = new IntersectionObserver(function (_a) {
1013
- var entry = _a[0];
1014
- if (!entry.isIntersecting)
1073
+ secondTickActiveRef.current = true;
1074
+ clearSlaMinuteTimeout();
1075
+ var sync = function () {
1076
+ var seconds = computeListSlaSeconds(item.createdAt);
1077
+ if (seconds == null)
1015
1078
  return;
1016
- scheduleOnce();
1017
- observer.disconnect();
1018
- }, { root: root, rootMargin: '80px 0px', threshold: 0 });
1019
- observer.observe(node);
1020
- return function () { return observer.disconnect(); };
1021
- }, [
1022
- canSeeDetailsChat,
1023
- beSnapshot,
1024
- isInprogress,
1025
- item._id,
1026
- item.createdAt,
1027
- idleSeconds,
1028
- ]);
1079
+ applyListSlaSeconds(seconds);
1080
+ };
1081
+ sync();
1082
+ var interval = setInterval(function () {
1083
+ if (document.visibilityState !== 'visible')
1084
+ return;
1085
+ sync();
1086
+ }, 1000);
1087
+ return function () {
1088
+ secondTickActiveRef.current = false;
1089
+ clearInterval(interval);
1090
+ clearSlaMinuteTimeout();
1091
+ if (!isSelectedRef.current) {
1092
+ refreshListSla(true);
1093
+ }
1094
+ };
1095
+ }, [canSeeDetailsChat, isInprogress, isSelected, item._id, item.createdAt]);
1029
1096
  (0,external_react_namespaceObject.useEffect)(function () {
1030
1097
  setUnreadMessages(item.totalUnreadMessages);
1031
1098
  }, [item]);