@microsurveysai/react-native 0.1.6 → 0.1.8

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/README.md CHANGED
@@ -59,7 +59,7 @@ import { MicroSurvey } from "@microsurveysai/react-native";
59
59
  | --- | --- | --- |
60
60
  | `apiKey` | — | Your project API key. |
61
61
  | `apiBaseURL` | `https://console.microsurveys.ai` | For self-hosted deployments. |
62
- | `refreshOnForeground` | `true` | On app foreground, retry queued ingest and refresh a stale config. |
62
+ | `refreshOnForeground` | `true` | On app foreground, refresh a stale config. (Queued impressions and responses are retried on every foreground, and every few seconds while offline, either way.) |
63
63
  | `configTTLms` | 12h | How long the cached config counts as fresh. |
64
64
  | `storage` | AsyncStorage | A synchronous store (e.g. MMKV) to persist into instead. |
65
65
  | `appVersion` | — | Your app's version (e.g. `"3.2.0"`), sent with responses so results can be compared across releases. With Expo, pass `Application.nativeApplicationVersion` from `expo-application`. |
package/dist/index.cjs CHANGED
@@ -221,6 +221,7 @@ function normalizeSurvey(raw) {
221
221
  dismissible: typeof raw.dismissible === "boolean" ? raw.dismissible : null,
222
222
  presentation: strOrUndef(raw.presentation) ?? null,
223
223
  successMessage: strOrUndef(raw.successMessage) ?? null,
224
+ successPace: raw.successPace === "slow" || raw.successPace === "fast" ? raw.successPace : null,
224
225
  platforms: Array.isArray(raw.platforms) ? raw.platforms.filter((p) => typeof p === "string") : null
225
226
  };
226
227
  }
