@hook-sdk/template 0.7.2 → 0.8.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
@@ -183,7 +183,8 @@ function usePaywallState() {
183
183
  method: "pix-auto",
184
184
  qrCodePayload: result.qrCodePayload,
185
185
  qrCodeBase64: result.qrCodeBase64,
186
- expiresAt: null
186
+ expiresAt: null,
187
+ paid: false
187
188
  });
188
189
  setOpening(false);
189
190
  return;
@@ -194,7 +195,8 @@ function usePaywallState() {
194
195
  method: "pix-once",
195
196
  qrCodePayload: result.qrCodePayload,
196
197
  qrCodeBase64: result.qrCodeBase64,
197
- expiresAt: result.expiresAt
198
+ expiresAt: result.expiresAt,
199
+ paid: false
198
200
  });
199
201
  setOpening(false);
200
202
  return;
@@ -218,7 +220,7 @@ function usePaywallState() {
218
220
  const subRef = (0, import_react3.useRef)(subscription);
219
221
  subRef.current = subscription;
220
222
  (0, import_react3.useEffect)(() => {
221
- if (!pixPending) return;
223
+ if (!pixPending || pixPending.paid) return;
222
224
  let attempts = 0;
223
225
  const MAX_ATTEMPTS = 60;
224
226
  let cancelled = false;
@@ -230,7 +232,7 @@ function usePaywallState() {
230
232
  if (cancelled) return;
231
233
  const s = subRef.current.status();
232
234
  if (s === "active" || s === "trialing") {
233
- setPixPending(null);
235
+ setPixPending((prev) => prev ? { ...prev, paid: true } : prev);
234
236
  return;
235
237
  }
236
238
  } catch {
@@ -276,14 +278,34 @@ function SubscriptionGate({ Paywall, children }) {
276
278
  var import_react4 = require("react");
277
279
  var import_sdk3 = require("@hook-sdk/sdk");
278
280
  var import_jsx_runtime6 = require("react/jsx-runtime");
281
+ var SAFETY_TIMEOUT_MS = 3e3;
279
282
  function PersistedKeysPrefetch({ children }) {
280
283
  const { appData } = (0, import_sdk3.useHook)();
281
284
  const config = useTemplateConfig();
285
+ const hasKeys = !!config.persistedKeys && config.persistedKeys.length > 0;
286
+ const [ready, setReady] = (0, import_react4.useState)(!hasKeys);
282
287
  (0, import_react4.useEffect)(() => {
283
288
  const keys = config.persistedKeys;
284
- if (!keys || keys.length === 0) return;
289
+ if (!keys || keys.length === 0) {
290
+ setReady(true);
291
+ return;
292
+ }
293
+ let cancelled = false;
285
294
  appData.cache.startPrefetch(keys, (ks) => appData.bulkRead(ks));
295
+ void appData.cache.waitForPrimed(SAFETY_TIMEOUT_MS).then((result) => {
296
+ if (cancelled) return;
297
+ if (result === "timeout") {
298
+ console.warn(
299
+ `[@hook-sdk/template] PersistedKeysPrefetch: bulk-read n\xE3o completou em ${SAFETY_TIMEOUT_MS}ms. Liberando render mesmo assim \u2014 usePersistedState pode expor defaultValue at\xE9 o fetch terminar (G77).`
300
+ );
301
+ }
302
+ setReady(true);
303
+ });
304
+ return () => {
305
+ cancelled = true;
306
+ };
286
307
  }, [appData, config.persistedKeys]);
308
+ if (!ready) return null;
287
309
  return /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(import_jsx_runtime6.Fragment, { children });
288
310
  }
289
311