@hook-sdk/template 0.19.0 → 0.20.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.
package/dist/index.cjs CHANGED
@@ -352,7 +352,7 @@ var FALLBACK_PAYWALL = {
352
352
  };
353
353
  var isMethodAvailable = (availability, method) => availability[method] !== false;
354
354
  function usePaywallState() {
355
- const { subscription, plan } = (0, import_sdk3.useHook)();
355
+ const { subscription, plan, authStatus, track: track2 } = (0, import_sdk3.useHook)();
356
356
  const configFromCtx = (0, import_react6.useContext)(AppConfigContext);
357
357
  const paywall = configFromCtx?.paywall ?? FALLBACK_PAYWALL;
358
358
  const isFree = paywall.mode === "free";
@@ -441,6 +441,21 @@ function usePaywallState() {
441
441
  [useDefaultMessages]
442
442
  );
443
443
  const submit = (0, import_react6.useCallback)(async () => {
444
+ if (authStatus === "loading") return void 0;
445
+ if (authStatus !== "authenticated") {
446
+ track2("unauthenticated_submit_attempted", {
447
+ method: selectedMethod,
448
+ cycle,
449
+ cpf_valid: cpfValid
450
+ });
451
+ return void 0;
452
+ }
453
+ track2("payment_attempted", {
454
+ method: selectedMethod,
455
+ cycle,
456
+ cpf_valid: cpfValid,
457
+ selected_amount_cents: cycle === "YEARLY" ? plan.data?.yearlyPriceCents ?? (isFree ? 0 : paywall.prices.yearlyCents) : plan.data?.priceCents ?? (isFree ? 0 : paywall.prices.monthlyCents)
458
+ });
444
459
  setSubmitting(true);
445
460
  setError(null);
446
461
  const methodToUse = selectedMethod;
@@ -501,7 +516,7 @@ function usePaywallState() {
501
516
  setSubmitting(false);
502
517
  return void 0;
503
518
  }
504
- }, [selectedMethod, availability, subscription, cycle, cpf, card, buildError]);
519
+ }, [authStatus, track2, selectedMethod, availability, subscription, cycle, cpf, cpfValid, card, buildError, plan, paywall]);
505
520
  const checkout = (0, import_react6.useCallback)(
506
521
  async (args) => {
507
522
  setSubmitting(true);
@@ -2099,15 +2114,21 @@ var BACKOFF_MS = [2e3, 5e3, 1e4, 2e4, 4e4];
2099
2114
  var MAX_CYCLES = 3;
2100
2115
  var SUPPORT_MAILTO = "mailto:suporte@usehook.net?subject=Pagamento%20pendente";
2101
2116
  function PaymentReturnHandler({ children }) {
2102
- const { subscription } = (0, import_sdk6.useHook)();
2117
+ const { subscription, track: track2 } = (0, import_sdk6.useHook)();
2103
2118
  const subRef = (0, import_react13.useRef)(subscription);
2104
2119
  subRef.current = subscription;
2105
2120
  const runIdRef = (0, import_react13.useRef)(0);
2106
2121
  const cyclesRef = (0, import_react13.useRef)(0);
2122
+ const startMsRef = (0, import_react13.useRef)(0);
2107
2123
  const [state, setState] = (0, import_react13.useState)("idle");
2108
2124
  const runPoll = (0, import_react13.useCallback)(() => {
2109
2125
  const runId = ++runIdRef.current;
2126
+ const isFirstRun = cyclesRef.current === 0;
2110
2127
  cyclesRef.current += 1;
2128
+ if (isFirstRun) {
2129
+ startMsRef.current = Date.now();
2130
+ track2("payment_confirmation_started", {});
2131
+ }
2111
2132
  setState("confirming");
2112
2133
  let attempts = 0;
2113
2134
  const tick = async () => {
@@ -2120,6 +2141,11 @@ function PaymentReturnHandler({ children }) {
2120
2141
  if (runIdRef.current !== runId) return;
2121
2142
  const status = subRef.current.status();
2122
2143
  if (status === "active" || status === "trialing") {
2144
+ track2("payment_confirmation_succeeded", {
2145
+ cycle_count: cyclesRef.current,
2146
+ attempt_count: attempts,
2147
+ duration_ms: Date.now() - startMsRef.current
2148
+ });
2123
2149
  const cleanUrl = new URL(window.location.href);
2124
2150
  cleanUrl.searchParams.delete("paymentReturn");
2125
2151
  window.history.replaceState({}, "", cleanUrl.toString());
@@ -2130,6 +2156,9 @@ function PaymentReturnHandler({ children }) {
2130
2156
  const delay = BACKOFF_MS[attempts - 1];
2131
2157
  if (delay === void 0) {
2132
2158
  if (cyclesRef.current >= MAX_CYCLES) {
2159
+ track2("payment_confirmation_timed_out", {
2160
+ total_duration_ms: Date.now() - startMsRef.current
2161
+ });
2133
2162
  setState("timeout");
2134
2163
  } else {
2135
2164
  setState("waiting");
@@ -2139,7 +2168,7 @@ function PaymentReturnHandler({ children }) {
2139
2168
  setTimeout(tick, delay);
2140
2169
  };
2141
2170
  void tick();
2142
- }, []);
2171
+ }, [track2]);
2143
2172
  (0, import_react13.useEffect)(() => {
2144
2173
  if (typeof window === "undefined") return;
2145
2174
  const url = new URL(window.location.href);
@@ -2482,27 +2511,11 @@ function usePush() {
2482
2511
 
2483
2512
  // src/components/PushPrompt.tsx
2484
2513
  var import_jsx_runtime22 = require("react/jsx-runtime");
2485
- function platformRecoveryCopy(texts) {
2486
- if (typeof navigator === "undefined") return null;
2487
- const ua = navigator.userAgent || "";
2488
- const platform = detectPlatform(ua);
2489
- switch (platform) {
2490
- case "ios-safari":
2491
- case "ios-other":
2492
- return texts.deniedRecoveryIos ?? null;
2493
- case "android":
2494
- return texts.deniedRecoveryAndroid ?? null;
2495
- case "desktop":
2496
- return texts.deniedRecoveryDesktop ?? null;
2497
- case "in-app":
2498
- return texts.deniedRecoveryInApp ?? null;
2499
- default:
2500
- return null;
2501
- }
2502
- }
2503
2514
  function PushPrompt2({ texts, onSubscribed, onDeclined, onInstallRequested, className }) {
2504
2515
  const { state, subscribe } = usePush();
2505
- if (state.kind === "subscribed" || state.kind === "dismissed") return null;
2516
+ if (state.kind === "denied" || state.kind === "dismissed" || state.kind === "subscribed") {
2517
+ return null;
2518
+ }
2506
2519
  if (state.kind === "ios_needs_install") {
2507
2520
  return /* @__PURE__ */ (0, import_jsx_runtime22.jsxs)("div", { className, role: "region", "aria-label": texts.iosInstallTitle, children: [
2508
2521
  /* @__PURE__ */ (0, import_jsx_runtime22.jsx)("h3", { children: texts.iosInstallTitle }),
@@ -2510,14 +2523,6 @@ function PushPrompt2({ texts, onSubscribed, onDeclined, onInstallRequested, clas
2510
2523
  onInstallRequested && texts.iosInstallCta && /* @__PURE__ */ (0, import_jsx_runtime22.jsx)("button", { onClick: onInstallRequested, children: texts.iosInstallCta })
2511
2524
  ] });
2512
2525
  }
2513
- if (state.kind === "denied") {
2514
- const recovery = platformRecoveryCopy(texts);
2515
- return /* @__PURE__ */ (0, import_jsx_runtime22.jsxs)("div", { className, role: "region", "aria-label": texts.deniedTitle, children: [
2516
- /* @__PURE__ */ (0, import_jsx_runtime22.jsx)("h3", { children: texts.deniedTitle }),
2517
- /* @__PURE__ */ (0, import_jsx_runtime22.jsx)("p", { children: texts.deniedBody }),
2518
- recovery && /* @__PURE__ */ (0, import_jsx_runtime22.jsx)("p", { "data-testid": "denied-recovery", children: recovery })
2519
- ] });
2520
- }
2521
2526
  if (state.kind === "unsupported") {
2522
2527
  return /* @__PURE__ */ (0, import_jsx_runtime22.jsx)("div", { className, role: "region", children: /* @__PURE__ */ (0, import_jsx_runtime22.jsx)("p", { children: texts.unsupportedBody }) });
2523
2528
  }