callix-dialer-widget 1.4.9 → 1.5.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.
@@ -53110,6 +53110,9 @@ function normalizePhoneNumberPaste(pastedText) {
53110
53110
  if (numero[0] === "9") {
53111
53111
  return cleaned;
53112
53112
  }
53113
+ if (["2", "3", "4"].includes(numero[0])) {
53114
+ return cleaned;
53115
+ }
53113
53116
  return ddd + "9" + numero;
53114
53117
  }
53115
53118
  if (len === 12) {
@@ -53753,6 +53756,7 @@ function AuthenticatedDialer({ username, dialValue, setDialValue, onLogout, vari
53753
53756
  const dialerCardRef = useRef(null);
53754
53757
  const dialDisplayRef = useRef(null);
53755
53758
  const hiddenInputRef = useRef(null);
53759
+ const visibleInputRef = useRef(null);
53756
53760
  const [callError, setCallError] = useState(null);
53757
53761
  const [availabilityError, setAvailabilityError] = useState(null);
53758
53762
  const [afterCallError, setAfterCallError] = useState(null);
@@ -53760,6 +53764,7 @@ function AuthenticatedDialer({ username, dialValue, setDialValue, onLogout, vari
53760
53764
  const [afterCallResult, setAfterCallResult] = useState("success");
53761
53765
  const [selectedQualificationId, setSelectedQualificationId] = useState(null);
53762
53766
  const [callDurationSeconds, setCallDurationSeconds] = useState(0);
53767
+ const [cursorPosition, setCursorPosition] = useState(0);
53763
53768
  const [networkMetric, setNetworkMetric] = useState(INITIAL_NETWORK_METRIC);
53764
53769
  const [audioMetric, setAudioMetric] = useState(INITIAL_AUDIO_METRIC);
53765
53770
  const toneContextRef = useRef(null);
@@ -54226,39 +54231,90 @@ function AuthenticatedDialer({ username, dialValue, setDialValue, onLogout, vari
54226
54231
  return;
54227
54232
  }
54228
54233
  setCallError(null);
54229
- setDialValue((prev) => (prev + digit).slice(0, MAX_DIAL_LENGTH));
54234
+ if (!/^[0-9*#]$/.test(digit)) {
54235
+ console.warn("[handleDial] Dígito inválido ignorado:", digit);
54236
+ return;
54237
+ }
54238
+ const capturedPos = cursorPosition;
54239
+ setDialValue((prevValue) => {
54240
+ const currentValue = prevValue;
54241
+ const newValue = currentValue.slice(0, capturedPos) + digit + currentValue.slice(capturedPos);
54242
+ const trimmedValue = newValue.slice(0, MAX_DIAL_LENGTH);
54243
+ const newCursorPos = Math.min(capturedPos + 1, trimmedValue.length);
54244
+ setCursorPosition(newCursorPos);
54245
+ setTimeout(() => {
54246
+ if (visibleInputRef.current) {
54247
+ visibleInputRef.current.focus();
54248
+ visibleInputRef.current.setSelectionRange(newCursorPos, newCursorPos);
54249
+ }
54250
+ }, 0);
54251
+ return trimmedValue;
54252
+ });
54230
54253
  },
54231
- [playTone, currentCall, isInCall, isRinging, canEditNumber, setDialValue]
54254
+ [playTone, currentCall, isInCall, isRinging, canEditNumber, cursorPosition, setDialValue]
54232
54255
  );
54233
54256
  const handleClear = useCallback(() => {
54234
54257
  setCallError(null);
54235
54258
  setDialValue("");
54259
+ setCursorPosition(0);
54260
+ setTimeout(() => {
54261
+ if (visibleInputRef.current) {
54262
+ visibleInputRef.current.focus();
54263
+ }
54264
+ }, 0);
54236
54265
  }, [setDialValue]);
54237
54266
  const handleBackspace = useCallback(() => {
54238
54267
  setCallError(null);
54239
- setDialValue((prev) => prev.slice(0, -1));
54240
- }, [setDialValue]);
54268
+ const capturedPos = cursorPosition;
54269
+ if (capturedPos === 0) {
54270
+ return;
54271
+ }
54272
+ setDialValue((prevValue) => {
54273
+ const newValue = prevValue.slice(0, capturedPos - 1) + prevValue.slice(capturedPos);
54274
+ const newCursorPos = capturedPos - 1;
54275
+ setCursorPosition(newCursorPos);
54276
+ setTimeout(() => {
54277
+ if (visibleInputRef.current) {
54278
+ visibleInputRef.current.focus();
54279
+ visibleInputRef.current.setSelectionRange(newCursorPos, newCursorPos);
54280
+ }
54281
+ }, 0);
54282
+ return newValue;
54283
+ });
54284
+ }, [setDialValue, cursorPosition]);
54241
54285
  const handleCall = useCallback(async () => {
54242
54286
  if (!isIdle && !isOnBreak) {
54243
54287
  return;
54244
54288
  }
54245
- if (!normalizedNumber) {
54289
+ if (!sanitizedDialValue) {
54246
54290
  setCallError("Informe um numero para ligar.");
54247
54291
  return;
54248
54292
  }
54249
- if (!isNumberValid) {
54250
- setCallError("Numero invalido. Digite apenas o numero local, sem o +55.");
54293
+ const normalizedLocal = normalizePhoneNumberPaste(sanitizedDialValue);
54294
+ console.log("[handleCall] sanitizedDialValue:", sanitizedDialValue);
54295
+ console.log("[handleCall] normalizedLocal após normalizePhoneNumberPaste:", normalizedLocal);
54296
+ const e164Number = normalizeToE164(normalizedLocal, DEFAULT_COUNTRY_PREFIX);
54297
+ console.log("[handleCall] e164Number após normalizeToE164:", e164Number);
54298
+ if (!e164Number) {
54299
+ setCallError("Informe um numero para ligar.");
54251
54300
  return;
54252
54301
  }
54302
+ const isValid2 = isValidPhoneNumberSafe(e164Number);
54303
+ console.log("[handleCall] isValidPhoneNumberSafe:", isValid2);
54304
+ if (!isValid2) {
54305
+ setCallError("Numero invalido. Digite um numero de celular valido com DDD.");
54306
+ return;
54307
+ }
54308
+ console.log("[handleCall] 🚀 Ligando para:", e164Number);
54253
54309
  try {
54254
54310
  setCallError(null);
54255
- setLastDialedNumber(normalizedNumber);
54256
- await makeManualCall(normalizedNumber);
54311
+ setLastDialedNumber(e164Number);
54312
+ void makeManualCall(e164Number);
54257
54313
  } catch (error2) {
54258
54314
  console.error("Erro ao iniciar chamada", error2);
54259
54315
  setCallError("Nao foi possivel iniciar a ligacao.");
54260
54316
  }
54261
- }, [isIdle, isOnBreak, normalizedNumber, isNumberValid, makeManualCall]);
54317
+ }, [isIdle, isOnBreak, sanitizedDialValue, makeManualCall]);
54262
54318
  const handleGoOnBreak = useCallback(async (breakId) => {
54263
54319
  try {
54264
54320
  setBreakError(null);
@@ -54280,7 +54336,17 @@ function AuthenticatedDialer({ username, dialValue, setDialValue, onLogout, vari
54280
54336
  }
54281
54337
  }, [goOffBreak]);
54282
54338
  useEffect(() => {
54283
- if (!isMiniMode && !isDialerFocused) {
54339
+ if (visibleInputRef.current && sanitizedDialValue.length < cursorPosition) {
54340
+ const newPos = sanitizedDialValue.length;
54341
+ setCursorPosition(newPos);
54342
+ visibleInputRef.current.setSelectionRange(newPos, newPos);
54343
+ }
54344
+ }, [sanitizedDialValue, cursorPosition]);
54345
+ useEffect(() => {
54346
+ if (isMiniMode) {
54347
+ return;
54348
+ }
54349
+ if (!isDialerFocused) {
54284
54350
  return;
54285
54351
  }
54286
54352
  const handleKeyDown = (event2) => {
@@ -54406,6 +54472,13 @@ function AuthenticatedDialer({ username, dialValue, setDialValue, onLogout, vari
54406
54472
  }
54407
54473
  const normalized = normalizePhoneNumberPaste(pastedText);
54408
54474
  setDialValue(normalized);
54475
+ setCursorPosition(normalized.length);
54476
+ setTimeout(() => {
54477
+ if (visibleInputRef.current) {
54478
+ visibleInputRef.current.focus();
54479
+ visibleInputRef.current.setSelectionRange(normalized.length, normalized.length);
54480
+ }
54481
+ }, 0);
54409
54482
  }, [setDialValue]);
54410
54483
  const handleSelectInputDevice = useCallback((event2) => {
54411
54484
  const value = event2.target.value;
@@ -54671,15 +54744,86 @@ function AuthenticatedDialer({ username, dialValue, setDialValue, onLogout, vari
54671
54744
  ] }) }) : /* @__PURE__ */ jsxRuntimeExports.jsxs(
54672
54745
  "div",
54673
54746
  {
54674
- className: tw`space-y-1 rounded-xl border border-slate-200 bg-slate-50 px-3 py-2 cursor-text`,
54675
- onClick: () => {
54676
- var _a3;
54677
- return (_a3 = hiddenInputRef.current) == null ? void 0 : _a3.focus();
54678
- },
54747
+ className: tw`space-y-1 rounded-xl border border-slate-200 bg-slate-50 px-3 py-2`,
54679
54748
  children: [
54680
54749
  /* @__PURE__ */ jsxRuntimeExports.jsx("span", { className: tw`text-[10px] font-semibold uppercase tracking-wide text-slate-500`, children: "Numero" }),
54681
54750
  /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: tw`flex items-center gap-2`, children: [
54682
- /* @__PURE__ */ jsxRuntimeExports.jsx("span", { className: tw`flex-1 truncate text-sm font-semibold text-slate-800`, children: dialDisplayText || /* @__PURE__ */ jsxRuntimeExports.jsx("span", { className: tw`text-slate-400 text-xs font-normal`, children: "Digite ou cole um numero" }) }),
54751
+ /* @__PURE__ */ jsxRuntimeExports.jsx(
54752
+ "input",
54753
+ {
54754
+ ref: visibleInputRef,
54755
+ type: "text",
54756
+ inputMode: "tel",
54757
+ value: sanitizedDialValue,
54758
+ onChange: (e) => {
54759
+ const input = e.target;
54760
+ setCursorPosition(input.selectionStart || 0);
54761
+ },
54762
+ onClick: (e) => {
54763
+ setCursorPosition(e.target.selectionStart || 0);
54764
+ },
54765
+ onKeyUp: (e) => {
54766
+ setCursorPosition(e.target.selectionStart || 0);
54767
+ },
54768
+ onSelect: (e) => {
54769
+ setCursorPosition(e.target.selectionStart || 0);
54770
+ },
54771
+ onKeyDown: (e) => {
54772
+ if (!miniCanUseKeypad) {
54773
+ e.preventDefault();
54774
+ return;
54775
+ }
54776
+ const key = e.key;
54777
+ const input = e.target;
54778
+ const currentPos = input.selectionStart || 0;
54779
+ setCursorPosition(currentPos);
54780
+ if (key === "Backspace") {
54781
+ e.preventDefault();
54782
+ handleBackspace();
54783
+ return;
54784
+ }
54785
+ if (key === "Delete") {
54786
+ e.preventDefault();
54787
+ const capturedPos = currentPos;
54788
+ setDialValue((prevValue) => {
54789
+ if (capturedPos < prevValue.length) {
54790
+ const newValue = prevValue.slice(0, capturedPos) + prevValue.slice(capturedPos + 1);
54791
+ setTimeout(() => {
54792
+ if (visibleInputRef.current) {
54793
+ visibleInputRef.current.setSelectionRange(capturedPos, capturedPos);
54794
+ setCursorPosition(capturedPos);
54795
+ }
54796
+ }, 0);
54797
+ return newValue;
54798
+ }
54799
+ return prevValue;
54800
+ });
54801
+ return;
54802
+ }
54803
+ if (key === "Enter") {
54804
+ e.preventDefault();
54805
+ void handleCall();
54806
+ return;
54807
+ }
54808
+ if (["ArrowLeft", "ArrowRight", "Home", "End"].includes(key)) {
54809
+ return;
54810
+ }
54811
+ if ((e.ctrlKey || e.metaKey) && ["a", "c", "v", "x"].includes(key.toLowerCase())) {
54812
+ return;
54813
+ }
54814
+ if (/^[0-9*#]$/.test(key)) {
54815
+ e.preventDefault();
54816
+ handleDial(key);
54817
+ return;
54818
+ }
54819
+ e.preventDefault();
54820
+ },
54821
+ onPaste: handlePaste,
54822
+ disabled: !miniCanUseKeypad,
54823
+ placeholder: "Digite ou cole um numero",
54824
+ className: tw`flex-1 bg-transparent text-sm font-semibold text-slate-800 outline-none placeholder:text-slate-400 placeholder:text-xs placeholder:font-normal disabled:opacity-60 disabled:cursor-not-allowed`
54825
+ }
54826
+ ),
54683
54827
  /* @__PURE__ */ jsxRuntimeExports.jsx(
54684
54828
  "button",
54685
54829
  {
@@ -55172,7 +55316,15 @@ function normalizeToE164(rawValue, defaultPrefix) {
55172
55316
  if (!prefixDigits) {
55173
55317
  return `+${digits}`;
55174
55318
  }
55175
- return `+${prefixDigits}${digits}`;
55319
+ let phoneNumber = digits;
55320
+ if (prefixDigits === "55" && digits.length >= 10) {
55321
+ const ddd = digits.substring(0, 2);
55322
+ const number = digits.substring(2);
55323
+ if (number.length === 8 && !["2", "3", "4"].includes(number[0])) {
55324
+ phoneNumber = `${ddd}9${number}`;
55325
+ }
55326
+ }
55327
+ return `+${prefixDigits}${phoneNumber}`;
55176
55328
  }
55177
55329
  function isValidPhoneNumberSafe(value) {
55178
55330
  try {