@microsurveysai/react-native 0.1.5 → 0.1.7

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.d.cts CHANGED
@@ -203,12 +203,20 @@ interface CreateClientOptions {
203
203
  * server events mailbox timers. */
204
204
  subscribeBackground?: (onBackground: () => void) => () => void;
205
205
  }
206
- /** An auto-triggered survey the host should present. Call `close` exactly once
207
- * when the UI dismisses (completed, partial, or dismissed). */
206
+ /** A survey the host should present. Call `shown` once it is on screen (this
207
+ * sends the impression), then `close` once when the UI goes away (completed,
208
+ * partial, or dismissed). Both are safe to call more than once. */
208
209
  interface ActiveSurvey {
209
210
  survey: Survey;
211
+ /** The trigger that fired; null for an inline survey. */
210
212
  trigger: Trigger | null;
211
- close(result: SurveyResult): void;
213
+ /** The survey is on screen: queues its impression (`dismissed: false`). */
214
+ shown(): void;
215
+ /** The survey went away. A dismissal re-sends the impression with
216
+ * `dismissed: true`; answers are sent as a response. Without a result (the
217
+ * survey never got to show), nothing more is sent. If `shown` was never
218
+ * called, the impression is sent now, with the final `dismissed`. */
219
+ close(result?: SurveyResult | null): void;
212
220
  }
213
221
  type PresentListener = (active: ActiveSurvey) => void;
214
222
  type ConfigListener = () => void;