@@ -250,12 +251,24 @@ function parseConfig(rawText) {
250
251
  // src/core/apiClient.ts
251
252
  var API_VERSION = "2026-06-30";
252
253
  var PENDING_PAGE_SIZE = 50;
254
+ var RETRY_BASE_MS = 2e3;
255
+ var RETRY_MAX_MS = 3e4;
256
+ var INGEST_TIMEOUT_MS = 15e3;
253
257
  var ApiClient = class {
254
258
  constructor(apiKey, baseURL, storage) {
255
259
  __publicField(this, "apiKey");
256
260
  __publicField(this, "baseURL");
257
261
  __publicField(this, "outbox");
258
262
  __publicField(this, "isFlushing", false);
263
+ /** The flush in progress, returned to callers that ask for another meanwhile. */
264
+ __publicField(this, "flushPromise", Promise.resolve());
265
+ /** Set when flush() is called during a flush: run another pass before finishing. */
266
+ __publicField(this, "flushAgain", false);
267
+ __publicField(this, "retryTimer", null);
268
+ /** Failed flushes in a row, for the retry backoff. */
269
+ __publicField(this, "failures", 0);
270
+ /** After `cancelRetry()`: no new retry timers until the next `resumeRetry()`. */
271
+ __publicField(this, "retryStopped", false);
259
272
  this.apiKey = apiKey;
260
273
  this.outbox = new Outbox(storage);
261
274
  this.baseURL = baseURL.replace(/\/+$/, "");
@@ -307,9 +320,11 @@ var ApiClient = class {
307
320
  }
308
321
  // ── Impressions / responses ─────────────────────────────────────────────────
309
322
  /** Queues the impression and returns its `clientId`, so the response from the
310
- * same showing can point back to it. */
323
+ * same showing can point back to it. Pass the `clientId` to send the same
324
+ * impression again (e.g. with `dismissed: true` once the user dismissed it):
325
+ * the server updates the one it has. */
311
326
  recordImpression(input) {
312
- const clientId = uuid();
327
+ const clientId = input.clientId ?? uuid();
313
328
  const payload = {
314
329
  clientId,
315
330
  surveyId: input.surveyId,
@@ -355,46 +370,94 @@ var ApiClient = class {
355
370
  }
356
371
  // ── Flush ────────────────────────────────────────────────────────────────────
357
372
  /** POSTs queued entries in order. Stops at the first retryable failure (likely
358
- * offline) and leaves the rest for next time. Entries queued while a flush is
359
- * running are sent by the same flush, so nothing waits for the next event.
360
- * Idempotency keys make re-sends safe. Never throws. */
361
- async flush() {
362
- if (this.isFlushing) return;
373
+ * offline) and schedules a retry (2 s, doubling, capped at 30 s). Entries
374
+ * queued while a flush is running are sent by the same flush, so nothing waits
375
+ * for the next event. Idempotency keys make re-sends safe. Resolves when the
376
+ * flush in progress is done; never throws. */
377
+ flush() {
378
+ if (this.isFlushing) {
379
+ this.flushAgain = true;
380
+ return this.flushPromise;
381
+ }
363
382
  this.isFlushing = true;
383
+ this.clearRetryTimer();
384
+ this.flushPromise = this.runFlush();
385
+ return this.flushPromise;
386
+ }
387
+ /** Stops retrying (the client is stopping). Queued entries stay for later. */
388
+ cancelRetry() {
389
+ this.retryStopped = true;
390
+ this.clearRetryTimer();
391
+ }
392
+ /** Allows retries again after `cancelRetry()`. */
393
+ resumeRetry() {
394
+ this.retryStopped = false;
395
+ }
396
+ async runFlush() {
397
+ let failed = false;
398
+ const sent = /* @__PURE__ */ new Set();
364
399
  try {
365
- const sent = /* @__PURE__ */ new Set();
366
- for (; ; ) {
367
- const pending = this.outbox.snapshot().filter((e) => !sent.has(e.id));
368
- if (pending.length === 0) break;
369
- let failed = false;
370
- for (const entry of pending) {
371
- try {
372
- await this.post(entry.path, entry.body);
373
- this.outbox.remove(entry.id);
374
- sent.add(entry.id);
375
- } catch {
376
- failed = true;
377
- break;
400
+ do {
401
+ this.flushAgain = false;
402
+ for (; ; ) {
403
+ const pending = this.outbox.snapshot().filter((e) => !sent.has(e.id));
404
+ if (pending.length === 0) break;
405
+ for (const entry of pending) {
406
+ try {
407
+ await this.post(entry.path, entry.body);
408
+ this.outbox.remove(entry.id);
409
+ sent.add(entry.id);
410
+ } catch {
411
+ failed = true;
412
+ break;
413
+ }
378
414
  }
415
+ if (failed) break;
379
416
  }
380
- if (failed) break;
381
- }
417
+ } while (!failed && this.flushAgain);
382
418
  } finally {
419
+ this.flushAgain = false;
383
420
  this.isFlushing = false;
384
421
  }
422
+ if (failed) {
423
+ this.failures += 1;
424
+ this.scheduleRetry();
425
+ } else {
426
+ this.failures = 0;
427
+ }
428
+ }
429
+ /** One pending retry at most. */
430
+ scheduleRetry() {
431
+ if (this.retryStopped || this.retryTimer) return;
432
+ const delay = Math.min(RETRY_BASE_MS * 2 ** Math.max(0, this.failures - 1), RETRY_MAX_MS);
433
+ this.retryTimer = setTimeout(() => {
434
+ this.retryTimer = null;
435
+ void this.flush();
436
+ }, delay);
437
+ }
438
+ clearRetryTimer() {
439
+ if (this.retryTimer) clearTimeout(this.retryTimer);
440
+ this.retryTimer = null;
385
441
  }
386
442
  async post(path, body) {
387
- const res = await fetch(this.endpoint(path), {
388
- method: "POST",
389
- headers: {
390
- Authorization: `Bearer ${this.apiKey}`,
391
- "MS-Api-Version": API_VERSION,
392
- "Content-Type": "application/json; charset=utf-8"
393
- },
394
- body
395
- });
396
- if (res.status === 429 || res.status >= 500 && res.status < 600) {
397
- throw new Error(`retryable ${res.status}`);
443
+ const controller = typeof AbortController !== "undefined" ? new AbortController() : null;
444
+ const timeout = controller ? setTimeout(() => controller.abort(), INGEST_TIMEOUT_MS) : null;
445
+ try {
446
+ const res = await fetch(this.endpoint(path), {
447
+ method: "POST",
448
+ headers: {
449
+ Authorization: `Bearer ${this.apiKey}`,
450
+ "MS-Api-Version": API_VERSION,
451
+ "Content-Type": "application/json; charset=utf-8"
452
+ },
453
+ body,
454
+ signal: controller?.signal
455
+ });
456
+ if (res.status === 429 || res.status >= 500 && res.status < 600) {
457
+ throw new Error(`retryable ${res.status}`);
458
+ }
459
+ } finally {
460
+ if (timeout) clearTimeout(timeout);
398
461
  }
399
462
  }
400
463
  endpoint(path) {
@@ -897,7 +960,7 @@ function debugProperties(properties) {
897
960
  }
898
961
 
899
962
  // src/core/version.ts
900
- var SDK_VERSION = "0.1.6";
963
+ var SDK_VERSION = "0.1.8";
901
964
 
902
965
  // src/core/client.ts
903
966
  var DEFAULT_BASE_URL = "https://console.microsurveys.ai";
@@ -962,15 +1025,14 @@ var MicroSurveysClient = class {
962
1025
  start() {
963
1026
  if (this.started) return;
964
1027
  this.started = true;
1028
+ this.api.resumeRetry();
965
1029
  void this.api.flush();
966
1030
  void this.refreshConfig().then(() => {
967
1031
  if (this.started) this.startPendingChecks();
968
1032
  });
969
1033
  this.unsubscribeForeground = this.subscribeForeground(() => {
970
- if (this.refreshOnForeground) {
971
- void this.api.flush();
972
- this.refreshConfigIfStale();
973
- }
1034
+ void this.api.flush();
1035
+ if (this.refreshOnForeground) this.refreshConfigIfStale();
974
1036
  this.startPendingChecks();
975
1037
  });
976
1038
  this.unsubscribeBackground = this.subscribeBackground?.(() => {
@@ -990,6 +1052,7 @@ var MicroSurveysClient = class {
990
1052
  this.unsubscribeBackground = null;
991
1053
  this.unsubscribeForeground?.();
992
1054
  this.unsubscribeForeground = null;
1055
+ this.api.cancelRetry();
993
1056
  this.started = false;
994
1057
  }
995
1058
  /** Force an ETag-aware config refresh; keeps the cache on failure. */
@@ -1167,49 +1230,81 @@ var MicroSurveysClient = class {
1167
1230
  return;
1168
1231
  }
1169
1232
  this.handleDecision({ surveyId: survey.id, triggerId: trigger.id, reason: "shown" });
1170
- const p = { survey, trigger, identity, shownAt: /* @__PURE__ */ new Date(), closed: false };
1233
+ const p = this.newPresentation(survey, trigger, identity);
1171
1234
  this.active = p;
1172
1235
  const active = this.toActiveSurvey(p);
1173
1236
  this.presentListeners.forEach((l) => l(active));
1174
1237
  }
1238
+ newPresentation(survey, trigger, identity) {
1239
+ return { survey, trigger, identity, impressionId: uuid(), presentedAt: /* @__PURE__ */ new Date(), impression: null, closed: false };
1240
+ }
1175
1241
  toActiveSurvey(p) {
1176
1242
  return {
1177
1243
  survey: p.survey,
1178
1244
  trigger: p.trigger,
1245
+ shown: () => this.markShown(p),
1179
1246
  close: (result) => {
1180
1247
  if (p.closed) return;
1181
1248
  p.closed = true;
1182
- this.recordOutcome(p.survey.id, p.trigger?.id ?? null, p.identity, p.shownAt, result);
1183
1249
  if (this.active === p) this.active = null;
1250
+ if (result) this.recordOutcome(p, result);
1184
1251
  }
1185
1252
  };
1186
1253
  }
1187
- /** Records the outcome of an **inline** `<MicroSurvey>` (no trigger/gate). */
1188
- recordInline(survey, result, shownAt) {
1189
- const identity = this.identityStore.snapshot();
1190
- this.recordOutcome(survey.id, null, identity, shownAt, result);
1254
+ /** Queues the impression the moment the survey is on screen, so it counts even
1255
+ * if the app is killed before the survey closes. */
1256
+ markShown(p) {
1257
+ if (p.impression || p.closed) return;
1258
+ p.impression = {
1259
+ clientId: p.impressionId,
1260
+ surveyId: p.survey.id,
1261
+ triggerId: p.trigger?.id ?? null,
1262
+ endUserId: endUserIdOf(p.identity),
1263
+ shownAt: /* @__PURE__ */ new Date(),
1264
+ dismissed: false,
1265
+ context: this.deviceContext()
1266
+ };
1267
+ this.api.recordImpression(p.impression);
1191
1268
  }
1192
- recordOutcome(surveyId, triggerId, identity, shownAt, result) {
1193
- const endUserId = endUserIdOf(identity);
1194
- const context = this.deviceContext();
1195
- const impressionId = this.api.recordImpression({
1196
- surveyId,
1197
- triggerId,
1198
- endUserId,
1199
- shownAt,
1200
- dismissed: result.dismissed,
1201
- context
1202
- });
1269
+ /** An **inline** `<MicroSurvey>` (no trigger, no gate, not "the" active survey).
1270
+ * Call `shown` when it renders and `close` with its outcome. */
1271
+ presentInline(survey) {
1272
+ return this.toActiveSurvey(this.newPresentation(survey, null, this.identityStore.snapshot()));
1273
+ }
1274
+ /** Records the outcome of an inline survey in one go, at close. Prefer
1275
+ * `presentInline`, which sends the impression when the survey appears. */
1276
+ recordInline(survey, result, shownAt) {
1277
+ const p = this.newPresentation(survey, null, this.identityStore.snapshot());
1278
+ p.presentedAt = shownAt;
1279
+ p.closed = true;
1280
+ this.recordOutcome(p, result);
1281
+ }
1282
+ recordOutcome(p, result) {
1283
+ let impression = p.impression;
1284
+ if (!impression) {
1285
+ impression = {
1286
+ clientId: p.impressionId,
1287
+ surveyId: p.survey.id,
1288
+ triggerId: p.trigger?.id ?? null,
1289
+ endUserId: endUserIdOf(p.identity),
1290
+ shownAt: p.presentedAt,
1291
+ dismissed: result.dismissed,
1292
+ context: this.deviceContext()
1293
+ };
1294
+ this.api.recordImpression(impression);
1295
+ } else if (result.dismissed) {
1296
+ this.api.recordImpression({ ...impression, dismissed: true });
1297
+ }
1203
1298
  if (result.completed || result.answers.length > 0) {
1204
1299
  this.api.recordResponse({
1205
- surveyId,
1206
- endUserId,
1300
+ surveyId: p.survey.id,
1301
+ endUserId: impression.endUserId,
1207
1302
  completed: result.completed,
1208
1303
  submittedAt: /* @__PURE__ */ new Date(),
1209
- userProps: identity.userProperties,
1210
- impressionId,
1211
- triggerId,
1212
- context,
1304
+ userProps: p.identity.userProperties,
1305
+ impressionId: p.impressionId,
1306
+ triggerId: p.trigger?.id ?? null,
1307
+ context: impression.context,
1213
1308
  answers: result.answers
1214
1309
  });
1215
1310
  }
@@ -1229,6 +1324,19 @@ function createClient(opts) {
1229
1324
  return new MicroSurveysClient(opts);
1230
1325
  }
1231
1326
 
1327
+ // src/core/thanks.ts
1328
+ var DEFAULT_SUCCESS_MESSAGE = "Thanks!";
1329
+ function thanksMessage(message) {
1330
+ const trimmed = message?.trim();
1331
+ return trimmed ? trimmed : DEFAULT_SUCCESS_MESSAGE;
1332
+ }
1333
+ function thanksDurationMs(message, pace) {
1334
+ const chars = [...thanksMessage(message)].length;
1335
+ const base = Math.min(Math.max(1e3 + 50 * chars, 1300), 7e3);
1336
+ const percent = pace === "slow" ? 150 : pace === "fast" ? 70 : 100;
1337
+ return Math.round(base * percent / 100);
1338
+ }
1339
+
1232
1340
  // src/amplitude.ts
1233
1341
  function createAmplitudePlugin(forwarder) {
1234
1342
  return {
@@ -1419,7 +1527,23 @@ var MicroSurveys = class {
1419
1527
  unsubscribe?.();
1420
1528
  };
1421
1529
  }
1422
- /** Records the outcome of an inline `<MicroSurvey>`. */
1530
+ /** Starts one showing of an inline `<MicroSurvey>`: call `shown` once it
1531
+ * renders (sends the impression) and `close` with its outcome. */
1532
+ presentInline(survey) {
1533
+ let inner = null;
1534
+ const withShowing = (fn) => this.withClient((c) => {
1535
+ inner ?? (inner = c.presentInline(survey));
1536
+ fn(inner);
1537
+ });
1538
+ return {
1539
+ survey,
1540
+ trigger: null,
1541
+ shown: () => withShowing((s) => s.shown()),
1542
+ close: (result) => withShowing((s) => s.close(result))
1543
+ };
1544
+ }
1545
+ /** Records the outcome of an inline survey in one go, at close. Prefer
1546
+ * `presentInline`, which sends the impression when the survey appears. */
1423
1547
  recordInline(survey, result, shownAt) {
1424
1548
  this.withClient((c) => c.recordInline(survey, result, shownAt));
1425
1549
  }
@@ -1866,7 +1990,6 @@ var styles2 = import_react_native4.StyleSheet.create({
1866
1990
  // src/ui/SurveyCard.tsx
1867
1991
  var import_jsx_runtime3 = require("react/jsx-runtime");
1868
1992
  var CLIP_ROOM = 4;
1869
- var THANKS_MS = 1300;
1870
1993
  var SurveyCard = (0, import_react2.forwardRef)(function SurveyCard2({ survey, theme, variant, bottomInset, onResult, onClose }, ref) {
1871
1994
  const questions = (0, import_react2.useMemo)(() => [...survey.questions].sort((a, b) => a.order - b.order), [survey]);
1872
1995
  const dismissible = survey.dismissible !== false;
@@ -1877,9 +2000,27 @@ var SurveyCard = (0, import_react2.forwardRef)(function SurveyCard2({ survey, th
1877
2000
  const [height, setHeight] = (0, import_react2.useState)(0);
1878
2001
  const reported = (0, import_react2.useRef)(false);
1879
2002
  const closeTimer = (0, import_react2.useRef)(null);
2003
+ const closed = (0, import_react2.useRef)(false);
2004
+ const unmounted = (0, import_react2.useRef)(false);
1880
2005
  (0, import_react2.useEffect)(() => () => {
2006
+ unmounted.current = true;
1881
2007
  if (closeTimer.current) clearTimeout(closeTimer.current);
1882
2008
  }, []);
2009
+ function closeThanks() {
2010
+ if (closed.current) return;
2011
+ closed.current = true;
2012
+ if (closeTimer.current) clearTimeout(closeTimer.current);
2013
+ closeTimer.current = null;
2014
+ onClose();
2015
+ }
2016
+ function startThanksTimer() {
2017
+ const ms = thanksDurationMs(survey.successMessage, survey.successPace);
2018
+ const schedule = (screenReader) => {
2019
+ if (closed.current || unmounted.current || closeTimer.current) return;
2020
+ closeTimer.current = setTimeout(closeThanks, screenReader ? ms * 2 : ms);
2021
+ };
2022
+ import_react_native5.AccessibilityInfo.isScreenReaderEnabled().then(schedule, () => schedule(false));
2023
+ }
1883
2024
  const question = questions[index];
1884
2025
  const isLast = index >= questions.length - 1;
1885
2026
  const valid = question ? isAnswerValid(question, current) : false;
@@ -1909,7 +2050,7 @@ var SurveyCard = (0, import_react2.forwardRef)(function SurveyCard2({ survey, th
1909
2050
  }
1910
2051
  if (report(true)) {
1911
2052
  setDone(true);
1912
- closeTimer.current = setTimeout(onClose, THANKS_MS);
2053
+ startThanksTimer();
1913
2054
  }
1914
2055
  }
1915
2056
  const pad = theme.spacing + 4;
@@ -1917,13 +2058,22 @@ var SurveyCard = (0, import_react2.forwardRef)(function SurveyCard2({ survey, th
1917
2058
  const hasProgress = questions.length > 1;
1918
2059
  if (!question) return null;
1919
2060
  if (done) {
1920
- const custom = survey.successMessage?.trim();
1921
2061
  return (
1922
2062
  // Keep the card's height so the sheet doesn't jump when the content swaps.
1923
- /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_react_native5.View, { style: [styles3.thanks, { minHeight: height, padding: pad }], accessibilityLiveRegion: "polite", children: /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(FadeIn, { children: /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(import_react_native5.View, { style: styles3.thanksStack, children: [
1924
- /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_react_native5.View, { style: [styles3.check, { backgroundColor: theme.accent }], children: /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_react_native5.Text, { allowFontScaling: false, style: [styles3.checkGlyph, { color: theme.accentText }], children: "\u2713" }) }),
1925
- /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_react_native5.Text, { style: [promptStyle(theme), { color: theme.text, textAlign: "center", marginTop: theme.spacing }], children: custom || "Thanks!" })
1926
- ] }) }) })
2063
+ // Tapping the thanks closes it at once.
2064
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
2065
+ import_react_native5.Pressable,
2066
+ {
2067
+ onPress: closeThanks,
2068
+ accessibilityLiveRegion: "polite",
2069
+ accessibilityHint: "Closes the survey",
2070
+ style: [styles3.thanks, { minHeight: height, padding: pad }],
2071
+ children: /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(FadeIn, { children: /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(import_react_native5.View, { style: styles3.thanksStack, children: [
2072
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_react_native5.View, { style: [styles3.check, { backgroundColor: theme.accent }], children: /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_react_native5.Text, { allowFontScaling: false, style: [styles3.checkGlyph, { color: theme.accentText }], children: "\u2713" }) }),
2073
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_react_native5.Text, { style: [promptStyle(theme), { color: theme.text, textAlign: "center", marginTop: theme.spacing }], children: thanksMessage(survey.successMessage) })
2074
+ ] }) })
2075
+ }
2076
+ )
1927
2077
  );
1928
2078
  }
1929
2079
  return /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(import_react_native5.View, { onLayout: (e) => setHeight(e.nativeEvent.layout.height), style: styles3.card, children: [
@@ -2001,7 +2151,8 @@ var styles3 = import_react_native5.StyleSheet.create({
2001
2151
  scroll: { flexGrow: 0, flexShrink: 1 },
2002
2152
  button: { alignItems: "center", justifyContent: "center" },
2003
2153
  thanks: { alignItems: "center", justifyContent: "center" },
2004
- thanksStack: { alignItems: "center" },
2154
+ // Stretch so a long message wraps inside the card instead of overflowing it.
2155
+ thanksStack: { alignItems: "center", alignSelf: "stretch" },
2005
2156
  check: { width: 48, height: 48, borderRadius: 24, alignItems: "center", justifyContent: "center" },
2006
2157
  checkGlyph: { fontSize: 26, fontWeight: "700", lineHeight: 30 }
2007
2158
  });
@@ -2255,7 +2406,11 @@ function OverlayHost({ instance, fontFamily }) {
2255
2406
  }),
2256
2407
  [instance]
2257
2408
  );
2409
+ (0, import_react4.useEffect)(() => {
2410
+ current?.active.shown();
2411
+ }, [current]);
2258
2412
  function showNext() {
2413
+ current?.active.close();
2259
2414
  const next = queue.current.shift();
2260
2415
  if (next) {
2261
2416
  setCurrent({ active: next, id: nextId.current++ });
@@ -2301,7 +2456,7 @@ function MicroSurvey({ surveyId, onComplete, style }) {
2301
2456
  const scheme = (0, import_react_native7.useColorScheme)() === "dark" ? "dark" : "light";
2302
2457
  const [survey, setSurvey] = (0, import_react5.useState)(null);
2303
2458
  const [closed, setClosed] = (0, import_react5.useState)(false);
2304
- const shownAt = (0, import_react5.useRef)(null);
2459
+ const showing = (0, import_react5.useRef)(null);
2305
2460
  const result = (0, import_react5.useRef)(null);
2306
2461
  (0, import_react5.useEffect)(() => {
2307
2462
  if (!instance) return;
@@ -2309,8 +2464,14 @@ function MicroSurvey({ surveyId, onComplete, style }) {
2309
2464
  }, [instance, surveyId]);
2310
2465
  const projectTheme = instance?.getTheme();
2311
2466
  const theme = (0, import_react5.useMemo)(() => resolveTheme(projectTheme, { scheme, fontFamily }), [projectTheme, scheme, fontFamily]);
2467
+ const visible = !!instance && !!survey && !closed;
2468
+ if (visible && (!showing.current || showing.current.survey.id !== survey.id)) {
2469
+ showing.current = instance.presentInline(survey);
2470
+ }
2471
+ (0, import_react5.useEffect)(() => {
2472
+ if (visible) showing.current?.shown();
2473
+ }, [visible, survey]);
2312
2474
  if (!instance || !survey || closed) return null;
2313
- if (!shownAt.current) shownAt.current = /* @__PURE__ */ new Date();
2314
2475
  return /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(
2315
2476
  import_react_native7.View,
2316
2477
  {
@@ -2326,7 +2487,7 @@ function MicroSurvey({ surveyId, onComplete, style }) {
2326
2487
  variant: "inline",
2327
2488
  onResult: (r) => {
2328
2489
  result.current = r;
2329
- instance.recordInline(survey, r, shownAt.current ?? /* @__PURE__ */ new Date());
2490
+ showing.current?.close(r);
2330
2491
  },
2331
2492
  onClose: () => {
2332
2493
  setClosed(true);