@namphuongtechnologi/np-hub 0.2.0 → 0.2.2

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/dist/index.cjs CHANGED
@@ -193,9 +193,9 @@ var SupportWidgetElement = class extends HTMLElement {
193
193
  this.prefilledAttachments = [];
194
194
  this.previewUrls = [];
195
195
  this.closeAfterSuccessTimer = null;
196
- this.toastAutoCloseTimer = null;
197
196
  this.toastCountdownInterval = null;
198
- this.toastDuration = 4e3;
197
+ this.toastSuccessDuration = 4e3;
198
+ this.toastErrorDuration = 4e3;
199
199
  this.toastRemainingMs = 0;
200
200
  this.toastTotalMs = 0;
201
201
  this.isToastPaused = false;
@@ -223,7 +223,6 @@ var SupportWidgetElement = class extends HTMLElement {
223
223
  disconnectedCallback() {
224
224
  window.removeEventListener("resize", this.handleResize);
225
225
  this.clearCloseAfterSuccessTimer();
226
- this.clearToastAutoCloseTimer();
227
226
  this.clearToastCountdown();
228
227
  }
229
228
  attributeChangedCallback() {
@@ -236,6 +235,10 @@ var SupportWidgetElement = class extends HTMLElement {
236
235
  this.user = user;
237
236
  this.prefillFromState();
238
237
  }
238
+ /**
239
+ * Updates the widget configuration.
240
+ * Supports custom toast auto-close durations via `toastDuration`.
241
+ */
239
242
  setConfig(config) {
240
243
  this.config = {
241
244
  ...this.config,
@@ -261,7 +264,23 @@ var SupportWidgetElement = class extends HTMLElement {
261
264
  this.emailContacts = config.emailContacts;
262
265
  }
263
266
  if (config.toastDuration !== void 0) {
264
- this.toastDuration = this.normalizeToastDuration(config.toastDuration);
267
+ const duration = config.toastDuration;
268
+ if (typeof duration === "number") {
269
+ const val = this.normalizeToastDuration(duration);
270
+ this.toastSuccessDuration = val;
271
+ this.toastErrorDuration = val;
272
+ } else if (duration && typeof duration === "object") {
273
+ if (duration.success !== void 0) {
274
+ this.toastSuccessDuration = this.normalizeToastDuration(duration.success);
275
+ } else {
276
+ this.toastSuccessDuration = 4e3;
277
+ }
278
+ if (duration.error !== void 0) {
279
+ this.toastErrorDuration = this.normalizeToastDuration(duration.error);
280
+ } else {
281
+ this.toastErrorDuration = 4e3;
282
+ }
283
+ }
265
284
  }
266
285
  }
267
286
  setFormPrefill(data) {
@@ -279,9 +298,14 @@ var SupportWidgetElement = class extends HTMLElement {
279
298
  var _a, _b;
280
299
  this.clearCloseAfterSuccessTimer();
281
300
  this.resetFormData();
301
+ this.hideToast(false);
282
302
  (_b = (_a = this.shadowRoot) == null ? void 0 : _a.getElementById("modal")) == null ? void 0 : _b.classList.remove("show");
283
303
  this.dispatchEvent(new CustomEvent(WIDGET_EVENTS.CLOSE, { bubbles: true }));
284
304
  }
305
+ /**
306
+ * Helper method to trigger a mock/demo toast popup for testing and development.
307
+ * @param type - Either "success" or "error"
308
+ */
285
309
  showDemoToast(type) {
286
310
  if (type === "success") {
287
311
  this.showToast({
@@ -308,9 +332,27 @@ var SupportWidgetElement = class extends HTMLElement {
308
332
  };
309
333
  const toastDurationAttr = this.getAttribute("toast-duration");
310
334
  if (toastDurationAttr !== null) {
311
- const parsed = parseInt(toastDurationAttr, 10);
312
- if (!isNaN(parsed)) {
313
- this.toastDuration = this.normalizeToastDuration(parsed);
335
+ const trimmed = toastDurationAttr.trim();
336
+ if (trimmed.startsWith("{")) {
337
+ try {
338
+ const parsed = JSON.parse(trimmed);
339
+ if (parsed && typeof parsed === "object") {
340
+ if (parsed.success !== void 0) {
341
+ this.toastSuccessDuration = this.normalizeToastDuration(Number(parsed.success));
342
+ }
343
+ if (parsed.error !== void 0) {
344
+ this.toastErrorDuration = this.normalizeToastDuration(Number(parsed.error));
345
+ }
346
+ }
347
+ } catch (e) {
348
+ }
349
+ } else {
350
+ const parsed = parseInt(trimmed, 10);
351
+ if (!isNaN(parsed)) {
352
+ const val = this.normalizeToastDuration(parsed);
353
+ this.toastSuccessDuration = val;
354
+ this.toastErrorDuration = val;
355
+ }
314
356
  }
315
357
  }
316
358
  }
@@ -751,7 +793,7 @@ var SupportWidgetElement = class extends HTMLElement {
751
793
  this.clearFieldError("email");
752
794
  this.clearFieldError("content");
753
795
  }
754
- showToast(payload, autoCloseMs = this.toastDuration) {
796
+ showToast(payload, autoCloseMs) {
755
797
  var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
756
798
  const toastEl = (_a = this.shadowRoot) == null ? void 0 : _a.getElementById("toast");
757
799
  const titleEl = (_b = this.shadowRoot) == null ? void 0 : _b.getElementById("toast-title");
@@ -772,7 +814,6 @@ var SupportWidgetElement = class extends HTMLElement {
772
814
  if (!toastEl || !titleEl || !messageEl || !detailsEl || !requestCodeEl || !linkRowEl || !linkEl || !countdownBarEl) {
773
815
  return;
774
816
  }
775
- this.clearToastAutoCloseTimer();
776
817
  this.clearToastCountdown();
777
818
  titleEl.textContent = payload.title;
778
819
  messageEl.textContent = payload.message;
@@ -801,8 +842,9 @@ var SupportWidgetElement = class extends HTMLElement {
801
842
  linkRowEl.classList.add("is-hidden");
802
843
  detailsEl.classList.add("is-hidden");
803
844
  }
804
- this.toastRemainingMs = autoCloseMs;
805
- this.toastTotalMs = autoCloseMs;
845
+ const duration = autoCloseMs !== void 0 ? autoCloseMs : payload.type === "success" ? this.toastSuccessDuration : this.toastErrorDuration;
846
+ this.toastRemainingMs = duration;
847
+ this.toastTotalMs = duration;
806
848
  const toastCardEl = (_l = this.shadowRoot) == null ? void 0 : _l.querySelector(".toast-card");
807
849
  this.isToastPaused = Boolean(toastCardEl == null ? void 0 : toastCardEl.matches(":hover"));
808
850
  toastEl.classList.remove("success", "error");
@@ -813,7 +855,7 @@ var SupportWidgetElement = class extends HTMLElement {
813
855
  countdownBarEl
814
856
  );
815
857
  }
816
- hideToast() {
858
+ hideToast(shouldCloseModal = true) {
817
859
  var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
818
860
  const toastEl = (_a = this.shadowRoot) == null ? void 0 : _a.getElementById("toast");
819
861
  const titleEl = (_b = this.shadowRoot) == null ? void 0 : _b.getElementById("toast-title");
@@ -834,7 +876,7 @@ var SupportWidgetElement = class extends HTMLElement {
834
876
  if (!toastEl || !titleEl || !messageEl || !detailsEl || !requestCodeEl || !linkRowEl || !linkEl || !countdownBarEl) {
835
877
  return;
836
878
  }
837
- this.clearToastAutoCloseTimer();
879
+ const isSuccess = toastEl.classList.contains("success");
838
880
  this.clearToastCountdown();
839
881
  this.toastRemainingMs = 0;
840
882
  this.toastTotalMs = 0;
@@ -855,6 +897,9 @@ var SupportWidgetElement = class extends HTMLElement {
855
897
  countdownEl.classList.add("is-hidden");
856
898
  }
857
899
  toastEl.classList.remove("success", "error", "show");
900
+ if (isSuccess && shouldCloseModal) {
901
+ this.close();
902
+ }
858
903
  }
859
904
  startToastCountdown(countdownEl, secondsEl, barEl) {
860
905
  const updateCountdown = () => {
@@ -897,12 +942,6 @@ var SupportWidgetElement = class extends HTMLElement {
897
942
  title: content.type === "success" ? "G\u1EEDi y\xEAu c\u1EA7u th\xE0nh c\xF4ng" : "G\u1EEDi y\xEAu c\u1EA7u th\u1EA5t b\u1EA1i"
898
943
  };
899
944
  }
900
- clearToastAutoCloseTimer() {
901
- if (this.toastAutoCloseTimer !== null) {
902
- window.clearTimeout(this.toastAutoCloseTimer);
903
- this.toastAutoCloseTimer = null;
904
- }
905
- }
906
945
  normalizeToastDuration(value) {
907
946
  if (!Number.isFinite(value) || value <= 0) {
908
947
  return 4e3;
@@ -954,7 +993,7 @@ var SupportWidgetElement = class extends HTMLElement {
954
993
  async submit() {
955
994
  var _a;
956
995
  this.clearCloseAfterSuccessTimer();
957
- this.hideToast();
996
+ this.hideToast(false);
958
997
  if (!this.validateRequiredFields()) {
959
998
  this.dispatchEvent(
960
999
  new CustomEvent(WIDGET_EVENTS.SUBMIT_ERROR, {
@@ -1044,8 +1083,8 @@ var SupportWidgetElement = class extends HTMLElement {
1044
1083
  })
1045
1084
  );
1046
1085
  this.closeAfterSuccessTimer = window.setTimeout(() => {
1047
- this.close();
1048
- }, 1200);
1086
+ this.hideToast();
1087
+ }, this.toastSuccessDuration);
1049
1088
  } catch (error) {
1050
1089
  const errorContent = mapHttpClientError(error);
1051
1090
  this.showToast(this.toToastPayload(errorContent));