@@ -288,8 +296,16 @@ declare class MicroSurveysClient {
288
296
  * survey is already active, the new subscriber is notified immediately. */
289
297
  onPresent(cb: PresentListener): () => void;
290
298
  private handlePresent;
299
+ private newPresentation;
291
300
  private toActiveSurvey;
292
- /** Records the outcome of an **inline** `<MicroSurvey>` (no trigger/gate). */
301
+ /** Queues the impression the moment the survey is on screen, so it counts even
302
+ * if the app is killed before the survey closes. */
303
+ private markShown;
304
+ /** An **inline** `<MicroSurvey>` (no trigger, no gate, not "the" active survey).
305
+ * Call `shown` when it renders and `close` with its outcome. */
306
+ presentInline(survey: Survey): ActiveSurvey;
307
+ /** Records the outcome of an inline survey in one go, at close. Prefer
308
+ * `presentInline`, which sends the impression when the survey appears. */
293
309
  recordInline(survey: Survey, result: SurveyResult, shownAt: Date): void;
294
310
  private recordOutcome;
295
311
  /** Where this showing happened. The language is read at record time: the user
@@ -337,8 +353,9 @@ interface MicroSurveysOptions {
337
353
  apiKey: string;
338
354
  /** Override for self-hosted deployments. Defaults to https://console.microsurveys.ai. */
339
355
  apiBaseURL?: string;
340
- /** Refresh config (when stale) and retry queued ingest whenever the app returns
341
- * to the foreground. Default true. */
356
+ /** Refresh config (when stale) whenever the app returns to the foreground.
357
+ * Default true. (Queued impressions/responses are retried on every foreground
358
+ * either way.) */
342
359
  refreshOnForeground?: boolean;
343
360
  /** Foreground freshness window for the cached config, in ms. Default 12h. */
344
361
  configTTLms?: number;
@@ -386,7 +403,11 @@ declare class MicroSurveys {
386
403
  onConfigChange(cb: () => void): () => void;
387
404
  /** Subscribe to auto-triggered surveys. Returns an unsubscribe function. */
388
405
  onPresent(cb: (active: ActiveSurvey) => void): () => void;
389
- /** Records the outcome of an inline `<MicroSurvey>`. */
406
+ /** Starts one showing of an inline `<MicroSurvey>`: call `shown` once it
407
+ * renders (sends the impression) and `close` with its outcome. */
408
+ presentInline(survey: Survey): ActiveSurvey;
409
+ /** Records the outcome of an inline survey in one go, at close. Prefer
410
+ * `presentInline`, which sends the impression when the survey appears. */
390
411
  recordInline(survey: Survey, result: SurveyResult, shownAt: Date): void;
391
412
  }
392
413
 
package/dist/index.d.ts CHANGED
@@ -203,12 +203,20 @@ interface CreateClientOptions {
203
203
  * server events mailbox timers. */
204
204
  subscribeBackground?: (onBackground: () => void) => () => void;
205
205
  }
206
- /** An auto-triggered survey the host should present. Call `close` exactly once
207
- * when the UI dismisses (completed, partial, or dismissed). */
206
+ /** A survey the host should present. Call `shown` once it is on screen (this
207
+ * sends the impression), then `close` once when the UI goes away (completed,
208
+ * partial, or dismissed). Both are safe to call more than once. */
208
209
  interface ActiveSurvey {
209
210
  survey: Survey;
211
+ /** The trigger that fired; null for an inline survey. */
210
212
  trigger: Trigger | null;
211
- close(result: SurveyResult): void;
213
+ /** The survey is on screen: queues its impression (`dismissed: false`). */
214
+ shown(): void;
215
+ /** The survey went away. A dismissal re-sends the impression with
216
+ * `dismissed: true`; answers are sent as a response. Without a result (the
217
+ * survey never got to show), nothing more is sent. If `shown` was never
218
+ * called, the impression is sent now, with the final `dismissed`. */
219
+ close(result?: SurveyResult | null): void;
212
220
  }
213
221
  type PresentListener = (active: ActiveSurvey) => void;
214
222
  type ConfigListener = () => void;
@@ -288,8 +296,16 @@ declare class MicroSurveysClient {
288
296
  * survey is already active, the new subscriber is notified immediately. */
289
297
  onPresent(cb: PresentListener): () => void;
290
298
  private handlePresent;
299
+ private newPresentation;
291
300
  private toActiveSurvey;
292
- /** Records the outcome of an **inline** `<MicroSurvey>` (no trigger/gate). */
301
+ /** Queues the impression the moment the survey is on screen, so it counts even
302
+ * if the app is killed before the survey closes. */
303
+ private markShown;
304
+ /** An **inline** `<MicroSurvey>` (no trigger, no gate, not "the" active survey).
305
+ * Call `shown` when it renders and `close` with its outcome. */
306
+ presentInline(survey: Survey): ActiveSurvey;
307
+ /** Records the outcome of an inline survey in one go, at close. Prefer
308
+ * `presentInline`, which sends the impression when the survey appears. */
293
309
  recordInline(survey: Survey, result: SurveyResult, shownAt: Date): void;
294
310
  private recordOutcome;
295
311
  /** Where this showing happened. The language is read at record time: the user
@@ -337,8 +353,9 @@ interface MicroSurveysOptions {
337
353
  apiKey: string;
338
354
  /** Override for self-hosted deployments. Defaults to https://console.microsurveys.ai. */
339
355
  apiBaseURL?: string;
340
- /** Refresh config (when stale) and retry queued ingest whenever the app returns
341
- * to the foreground. Default true. */
356
+ /** Refresh config (when stale) whenever the app returns to the foreground.
357
+ * Default true. (Queued impressions/responses are retried on every foreground
358
+ * either way.) */
342
359
  refreshOnForeground?: boolean;
343
360
  /** Foreground freshness window for the cached config, in ms. Default 12h. */
344
361
  configTTLms?: number;
@@ -386,7 +403,11 @@ declare class MicroSurveys {
386
403
  onConfigChange(cb: () => void): () => void;
387
404
  /** Subscribe to auto-triggered surveys. Returns an unsubscribe function. */
388
405
  onPresent(cb: (active: ActiveSurvey) => void): () => void;
389
- /** Records the outcome of an inline `<MicroSurvey>`. */
406
+ /** Starts one showing of an inline `<MicroSurvey>`: call `shown` once it
407
+ * renders (sends the impression) and `close` with its outcome. */
408
+ presentInline(survey: Survey): ActiveSurvey;
409
+ /** Records the outcome of an inline survey in one go, at close. Prefer
410
+ * `presentInline`, which sends the impression when the survey appears. */
390
411
  recordInline(survey: Survey, result: SurveyResult, shownAt: Date): void;
391
412
  }
392
413
 
package/dist/index.js CHANGED
@@ -80,6 +80,7 @@ var MAX_SCALE_POINTS = 101;
80
80
  var MAX_SCALE_ABS = 1e3;
81
81
  var MAX_STARS = 100;
82
82
  var MAX_OPTIONS = 100;
83
+ var QUESTION_TYPES = ["NPS", "CES", "CSAT_STAR", "CSAT_EMOJI", "THUMBS", "SINGLE_CHOICE", "OPEN_TEXT"];
83
84
  function boundedScale(min, max) {
84
85
  const lo = Math.min(Math.max(min, -MAX_SCALE_ABS), MAX_SCALE_ABS);
85
86
  let hi = Math.min(Math.max(max, -MAX_SCALE_ABS), MAX_SCALE_ABS);
@@ -158,8 +159,14 @@ function normalizeSurvey(raw) {
158
159
  const id = strOrUndef(raw.id);
159
160
  const name = strOrUndef(raw.name);
160
161
  if (!id || name === void 0) return null;
161
- const questions = (Array.isArray(raw.questions) ? raw.questions : []).map((q) => normalizeQuestion(asObject(q))).filter((q) => q !== null);
162
- const triggers = (Array.isArray(raw.triggers) ? raw.triggers : []).map((t) => normalizeTrigger(asObject(t))).filter((t) => t !== null);
162
+ const rawQuestions = Array.isArray(raw.questions) ? raw.questions : [];
163
+ const rawTriggers = Array.isArray(raw.triggers) ? raw.triggers : [];
164
+ if (rawQuestions.some((q) => !QUESTION_TYPES.includes(asObject(q).type)) || rawTriggers.some((t) => {
165
+ const type = asObject(t).type;
166
+ return type != null && type !== "SINGLE" && type !== "SEQUENCE";
167
+ })) return null;
168
+ const questions = rawQuestions.map((q) => normalizeQuestion(asObject(q))).filter((q) => q !== null);
169
+ const triggers = rawTriggers.map((t) => normalizeTrigger(asObject(t))).filter((t) => t !== null);
163
170
  return {
164
171
  id,
165
172
  name,
@@ -202,12 +209,24 @@ function parseConfig(rawText) {
202
209
  // src/core/apiClient.ts
203
210
  var API_VERSION = "2026-06-30";
204
211
  var PENDING_PAGE_SIZE = 50;
212
+ var RETRY_BASE_MS = 2e3;
213
+ var RETRY_MAX_MS = 3e4;
214
+ var INGEST_TIMEOUT_MS = 15e3;
205
215
  var ApiClient = class {
206
216
  constructor(apiKey, baseURL, storage) {
207
217
  __publicField(this, "apiKey");
208
218
  __publicField(this, "baseURL");
209
219
  __publicField(this, "outbox");
210
220
  __publicField(this, "isFlushing", false);
221
+ /** The flush in progress, returned to callers that ask for another meanwhile. */
222
+ __publicField(this, "flushPromise", Promise.resolve());
223
+ /** Set when flush() is called during a flush: run another pass before finishing. */
224
+ __publicField(this, "flushAgain", false);
225
+ __publicField(this, "retryTimer", null);
226
+ /** Failed flushes in a row, for the retry backoff. */
227
+ __publicField(this, "failures", 0);
228
+ /** After `cancelRetry()`: no new retry timers until the next `resumeRetry()`. */
229
+ __publicField(this, "retryStopped", false);
211
230
  this.apiKey = apiKey;
212
231
  this.outbox = new Outbox(storage);
213
232
  this.baseURL = baseURL.replace(/\/+$/, "");
@@ -259,9 +278,11 @@ var ApiClient = class {
259
278
  }
260
279
  // ── Impressions / responses ─────────────────────────────────────────────────
261
280
  /** Queues the impression and returns its `clientId`, so the response from the
262
- * same showing can point back to it. */
281
+ * same showing can point back to it. Pass the `clientId` to send the same
282
+ * impression again (e.g. with `dismissed: true` once the user dismissed it):
283
+ * the server updates the one it has. */
263
284
  recordImpression(input) {
264
- const clientId = uuid();
285
+ const clientId = input.clientId ?? uuid();
265
286
  const payload = {
266
287
  clientId,
267
288
  surveyId: input.surveyId,
@@ -307,46 +328,94 @@ var ApiClient = class {
307
328
  }
308
329
  // ── Flush ────────────────────────────────────────────────────────────────────
309
330
  /** POSTs queued entries in order. Stops at the first retryable failure (likely
310
- * offline) and leaves the rest for next time. Entries queued while a flush is
311
- * running are sent by the same flush, so nothing waits for the next event.
312
- * Idempotency keys make re-sends safe. Never throws. */
313
- async flush() {
314
- if (this.isFlushing) return;
331
+ * offline) and schedules a retry (2 s, doubling, capped at 30 s). Entries
332
+ * queued while a flush is running are sent by the same flush, so nothing waits
333
+ * for the next event. Idempotency keys make re-sends safe. Resolves when the
334
+ * flush in progress is done; never throws. */
335
+ flush() {
336
+ if (this.isFlushing) {
337
+ this.flushAgain = true;
338
+ return this.flushPromise;
339
+ }
315
340
  this.isFlushing = true;
341
+ this.clearRetryTimer();
342
+ this.flushPromise = this.runFlush();
343
+ return this.flushPromise;
344
+ }
345
+ /** Stops retrying (the client is stopping). Queued entries stay for later. */
346
+ cancelRetry() {
347
+ this.retryStopped = true;
348
+ this.clearRetryTimer();
349
+ }
350
+ /** Allows retries again after `cancelRetry()`. */
351
+ resumeRetry() {
352
+ this.retryStopped = false;
353
+ }
354
+ async runFlush() {
355
+ let failed = false;
356
+ const sent = /* @__PURE__ */ new Set();
316
357
  try {
317
- const sent = /* @__PURE__ */ new Set();
318
- for (; ; ) {
319
- const pending = this.outbox.snapshot().filter((e) => !sent.has(e.id));
320
- if (pending.length === 0) break;
321
- let failed = false;
322
- for (const entry of pending) {
323
- try {
324
- await this.post(entry.path, entry.body);
325
- this.outbox.remove(entry.id);
326
- sent.add(entry.id);
327
- } catch {
328
- failed = true;
329
- break;
358
+ do {
359
+ this.flushAgain = false;
360
+ for (; ; ) {
361
+ const pending = this.outbox.snapshot().filter((e) => !sent.has(e.id));
362
+ if (pending.length === 0) break;
363
+ for (const entry of pending) {
364
+ try {
365
+ await this.post(entry.path, entry.body);
366
+ this.outbox.remove(entry.id);
367
+ sent.add(entry.id);
368
+ } catch {
369
+ failed = true;
370
+ break;
371
+ }
330
372
  }
373
+ if (failed) break;
331
374
  }
332
- if (failed) break;
333
- }
375
+ } while (!failed && this.flushAgain);
334
376
  } finally {
377
+ this.flushAgain = false;
335
378
  this.isFlushing = false;
336
379
  }
380
+ if (failed) {
381
+ this.failures += 1;
382
+ this.scheduleRetry();
383
+ } else {
384
+ this.failures = 0;
385
+ }
386
+ }
387
+ /** One pending retry at most. */
388
+ scheduleRetry() {
389
+ if (this.retryStopped || this.retryTimer) return;
390
+ const delay = Math.min(RETRY_BASE_MS * 2 ** Math.max(0, this.failures - 1), RETRY_MAX_MS);
391
+ this.retryTimer = setTimeout(() => {
392
+ this.retryTimer = null;
393
+ void this.flush();
394
+ }, delay);
395
+ }
396
+ clearRetryTimer() {
397
+ if (this.retryTimer) clearTimeout(this.retryTimer);
398
+ this.retryTimer = null;
337
399
  }
338
400
  async post(path, body) {
339
- const res = await fetch(this.endpoint(path), {
340
- method: "POST",
341
- headers: {
342
- Authorization: `Bearer ${this.apiKey}`,
343
- "MS-Api-Version": API_VERSION,
344
- "Content-Type": "application/json; charset=utf-8"
345
- },
346
- body
347
- });
348
- if (res.status === 429 || res.status >= 500 && res.status < 600) {
349
- throw new Error(`retryable ${res.status}`);
401
+ const controller = typeof AbortController !== "undefined" ? new AbortController() : null;
402
+ const timeout = controller ? setTimeout(() => controller.abort(), INGEST_TIMEOUT_MS) : null;
403
+ try {
404
+ const res = await fetch(this.endpoint(path), {
405
+ method: "POST",
406
+ headers: {
407
+ Authorization: `Bearer ${this.apiKey}`,
408
+ "MS-Api-Version": API_VERSION,
409
+ "Content-Type": "application/json; charset=utf-8"
410
+ },
411
+ body,
412
+ signal: controller?.signal
413
+ });
414
+ if (res.status === 429 || res.status >= 500 && res.status < 600) {
415
+ throw new Error(`retryable ${res.status}`);
416
+ }
417
+ } finally {
418
+ if (timeout) clearTimeout(timeout);
350
419
  }
351
420
  }
352
421
  endpoint(path) {
@@ -849,7 +918,7 @@ function debugProperties(properties) {
849
918
  }
850
919
 
851
920
  // src/core/version.ts
852
- var SDK_VERSION = "0.1.5";
921
+ var SDK_VERSION = "0.1.7";
853
922
 
854
923
  // src/core/client.ts
855
924
  var DEFAULT_BASE_URL = "https://console.microsurveys.ai";
@@ -914,15 +983,14 @@ var MicroSurveysClient = class {
914
983
  start() {
915
984
  if (this.started) return;
916
985
  this.started = true;
986
+ this.api.resumeRetry();
917
987
  void this.api.flush();
918
988
  void this.refreshConfig().then(() => {
919
989
  if (this.started) this.startPendingChecks();
920
990
  });
921
991
  this.unsubscribeForeground = this.subscribeForeground(() => {
922
- if (this.refreshOnForeground) {
923
- void this.api.flush();
924
- this.refreshConfigIfStale();
925
- }
992
+ void this.api.flush();
993
+ if (this.refreshOnForeground) this.refreshConfigIfStale();
926
994
  this.startPendingChecks();
927
995
  });
928
996
  this.unsubscribeBackground = this.subscribeBackground?.(() => {
@@ -942,6 +1010,7 @@ var MicroSurveysClient = class {
942
1010
  this.unsubscribeBackground = null;
943
1011
  this.unsubscribeForeground?.();
944
1012
  this.unsubscribeForeground = null;
1013
+ this.api.cancelRetry();
945
1014
  this.started = false;
946
1015
  }
947
1016
  /** Force an ETag-aware config refresh; keeps the cache on failure. */
@@ -1119,49 +1188,81 @@ var MicroSurveysClient = class {
1119
1188
  return;
1120
1189
  }
1121
1190
  this.handleDecision({ surveyId: survey.id, triggerId: trigger.id, reason: "shown" });
1122
- const p = { survey, trigger, identity, shownAt: /* @__PURE__ */ new Date(), closed: false };
1191
+ const p = this.newPresentation(survey, trigger, identity);
1123
1192
  this.active = p;
1124
1193
  const active = this.toActiveSurvey(p);
1125
1194
  this.presentListeners.forEach((l) => l(active));
1126
1195
  }
1196
+ newPresentation(survey, trigger, identity) {
1197
+ return { survey, trigger, identity, impressionId: uuid(), presentedAt: /* @__PURE__ */ new Date(), impression: null, closed: false };
1198
+ }
1127
1199
  toActiveSurvey(p) {
1128
1200
  return {
1129
1201
  survey: p.survey,
1130
1202
  trigger: p.trigger,
1203
+ shown: () => this.markShown(p),
1131
1204
  close: (result) => {
1132
1205
  if (p.closed) return;
1133
1206
  p.closed = true;
1134
- this.recordOutcome(p.survey.id, p.trigger?.id ?? null, p.identity, p.shownAt, result);
1135
1207
  if (this.active === p) this.active = null;
1208
+ if (result) this.recordOutcome(p, result);
1136
1209
  }
1137
1210
  };
1138
1211
  }
1139
- /** Records the outcome of an **inline** `<MicroSurvey>` (no trigger/gate). */
1140
- recordInline(survey, result, shownAt) {
1141
- const identity = this.identityStore.snapshot();
1142
- this.recordOutcome(survey.id, null, identity, shownAt, result);
1212
+ /** Queues the impression the moment the survey is on screen, so it counts even
1213
+ * if the app is killed before the survey closes. */
1214
+ markShown(p) {
1215
+ if (p.impression || p.closed) return;
1216
+ p.impression = {
1217
+ clientId: p.impressionId,
1218
+ surveyId: p.survey.id,
1219
+ triggerId: p.trigger?.id ?? null,
1220
+ endUserId: endUserIdOf(p.identity),
1221
+ shownAt: /* @__PURE__ */ new Date(),
1222
+ dismissed: false,
1223
+ context: this.deviceContext()
1224
+ };
1225
+ this.api.recordImpression(p.impression);
1143
1226
  }
1144
- recordOutcome(surveyId, triggerId, identity, shownAt, result) {
1145
- const endUserId = endUserIdOf(identity);
1146
- const context = this.deviceContext();
1147
- const impressionId = this.api.recordImpression({
1148
- surveyId,
1149
- triggerId,
1150
- endUserId,
1151
- shownAt,
1152
- dismissed: result.dismissed,
1153
- context
1154
- });
1227
+ /** An **inline** `<MicroSurvey>` (no trigger, no gate, not "the" active survey).
1228
+ * Call `shown` when it renders and `close` with its outcome. */
1229
+ presentInline(survey) {
1230
+ return this.toActiveSurvey(this.newPresentation(survey, null, this.identityStore.snapshot()));
1231
+ }
1232
+ /** Records the outcome of an inline survey in one go, at close. Prefer
1233
+ * `presentInline`, which sends the impression when the survey appears. */
1234
+ recordInline(survey, result, shownAt) {
1235
+ const p = this.newPresentation(survey, null, this.identityStore.snapshot());
1236
+ p.presentedAt = shownAt;
1237
+ p.closed = true;
1238
+ this.recordOutcome(p, result);
1239
+ }
1240
+ recordOutcome(p, result) {
1241
+ let impression = p.impression;
1242
+ if (!impression) {
1243
+ impression = {
1244
+ clientId: p.impressionId,
1245
+ surveyId: p.survey.id,
1246
+ triggerId: p.trigger?.id ?? null,
1247
+ endUserId: endUserIdOf(p.identity),
1248
+ shownAt: p.presentedAt,
1249
+ dismissed: result.dismissed,
1250
+ context: this.deviceContext()
1251
+ };
1252
+ this.api.recordImpression(impression);
1253
+ } else if (result.dismissed) {
1254
+ this.api.recordImpression({ ...impression, dismissed: true });
1255
+ }
1155
1256
  if (result.completed || result.answers.length > 0) {
1156
1257
  this.api.recordResponse({
1157
- surveyId,
1158
- endUserId,
1258
+ surveyId: p.survey.id,
1259
+ endUserId: impression.endUserId,
1159
1260
  completed: result.completed,
1160
1261
  submittedAt: /* @__PURE__ */ new Date(),
1161
- userProps: identity.userProperties,
1162
- impressionId,
1163
- triggerId,
1164
- context,
1262
+ userProps: p.identity.userProperties,
1263
+ impressionId: p.impressionId,
1264
+ triggerId: p.trigger?.id ?? null,
1265
+ context: impression.context,
1165
1266
  answers: result.answers
1166
1267
  });
1167
1268
  }
@@ -1371,7 +1472,23 @@ var MicroSurveys = class {
1371
1472
  unsubscribe?.();
1372
1473
  };
1373
1474
  }
1374
- /** Records the outcome of an inline `<MicroSurvey>`. */
1475
+ /** Starts one showing of an inline `<MicroSurvey>`: call `shown` once it
1476
+ * renders (sends the impression) and `close` with its outcome. */
1477
+ presentInline(survey) {
1478
+ let inner = null;
1479
+ const withShowing = (fn) => this.withClient((c) => {
1480
+ inner ?? (inner = c.presentInline(survey));
1481
+ fn(inner);
1482
+ });
1483
+ return {
1484
+ survey,
1485
+ trigger: null,
1486
+ shown: () => withShowing((s) => s.shown()),
1487
+ close: (result) => withShowing((s) => s.close(result))
1488
+ };
1489
+ }
1490
+ /** Records the outcome of an inline survey in one go, at close. Prefer
1491
+ * `presentInline`, which sends the impression when the survey appears. */
1375
1492
  recordInline(survey, result, shownAt) {
1376
1493
  this.withClient((c) => c.recordInline(survey, result, shownAt));
1377
1494
  }
@@ -2220,7 +2337,11 @@ function OverlayHost({ instance, fontFamily }) {
2220
2337
  }),
2221
2338
  [instance]
2222
2339
  );
2340
+ useEffect3(() => {
2341
+ current?.active.shown();
2342
+ }, [current]);
2223
2343
  function showNext() {
2344
+ current?.active.close();
2224
2345
  const next = queue.current.shift();
2225
2346
  if (next) {
2226
2347
  setCurrent({ active: next, id: nextId.current++ });
@@ -2266,7 +2387,7 @@ function MicroSurvey({ surveyId, onComplete, style }) {
2266
2387
  const scheme = useColorScheme2() === "dark" ? "dark" : "light";
2267
2388
  const [survey, setSurvey] = useState5(null);
2268
2389
  const [closed, setClosed] = useState5(false);
2269
- const shownAt = useRef4(null);
2390
+ const showing = useRef4(null);
2270
2391
  const result = useRef4(null);
2271
2392
  useEffect4(() => {
2272
2393
  if (!instance) return;
@@ -2274,8 +2395,14 @@ function MicroSurvey({ surveyId, onComplete, style }) {
2274
2395
  }, [instance, surveyId]);
2275
2396
  const projectTheme = instance?.getTheme();
2276
2397
  const theme = useMemo4(() => resolveTheme(projectTheme, { scheme, fontFamily }), [projectTheme, scheme, fontFamily]);
2398
+ const visible = !!instance && !!survey && !closed;
2399
+ if (visible && (!showing.current || showing.current.survey.id !== survey.id)) {
2400
+ showing.current = instance.presentInline(survey);
2401
+ }
2402
+ useEffect4(() => {
2403
+ if (visible) showing.current?.shown();
2404
+ }, [visible, survey]);
2277
2405
  if (!instance || !survey || closed) return null;
2278
- if (!shownAt.current) shownAt.current = /* @__PURE__ */ new Date();
2279
2406
  return /* @__PURE__ */ jsx6(
2280
2407
  View5,
2281
2408
  {
@@ -2291,7 +2418,7 @@ function MicroSurvey({ surveyId, onComplete, style }) {
2291
2418
  variant: "inline",
2292
2419
  onResult: (r) => {
2293
2420
  result.current = r;
2294
- instance.recordInline(survey, r, shownAt.current ?? /* @__PURE__ */ new Date());
2421
+ showing.current?.close(r);
2295
2422
  },
2296
2423
  onClose: () => {
2297
2424
  setClosed(true);