@shotik/react-native 0.1.0 → 0.1.1

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
@@ -176,8 +176,9 @@ The same package ships analytics at `@shotik/react-native/analytics`, using the
176
176
  `moduleResolution: "bundler"` (the Expo default). Metro reads the TypeScript source in `src/`.
177
177
  - **`myapp://support` shows "not found".** The route file is missing, or your router root is `src/app`. Put
178
178
  `support.tsx` next to your root `_layout.tsx`.
179
- - **Messages sit at "Sending".** While offline they show "Queued" and the header counts them; they go out when the
180
- server is reachable. After 3 failed attempts they show "Not sent · Retry".
179
+ - **Messages sit at "Sending".** While offline, or while the server is unreachable or answers 5xx/429, they show
180
+ "Will send when you're back online" (clock) and the header counts them; they go out on their own once the server is
181
+ reachable. Only a 4xx answer shows "Not sent · Retry".
181
182
  - **No attach button.** Neither `expo-image-picker` nor `react-native-view-shot` is in the dev client. Install one
182
183
  and rebuild the dev client.
183
184
 
package/README.ru.md CHANGED
@@ -185,7 +185,8 @@ RevenueCat и для 20% сессий вайрфреймы экранов (`snap
185
185
  `moduleResolution: "bundler"` (по умолчанию в Expo). Metro читает исходники TypeScript из `src/`.
186
186
  - **`myapp://support` показывает «not found».** Нет файла маршрута или корень роутера — `src/app`. Положите
187
187
  `support.tsx` рядом с корневым `_layout.tsx`.
188
- - **Сообщения висят в «Отправляется».** Без сети они «В очереди», а шапка показывает их число; уйдут, когда сервер
189
- станет доступен. После 3 неудачных попыток — «Не отправлено · Повторить».
188
+ - **Сообщения висят в «Отправляется».** Без сети, а также пока сервер недоступен или отвечает 5xx/429, под ними
189
+ «Отправим, когда появится связь» (часы), а шапка показывает их число; уйдут сами, когда сервер станет доступен.
190
+ «Не отправлено · Повторить» появляется только при ответе 4xx.
190
191
  - **Нет кнопки вложения.** В dev client нет ни `expo-image-picker`, ни `react-native-view-shot`. Поставьте один и
191
192
  пересоберите dev client.
package/dist/client.d.ts CHANGED
@@ -37,7 +37,7 @@ export interface Message {
37
37
  meta?: Record<string, unknown> | null;
38
38
  deliveredAt?: number | null;
39
39
  localId?: string;
40
- pending?: 'sending' | 'failed';
40
+ pending?: 'sending' | 'waiting' | 'failed';
41
41
  }
42
42
  export interface UserInfo {
43
43
  userId?: string;
package/dist/client.js CHANGED
@@ -3,14 +3,14 @@
3
3
  // an offline send queue with optimistic messages, and a pull for anything missed. No React Native imports, so it runs
4
4
  // (and is tested) under plain Node as well as in the app.
5
5
  // Receipt for one of your messages: sending -> sent -> delivered (reached Shotik) -> seen (the founder read it).
6
- // queued = waiting for the network.
6
+ // queued = waiting to send: offline, or the last attempt hit a network error, 5xx or 429 (it goes out on its own).
7
7
  export function receiptOf(m, list, conv, offline = false) {
8
8
  if (m.author !== 'user')
9
9
  return null;
10
10
  if (m.pending === 'failed')
11
11
  return 'failed';
12
12
  if (m.pending)
13
- return offline ? 'queued' : 'sending';
13
+ return offline || m.pending === 'waiting' ? 'queued' : 'sending';
14
14
  const unseen = conv?.founderUnread ?? null;
15
15
  if (unseen !== null) {
16
16
  const mine = list.filter((x) => x.author === 'user' && !x.pending);
@@ -28,7 +28,6 @@ export class HttpError extends Error {
28
28
  }
29
29
  const K = { anon: 'shotik.support.anon', session: 'shotik.support.session', queue: 'shotik.support.queue', read: 'shotik.support.read', push: 'shotik.support.push' };
30
30
  const LOCAL_CONV = 'local_conv_';
31
- const AUTO_RETRIES = 3;
32
31
  export function memoryStorage() {
33
32
  const m = new Map();
34
33
  return {
@@ -102,7 +101,7 @@ export class SupportClient {
102
101
  if (this.queue.length) {
103
102
  const messages = { ...this.state.messages };
104
103
  for (const q of this.queue)
105
- messages[q.conversationId] = [...(messages[q.conversationId] ?? []), this.optimistic(q, 'sending')];
104
+ messages[q.conversationId] = [...(messages[q.conversationId] ?? []), this.optimistic(q, q.attempts ? 'waiting' : 'sending')];
106
105
  this.set({ messages, conversations: this.withLocalConversations(this.state.conversations, messages) });
107
106
  }
108
107
  })());
@@ -267,7 +266,8 @@ export class SupportClient {
267
266
  get pendingCount() {
268
267
  return this.queue.length;
269
268
  }
270
- // Deliver queued messages in order. Network errors and 5xx keep them queued with backoff; a 4xx fails the message.
269
+ // Deliver queued messages in order. Network errors, 5xx and 429 keep them queued as 'waiting' with backoff; another 4xx
270
+ // fails the message.
271
271
  flush() {
272
272
  this.flushing ?? (this.flushing = (async () => {
273
273
  try {
@@ -288,7 +288,7 @@ export class SupportClient {
288
288
  this.identify().catch(() => { });
289
289
  return;
290
290
  }
291
- if (status >= 400 && status < 500) {
291
+ if (status >= 400 && status < 500 && status !== 429) {
292
292
  this.queue.shift();
293
293
  this.persist(K.queue, this.queue);
294
294
  this.markPending(item, 'failed');
@@ -296,8 +296,7 @@ export class SupportClient {
296
296
  }
297
297
  item.attempts++;
298
298
  this.persist(K.queue, this.queue);
299
- if (item.attempts >= AUTO_RETRIES)
300
- this.markPending(item, 'failed');
299
+ this.markPending(item, 'waiting');
301
300
  this.set({ status: this.ws ? this.state.status : 'offline', error: e.message });
302
301
  this.scheduleFlush(Math.min(30000, 500 * 2 ** item.attempts));
303
302
  return;
package/dist/sheet.js CHANGED
@@ -73,7 +73,7 @@ export function Sheet({ s, c, t, title, team, quickReplies, presentation = 'moda
73
73
  setComposing(false);
74
74
  s.select(null);
75
75
  };
76
- const queued = Object.values(s.messages).reduce((n, l) => n + l.filter((m) => m.pending === 'sending').length, 0);
76
+ const queued = Object.values(s.messages).reduce((n, l) => n + l.filter((m) => m.pending && m.pending !== 'failed').length, 0);
77
77
  const status = s.status === 'live' ? { color: c.ok, text: t.online }
78
78
  : s.status === 'offline' ? { color: c.danger, text: queued ? t.offline + ' · ' + fill(t.queuedN, { n: queued }) : t.offline }
79
79
  : s.status === 'error' ? { color: c.danger, text: t.error + ' · ' + t.retry }
package/dist/theme.js CHANGED
@@ -29,7 +29,7 @@ const en = {
29
29
  cancel: 'Cancel',
30
30
  remove: 'Remove',
31
31
  sending: 'Sending',
32
- queued: 'Queued',
32
+ queued: "Will send when you're back online",
33
33
  sent: 'Sent',
34
34
  delivered: 'Delivered',
35
35
  seen: 'Seen',
@@ -65,7 +65,7 @@ const ru = {
65
65
  cancel: 'Отмена',
66
66
  remove: 'Убрать',
67
67
  sending: 'Отправляется',
68
- queued: 'В очереди',
68
+ queued: 'Отправим, когда появится связь',
69
69
  sent: 'Отправлено',
70
70
  delivered: 'Доставлено',
71
71
  seen: 'Прочитано',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shotik/react-native",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "Shotik SDK for React Native and Expo: in-app support chat answered from Shotik, and product analytics.",
5
5
  "keywords": [
6
6
  "react-native",
package/src/client.ts CHANGED
@@ -50,7 +50,7 @@ export interface Message {
50
50
  // When the message reached the other side (Shotik for yours, the device for replies).
51
51
  deliveredAt?: number | null
52
52
  localId?: string
53
- pending?: 'sending' | 'failed'
53
+ pending?: 'sending' | 'waiting' | 'failed'
54
54
  }
55
55
 
56
56
  export interface UserInfo {
@@ -107,11 +107,11 @@ interface QueueItem {
107
107
  }
108
108
 
109
109
  // Receipt for one of your messages: sending -> sent -> delivered (reached Shotik) -> seen (the founder read it).
110
- // queued = waiting for the network.
110
+ // queued = waiting to send: offline, or the last attempt hit a network error, 5xx or 429 (it goes out on its own).
111
111
  export function receiptOf(m: Message, list: Message[], conv: Conversation | undefined, offline = false): Receipt | null {
112
112
  if (m.author !== 'user') return null
113
113
  if (m.pending === 'failed') return 'failed'
114
- if (m.pending) return offline ? 'queued' : 'sending'
114
+ if (m.pending) return offline || m.pending === 'waiting' ? 'queued' : 'sending'
115
115
  const unseen = conv?.founderUnread ?? null
116
116
  if (unseen !== null) {
117
117
  const mine = list.filter((x) => x.author === 'user' && !x.pending)
@@ -138,7 +138,6 @@ export class HttpError extends Error {
138
138
 
139
139
  const K = { anon: 'shotik.support.anon', session: 'shotik.support.session', queue: 'shotik.support.queue', read: 'shotik.support.read', push: 'shotik.support.push' }
140
140
  const LOCAL_CONV = 'local_conv_'
141
- const AUTO_RETRIES = 3
142
141
 
143
142
  export function memoryStorage(): KeyValueStorage {
144
143
  const m = new Map<string, string>()
@@ -220,7 +219,7 @@ export class SupportClient {
220
219
  this.pushToken = push ? (JSON.parse(push) as { token: string; platform: string }) : null
221
220
  if (this.queue.length) {
222
221
  const messages = { ...this.state.messages }
223
- for (const q of this.queue) messages[q.conversationId] = [...(messages[q.conversationId] ?? []), this.optimistic(q, 'sending')]
222
+ for (const q of this.queue) messages[q.conversationId] = [...(messages[q.conversationId] ?? []), this.optimistic(q, q.attempts ? 'waiting' : 'sending')]
224
223
  this.set({ messages, conversations: this.withLocalConversations(this.state.conversations, messages) })
225
224
  }
226
225
  })()
@@ -385,7 +384,8 @@ export class SupportClient {
385
384
  return this.queue.length
386
385
  }
387
386
 
388
- // Deliver queued messages in order. Network errors and 5xx keep them queued with backoff; a 4xx fails the message.
387
+ // Deliver queued messages in order. Network errors, 5xx and 429 keep them queued as 'waiting' with backoff; another 4xx
388
+ // fails the message.
389
389
  flush(): Promise<void> {
390
390
  this.flushing ??= (async () => {
391
391
  try {
@@ -404,7 +404,7 @@ export class SupportClient {
404
404
  this.identify().catch(() => {})
405
405
  return
406
406
  }
407
- if (status >= 400 && status < 500) {
407
+ if (status >= 400 && status < 500 && status !== 429) {
408
408
  this.queue.shift()
409
409
  this.persist(K.queue, this.queue)
410
410
  this.markPending(item, 'failed')
@@ -412,7 +412,7 @@ export class SupportClient {
412
412
  }
413
413
  item.attempts++
414
414
  this.persist(K.queue, this.queue)
415
- if (item.attempts >= AUTO_RETRIES) this.markPending(item, 'failed')
415
+ this.markPending(item, 'waiting')
416
416
  this.set({ status: this.ws ? this.state.status : 'offline', error: (e as Error).message })
417
417
  this.scheduleFlush(Math.min(30_000, 500 * 2 ** item.attempts))
418
418
  return
@@ -506,12 +506,12 @@ export class SupportClient {
506
506
  this.set({ messages, conversations: this.sortConvs([conv, ...conversations]), activeConversationId: this.state.activeConversationId === from ? conv.id : this.state.activeConversationId })
507
507
  }
508
508
 
509
- private optimistic(q: QueueItem, pending: 'sending' | 'failed'): Message {
509
+ private optimistic(q: QueueItem, pending: NonNullable<Message['pending']>): Message {
510
510
  const meta = q.attachments?.length ? { attachments: q.attachments.map((a) => ({ uri: a.uri, width: a.width ?? null, height: a.height ?? null, type: a.type ?? 'image/jpeg' })) } : null
511
511
  return { id: q.localId, localId: q.localId, conversationId: q.conversationId, author: 'user', body: q.body, createdAt: q.createdAt, pending, meta }
512
512
  }
513
513
 
514
- private markPending(item: QueueItem, pending: 'sending' | 'failed'): void {
514
+ private markPending(item: QueueItem, pending: NonNullable<Message['pending']>): void {
515
515
  const list = this.state.messages[item.conversationId] ?? []
516
516
  this.set({ messages: { ...this.state.messages, [item.conversationId]: list.map((m) => (m.localId === item.localId ? { ...m, pending } : m)) } })
517
517
  }
package/src/sheet.tsx CHANGED
@@ -101,7 +101,7 @@ export function Sheet({ s, c, t, title, team, quickReplies, presentation = 'moda
101
101
  setComposing(false)
102
102
  s.select(null)
103
103
  }
104
- const queued = Object.values(s.messages).reduce((n, l) => n + l.filter((m) => m.pending === 'sending').length, 0)
104
+ const queued = Object.values(s.messages).reduce((n, l) => n + l.filter((m) => m.pending && m.pending !== 'failed').length, 0)
105
105
  const status =
106
106
  s.status === 'live' ? { color: c.ok, text: t.online }
107
107
  : s.status === 'offline' ? { color: c.danger, text: queued ? t.offline + ' · ' + fill(t.queuedN, { n: queued }) : t.offline }
package/src/theme.ts CHANGED
@@ -70,7 +70,7 @@ const en = {
70
70
  cancel: 'Cancel',
71
71
  remove: 'Remove',
72
72
  sending: 'Sending',
73
- queued: 'Queued',
73
+ queued: "Will send when you're back online",
74
74
  sent: 'Sent',
75
75
  delivered: 'Delivered',
76
76
  seen: 'Seen',
@@ -109,7 +109,7 @@ const ru: Copy = {
109
109
  cancel: 'Отмена',
110
110
  remove: 'Убрать',
111
111
  sending: 'Отправляется',
112
- queued: 'В очереди',
112
+ queued: 'Отправим, когда появится связь',
113
113
  sent: 'Отправлено',
114
114
  delivered: 'Доставлено',
115
115
  seen: 'Прочитано',