@appsupportkit/support-sdk 0.1.2 → 0.3.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/lib/commonjs/api.js +17 -2
- package/lib/commonjs/api.js.map +1 -1
- package/lib/commonjs/components/BottomSheet.js +67 -47
- package/lib/commonjs/components/BottomSheet.js.map +1 -1
- package/lib/commonjs/components/LauncherButton.js +177 -0
- package/lib/commonjs/components/LauncherButton.js.map +1 -0
- package/lib/commonjs/components/SupportButton.js +168 -190
- package/lib/commonjs/components/SupportButton.js.map +1 -1
- package/lib/commonjs/components/icons.js +110 -23
- package/lib/commonjs/components/icons.js.map +1 -1
- package/lib/commonjs/components/primitives.js +191 -44
- package/lib/commonjs/components/primitives.js.map +1 -1
- package/lib/commonjs/components/screens/ComposeScreen.js +108 -60
- package/lib/commonjs/components/screens/ComposeScreen.js.map +1 -1
- package/lib/commonjs/components/screens/HomeScreen.js +221 -118
- package/lib/commonjs/components/screens/HomeScreen.js.map +1 -1
- package/lib/commonjs/components/screens/LanguageScreen.js +150 -0
- package/lib/commonjs/components/screens/LanguageScreen.js.map +1 -0
- package/lib/commonjs/components/screens/ThreadScreen.js +237 -111
- package/lib/commonjs/components/screens/ThreadScreen.js.map +1 -1
- package/lib/commonjs/i18n/index.js +101 -0
- package/lib/commonjs/i18n/index.js.map +1 -0
- package/lib/commonjs/i18n/languages.js +95 -0
- package/lib/commonjs/i18n/languages.js.map +1 -0
- package/lib/commonjs/i18n/strings/de.js +72 -0
- package/lib/commonjs/i18n/strings/de.js.map +1 -0
- package/lib/commonjs/i18n/strings/en.js +88 -0
- package/lib/commonjs/i18n/strings/en.js.map +1 -0
- package/lib/commonjs/i18n/strings/es.js +72 -0
- package/lib/commonjs/i18n/strings/es.js.map +1 -0
- package/lib/commonjs/i18n/strings/fr.js +72 -0
- package/lib/commonjs/i18n/strings/fr.js.map +1 -0
- package/lib/commonjs/i18n/strings/hi.js +72 -0
- package/lib/commonjs/i18n/strings/hi.js.map +1 -0
- package/lib/commonjs/i18n/strings/it.js +72 -0
- package/lib/commonjs/i18n/strings/it.js.map +1 -0
- package/lib/commonjs/i18n/strings/ja.js +72 -0
- package/lib/commonjs/i18n/strings/ja.js.map +1 -0
- package/lib/commonjs/i18n/strings/ko.js +72 -0
- package/lib/commonjs/i18n/strings/ko.js.map +1 -0
- package/lib/commonjs/i18n/strings/nl.js +72 -0
- package/lib/commonjs/i18n/strings/nl.js.map +1 -0
- package/lib/commonjs/i18n/strings/pl.js +72 -0
- package/lib/commonjs/i18n/strings/pl.js.map +1 -0
- package/lib/commonjs/i18n/strings/pt.js +72 -0
- package/lib/commonjs/i18n/strings/pt.js.map +1 -0
- package/lib/commonjs/i18n/strings/ru.js +72 -0
- package/lib/commonjs/i18n/strings/ru.js.map +1 -0
- package/lib/commonjs/i18n/strings/tr.js +72 -0
- package/lib/commonjs/i18n/strings/tr.js.map +1 -0
- package/lib/commonjs/i18n/strings/zh.js +72 -0
- package/lib/commonjs/i18n/strings/zh.js.map +1 -0
- package/lib/commonjs/index.js +31 -0
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/theme.js +136 -17
- package/lib/commonjs/theme.js.map +1 -1
- package/lib/commonjs/types.js +18 -0
- package/lib/commonjs/types.js.map +1 -1
- package/lib/commonjs/unread.js +92 -0
- package/lib/commonjs/unread.js.map +1 -0
- package/lib/commonjs/useSupportThreads.js +296 -0
- package/lib/commonjs/useSupportThreads.js.map +1 -0
- package/lib/commonjs/utils.js +58 -10
- package/lib/commonjs/utils.js.map +1 -1
- package/lib/module/api.js +16 -2
- package/lib/module/api.js.map +1 -1
- package/lib/module/components/BottomSheet.js +68 -48
- package/lib/module/components/BottomSheet.js.map +1 -1
- package/lib/module/components/LauncherButton.js +172 -0
- package/lib/module/components/LauncherButton.js.map +1 -0
- package/lib/module/components/SupportButton.js +172 -194
- package/lib/module/components/SupportButton.js.map +1 -1
- package/lib/module/components/icons.js +107 -22
- package/lib/module/components/icons.js.map +1 -1
- package/lib/module/components/primitives.js +187 -45
- package/lib/module/components/primitives.js.map +1 -1
- package/lib/module/components/screens/ComposeScreen.js +114 -64
- package/lib/module/components/screens/ComposeScreen.js.map +1 -1
- package/lib/module/components/screens/HomeScreen.js +224 -121
- package/lib/module/components/screens/HomeScreen.js.map +1 -1
- package/lib/module/components/screens/LanguageScreen.js +147 -0
- package/lib/module/components/screens/LanguageScreen.js.map +1 -0
- package/lib/module/components/screens/ThreadScreen.js +243 -115
- package/lib/module/components/screens/ThreadScreen.js.map +1 -1
- package/lib/module/i18n/index.js +56 -0
- package/lib/module/i18n/index.js.map +1 -0
- package/lib/module/i18n/languages.js +88 -0
- package/lib/module/i18n/languages.js.map +1 -0
- package/lib/module/i18n/strings/de.js +68 -0
- package/lib/module/i18n/strings/de.js.map +1 -0
- package/lib/module/i18n/strings/en.js +84 -0
- package/lib/module/i18n/strings/en.js.map +1 -0
- package/lib/module/i18n/strings/es.js +68 -0
- package/lib/module/i18n/strings/es.js.map +1 -0
- package/lib/module/i18n/strings/fr.js +68 -0
- package/lib/module/i18n/strings/fr.js.map +1 -0
- package/lib/module/i18n/strings/hi.js +68 -0
- package/lib/module/i18n/strings/hi.js.map +1 -0
- package/lib/module/i18n/strings/it.js +68 -0
- package/lib/module/i18n/strings/it.js.map +1 -0
- package/lib/module/i18n/strings/ja.js +68 -0
- package/lib/module/i18n/strings/ja.js.map +1 -0
- package/lib/module/i18n/strings/ko.js +68 -0
- package/lib/module/i18n/strings/ko.js.map +1 -0
- package/lib/module/i18n/strings/nl.js +68 -0
- package/lib/module/i18n/strings/nl.js.map +1 -0
- package/lib/module/i18n/strings/pl.js +68 -0
- package/lib/module/i18n/strings/pl.js.map +1 -0
- package/lib/module/i18n/strings/pt.js +68 -0
- package/lib/module/i18n/strings/pt.js.map +1 -0
- package/lib/module/i18n/strings/ru.js +68 -0
- package/lib/module/i18n/strings/ru.js.map +1 -0
- package/lib/module/i18n/strings/tr.js +68 -0
- package/lib/module/i18n/strings/tr.js.map +1 -0
- package/lib/module/i18n/strings/zh.js +68 -0
- package/lib/module/i18n/strings/zh.js.map +1 -0
- package/lib/module/index.js +1 -0
- package/lib/module/index.js.map +1 -1
- package/lib/module/theme.js +135 -16
- package/lib/module/theme.js.map +1 -1
- package/lib/module/types.js +18 -0
- package/lib/module/types.js.map +1 -1
- package/lib/module/unread.js +83 -0
- package/lib/module/unread.js.map +1 -0
- package/lib/module/useSupportThreads.js +291 -0
- package/lib/module/useSupportThreads.js.map +1 -0
- package/lib/module/utils.js +55 -10
- package/lib/module/utils.js.map +1 -1
- package/lib/typescript/api.d.ts +9 -0
- package/lib/typescript/api.d.ts.map +1 -1
- package/lib/typescript/components/BottomSheet.d.ts.map +1 -1
- package/lib/typescript/components/LauncherButton.d.ts +14 -0
- package/lib/typescript/components/LauncherButton.d.ts.map +1 -0
- package/lib/typescript/components/SupportButton.d.ts +3 -3
- package/lib/typescript/components/SupportButton.d.ts.map +1 -1
- package/lib/typescript/components/icons.d.ts +18 -2
- package/lib/typescript/components/icons.d.ts.map +1 -1
- package/lib/typescript/components/primitives.d.ts +42 -7
- package/lib/typescript/components/primitives.d.ts.map +1 -1
- package/lib/typescript/components/screens/ComposeScreen.d.ts +3 -1
- package/lib/typescript/components/screens/ComposeScreen.d.ts.map +1 -1
- package/lib/typescript/components/screens/HomeScreen.d.ts +8 -2
- package/lib/typescript/components/screens/HomeScreen.d.ts.map +1 -1
- package/lib/typescript/components/screens/LanguageScreen.d.ts +30 -0
- package/lib/typescript/components/screens/LanguageScreen.d.ts.map +1 -0
- package/lib/typescript/components/screens/ThreadScreen.d.ts +3 -1
- package/lib/typescript/components/screens/ThreadScreen.d.ts.map +1 -1
- package/lib/typescript/i18n/index.d.ts +15 -0
- package/lib/typescript/i18n/index.d.ts.map +1 -0
- package/lib/typescript/i18n/languages.d.ts +36 -0
- package/lib/typescript/i18n/languages.d.ts.map +1 -0
- package/lib/typescript/i18n/strings/de.d.ts +3 -0
- package/lib/typescript/i18n/strings/de.d.ts.map +1 -0
- package/lib/typescript/i18n/strings/en.d.ts +83 -0
- package/lib/typescript/i18n/strings/en.d.ts.map +1 -0
- package/lib/typescript/i18n/strings/es.d.ts +3 -0
- package/lib/typescript/i18n/strings/es.d.ts.map +1 -0
- package/lib/typescript/i18n/strings/fr.d.ts +3 -0
- package/lib/typescript/i18n/strings/fr.d.ts.map +1 -0
- package/lib/typescript/i18n/strings/hi.d.ts +3 -0
- package/lib/typescript/i18n/strings/hi.d.ts.map +1 -0
- package/lib/typescript/i18n/strings/it.d.ts +3 -0
- package/lib/typescript/i18n/strings/it.d.ts.map +1 -0
- package/lib/typescript/i18n/strings/ja.d.ts +3 -0
- package/lib/typescript/i18n/strings/ja.d.ts.map +1 -0
- package/lib/typescript/i18n/strings/ko.d.ts +3 -0
- package/lib/typescript/i18n/strings/ko.d.ts.map +1 -0
- package/lib/typescript/i18n/strings/nl.d.ts +3 -0
- package/lib/typescript/i18n/strings/nl.d.ts.map +1 -0
- package/lib/typescript/i18n/strings/pl.d.ts +3 -0
- package/lib/typescript/i18n/strings/pl.d.ts.map +1 -0
- package/lib/typescript/i18n/strings/pt.d.ts +3 -0
- package/lib/typescript/i18n/strings/pt.d.ts.map +1 -0
- package/lib/typescript/i18n/strings/ru.d.ts +3 -0
- package/lib/typescript/i18n/strings/ru.d.ts.map +1 -0
- package/lib/typescript/i18n/strings/tr.d.ts +3 -0
- package/lib/typescript/i18n/strings/tr.d.ts.map +1 -0
- package/lib/typescript/i18n/strings/zh.d.ts +3 -0
- package/lib/typescript/i18n/strings/zh.d.ts.map +1 -0
- package/lib/typescript/index.d.ts +2 -1
- package/lib/typescript/index.d.ts.map +1 -1
- package/lib/typescript/theme.d.ts +44 -3
- package/lib/typescript/theme.d.ts.map +1 -1
- package/lib/typescript/types.d.ts +54 -2
- package/lib/typescript/types.d.ts.map +1 -1
- package/lib/typescript/unread.d.ts +22 -0
- package/lib/typescript/unread.d.ts.map +1 -0
- package/lib/typescript/useSupportThreads.d.ts +38 -0
- package/lib/typescript/useSupportThreads.d.ts.map +1 -0
- package/lib/typescript/utils.d.ts +8 -2
- package/lib/typescript/utils.d.ts.map +1 -1
- package/package.json +3 -3
- package/src/api.ts +24 -1
- package/src/components/BottomSheet.tsx +61 -43
- package/src/components/LauncherButton.tsx +217 -0
- package/src/components/SupportButton.tsx +195 -210
- package/src/components/icons.tsx +113 -19
- package/src/components/primitives.tsx +230 -39
- package/src/components/screens/ComposeScreen.tsx +129 -63
- package/src/components/screens/HomeScreen.tsx +230 -120
- package/src/components/screens/LanguageScreen.tsx +172 -0
- package/src/components/screens/ThreadScreen.tsx +319 -122
- package/src/i18n/index.ts +81 -0
- package/src/i18n/languages.ts +106 -0
- package/src/i18n/strings/de.ts +78 -0
- package/src/i18n/strings/en.ts +94 -0
- package/src/i18n/strings/es.ts +77 -0
- package/src/i18n/strings/fr.ts +78 -0
- package/src/i18n/strings/hi.ts +77 -0
- package/src/i18n/strings/it.ts +77 -0
- package/src/i18n/strings/ja.ts +77 -0
- package/src/i18n/strings/ko.ts +76 -0
- package/src/i18n/strings/nl.ts +77 -0
- package/src/i18n/strings/pl.ts +77 -0
- package/src/i18n/strings/pt.ts +77 -0
- package/src/i18n/strings/ru.ts +77 -0
- package/src/i18n/strings/tr.ts +77 -0
- package/src/i18n/strings/zh.ts +75 -0
- package/src/index.tsx +9 -0
- package/src/theme.ts +165 -18
- package/src/types.ts +56 -2
- package/src/unread.ts +91 -0
- package/src/useSupportThreads.ts +392 -0
- package/src/utils.ts +48 -10
|
@@ -0,0 +1,392 @@
|
|
|
1
|
+
// Threads + unread state for the SDK: fetching, poll cadence, and read
|
|
2
|
+
// tracking. Kept out of SupportButton so the rules below live in one place.
|
|
3
|
+
//
|
|
4
|
+
// Three invariants make the badge trustworthy:
|
|
5
|
+
//
|
|
6
|
+
// 1. Responses are applied in issue order (`appliedSeq`), so a slow poll
|
|
7
|
+
// can never overwrite a newer one.
|
|
8
|
+
// 2. The badge-only endpoint returns a bare number that cannot be
|
|
9
|
+
// reconciled against local reads, so its answer is dropped when a read
|
|
10
|
+
// happened while it was in flight (`readEpoch`), and it isn't used at
|
|
11
|
+
// all while any `mark-read` is still unconfirmed — the full endpoint is
|
|
12
|
+
// polled instead, since that payload *can* be reconciled.
|
|
13
|
+
// 3. `mark-read` failures are retried instead of swallowed, and the local
|
|
14
|
+
// watermark holds the UI correct in the meantime.
|
|
15
|
+
import { useCallback, useEffect, useRef, useState } from 'react';
|
|
16
|
+
import { AppState } from 'react-native';
|
|
17
|
+
import {
|
|
18
|
+
getThreads,
|
|
19
|
+
getUnreadCount,
|
|
20
|
+
markRead,
|
|
21
|
+
SupportApiError,
|
|
22
|
+
type ApiConfig,
|
|
23
|
+
} from './api';
|
|
24
|
+
import type { StringKey } from './i18n';
|
|
25
|
+
import type { Thread } from './types';
|
|
26
|
+
import {
|
|
27
|
+
latestAdminMessageAt,
|
|
28
|
+
pruneWatermarks,
|
|
29
|
+
reconcileThreads,
|
|
30
|
+
sumUnread,
|
|
31
|
+
type ReadWatermarks,
|
|
32
|
+
} from './unread';
|
|
33
|
+
|
|
34
|
+
// Poll cadence by context: fast where latency is felt (staring at a thread
|
|
35
|
+
// waiting for a reply), slow when the sheet is closed — and the idle tick
|
|
36
|
+
// uses the count-only endpoint, so it costs ~18 bytes instead of the full
|
|
37
|
+
// thread list.
|
|
38
|
+
const THREAD_POLL_MS = 5_000;
|
|
39
|
+
const SHEET_POLL_MS = 15_000;
|
|
40
|
+
export const IDLE_POLL_MS = 60_000;
|
|
41
|
+
const MIN_POLL_MS = 3_000;
|
|
42
|
+
|
|
43
|
+
// A failed mark-read used to be dropped on the floor, which resurrected the
|
|
44
|
+
// badge on the next poll. Retry a few times with backoff; anything still
|
|
45
|
+
// unconfirmed after that is picked up by the next poll tick or foreground.
|
|
46
|
+
const MARK_READ_MAX_ATTEMPTS = 4;
|
|
47
|
+
const MARK_READ_BACKOFF_MS = 1_500;
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Load failures the user can act on say so; the rest stay neutral. A 401 is
|
|
51
|
+
* an integration mistake — the end user retrying forever won't fix a key
|
|
52
|
+
* that doesn't match the app — so the real cause is logged for whoever is
|
|
53
|
+
* running the build instead of hidden behind "couldn't load".
|
|
54
|
+
*
|
|
55
|
+
* Returns a key rather than a message: the hook has no business knowing what
|
|
56
|
+
* language the sheet is in, and resolving it here would make every callback
|
|
57
|
+
* depend on the translator.
|
|
58
|
+
*/
|
|
59
|
+
function loadErrorKey(e: unknown): StringKey {
|
|
60
|
+
if (e instanceof SupportApiError) {
|
|
61
|
+
if (e.offline) return 'error.offline';
|
|
62
|
+
if (e.status === 401 || e.status === 403) {
|
|
63
|
+
if (__DEV__) {
|
|
64
|
+
console.error(
|
|
65
|
+
'SupportButton: the support API rejected this app (401). Check that ' +
|
|
66
|
+
'`apiKey` is the key shown when the app was registered and that ' +
|
|
67
|
+
'`appName` matches that app exactly.',
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
return 'error.unavailable';
|
|
71
|
+
}
|
|
72
|
+
if (e.status === 429) return 'error.loadRateLimited';
|
|
73
|
+
}
|
|
74
|
+
return 'error.loadFailed';
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
interface ReadAttempt {
|
|
78
|
+
/** Newest reply timestamp this thread's receipt still has to cover. */
|
|
79
|
+
target: number;
|
|
80
|
+
/** Timestamp covered by the request currently in flight, if any. */
|
|
81
|
+
inFlight: number | null;
|
|
82
|
+
attempts: number;
|
|
83
|
+
timer: ReturnType<typeof setTimeout> | null;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export interface SupportThreadsState {
|
|
87
|
+
/** Server threads with unread counts narrowed by local reads. */
|
|
88
|
+
threads: Thread[] | null;
|
|
89
|
+
totalUnread: number;
|
|
90
|
+
/** First load only — there is nothing cached to show yet. */
|
|
91
|
+
loading: boolean;
|
|
92
|
+
/** User-pulled refresh with content already on screen. */
|
|
93
|
+
refreshing: boolean;
|
|
94
|
+
/** Set only when there is nothing cached; background failures stay quiet. */
|
|
95
|
+
errorKey: StringKey | null;
|
|
96
|
+
/** The language this user picked, or null if they never have. */
|
|
97
|
+
serverLanguage: string | null;
|
|
98
|
+
/**
|
|
99
|
+
* False until the first successful load. `serverLanguage === null` only
|
|
100
|
+
* means "never picked" once this is true — before that it means "don't
|
|
101
|
+
* know yet", and showing the picker on a guess would nag a user who
|
|
102
|
+
* answered on another device.
|
|
103
|
+
*/
|
|
104
|
+
languageLoaded: boolean;
|
|
105
|
+
/** User-initiated refresh (pull-to-refresh, retry button). */
|
|
106
|
+
refresh: () => void;
|
|
107
|
+
/** Awaited by send/submit handlers so the list reflects the write. */
|
|
108
|
+
reload: () => Promise<void>;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export function useSupportThreads({
|
|
112
|
+
config,
|
|
113
|
+
revenuecatId,
|
|
114
|
+
enabled,
|
|
115
|
+
sheetOpen,
|
|
116
|
+
activeThreadId,
|
|
117
|
+
idlePollMs,
|
|
118
|
+
}: {
|
|
119
|
+
config: ApiConfig;
|
|
120
|
+
revenuecatId: string;
|
|
121
|
+
/** False when props failed validation — no requests are made. */
|
|
122
|
+
enabled: boolean;
|
|
123
|
+
sheetOpen: boolean;
|
|
124
|
+
activeThreadId: string | null;
|
|
125
|
+
idlePollMs: number;
|
|
126
|
+
}): SupportThreadsState {
|
|
127
|
+
const [threads, setThreads] = useState<Thread[] | null>(null);
|
|
128
|
+
const [totalUnread, setTotalUnread] = useState(0);
|
|
129
|
+
const [loading, setLoading] = useState(false);
|
|
130
|
+
const [refreshing, setRefreshing] = useState(false);
|
|
131
|
+
const [errorKey, setErrorKey] = useState<StringKey | null>(null);
|
|
132
|
+
const [serverLanguage, setServerLanguage] = useState<string | null>(null);
|
|
133
|
+
const [languageLoaded, setLanguageLoaded] = useState(false);
|
|
134
|
+
|
|
135
|
+
const mounted = useRef(true);
|
|
136
|
+
/** Last server payload, before local read reconciliation. */
|
|
137
|
+
const rawThreads = useRef<Thread[] | null>(null);
|
|
138
|
+
const watermarks = useRef<ReadWatermarks>(new Map());
|
|
139
|
+
const reads = useRef(new Map<string, ReadAttempt>());
|
|
140
|
+
const requestSeq = useRef(0);
|
|
141
|
+
const appliedSeq = useRef(0);
|
|
142
|
+
const readEpoch = useRef(0);
|
|
143
|
+
const activeThreadIdRef = useRef<string | null>(activeThreadId);
|
|
144
|
+
|
|
145
|
+
useEffect(() => {
|
|
146
|
+
activeThreadIdRef.current = activeThreadId;
|
|
147
|
+
}, [activeThreadId]);
|
|
148
|
+
|
|
149
|
+
useEffect(() => {
|
|
150
|
+
mounted.current = true;
|
|
151
|
+
const timers = reads.current;
|
|
152
|
+
return () => {
|
|
153
|
+
mounted.current = false;
|
|
154
|
+
for (const attempt of timers.values()) {
|
|
155
|
+
if (attempt.timer !== null) clearTimeout(attempt.timer);
|
|
156
|
+
}
|
|
157
|
+
timers.clear();
|
|
158
|
+
};
|
|
159
|
+
}, []);
|
|
160
|
+
|
|
161
|
+
const publish = useCallback(() => {
|
|
162
|
+
const raw = rawThreads.current;
|
|
163
|
+
if (raw === null) return;
|
|
164
|
+
const reconciled = reconcileThreads(raw, watermarks.current);
|
|
165
|
+
setThreads(reconciled);
|
|
166
|
+
setTotalUnread(sumUnread(reconciled));
|
|
167
|
+
}, []);
|
|
168
|
+
|
|
169
|
+
/** POSTs the receipt, retrying transient failures with backoff. */
|
|
170
|
+
const sendRead = useCallback(
|
|
171
|
+
(threadId: string) => {
|
|
172
|
+
const attempt = reads.current.get(threadId);
|
|
173
|
+
if (!attempt || attempt.inFlight !== null) return;
|
|
174
|
+
const covers = attempt.target;
|
|
175
|
+
attempt.inFlight = covers;
|
|
176
|
+
attempt.timer = null;
|
|
177
|
+
|
|
178
|
+
markRead(config, { queryId: threadId, revenuecatId })
|
|
179
|
+
.then(() => {
|
|
180
|
+
if (!mounted.current) return;
|
|
181
|
+
const current = reads.current.get(threadId);
|
|
182
|
+
if (!current) return;
|
|
183
|
+
current.inFlight = null;
|
|
184
|
+
// A reply that landed while this request was in flight isn't
|
|
185
|
+
// covered by the receipt it just wrote — send one more.
|
|
186
|
+
if (current.target > covers) {
|
|
187
|
+
current.attempts = 0;
|
|
188
|
+
sendRead(threadId);
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
reads.current.delete(threadId);
|
|
192
|
+
// The receipt now exists server-side, so any response still in
|
|
193
|
+
// flight predates it and must not be trusted for unread counts.
|
|
194
|
+
readEpoch.current += 1;
|
|
195
|
+
})
|
|
196
|
+
.catch(() => {
|
|
197
|
+
if (!mounted.current) return;
|
|
198
|
+
const current = reads.current.get(threadId);
|
|
199
|
+
if (!current) return;
|
|
200
|
+
current.inFlight = null;
|
|
201
|
+
current.attempts += 1;
|
|
202
|
+
if (current.attempts >= MARK_READ_MAX_ATTEMPTS) return;
|
|
203
|
+
current.timer = setTimeout(
|
|
204
|
+
() => sendRead(threadId),
|
|
205
|
+
MARK_READ_BACKOFF_MS * 2 ** (current.attempts - 1),
|
|
206
|
+
);
|
|
207
|
+
});
|
|
208
|
+
},
|
|
209
|
+
[config, revenuecatId],
|
|
210
|
+
);
|
|
211
|
+
|
|
212
|
+
/** Re-drives receipts that gave up earlier (next poll tick / foreground). */
|
|
213
|
+
const flushReads = useCallback(() => {
|
|
214
|
+
for (const [threadId, attempt] of reads.current) {
|
|
215
|
+
if (attempt.inFlight !== null || attempt.timer !== null) continue;
|
|
216
|
+
attempt.attempts = 0;
|
|
217
|
+
sendRead(threadId);
|
|
218
|
+
}
|
|
219
|
+
}, [sendRead]);
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* Records that the user has seen every reply currently in `threadId` and
|
|
223
|
+
* persists it. Idempotent: re-marking a thread with nothing new is a
|
|
224
|
+
* no-op, so the 5s in-thread poll doesn't spam the endpoint.
|
|
225
|
+
*/
|
|
226
|
+
const markThreadRead = useCallback(
|
|
227
|
+
(threadId: string) => {
|
|
228
|
+
const thread = rawThreads.current?.find((t) => t.id === threadId);
|
|
229
|
+
if (!thread) return;
|
|
230
|
+
const latest = latestAdminMessageAt(thread);
|
|
231
|
+
if (latest === 0) return;
|
|
232
|
+
if (latest <= (watermarks.current.get(threadId) ?? 0)) return;
|
|
233
|
+
|
|
234
|
+
watermarks.current.set(threadId, latest);
|
|
235
|
+
// Invalidate the bare counts of any request already in flight.
|
|
236
|
+
readEpoch.current += 1;
|
|
237
|
+
publish();
|
|
238
|
+
|
|
239
|
+
const attempt = reads.current.get(threadId);
|
|
240
|
+
if (attempt) attempt.target = latest;
|
|
241
|
+
else {
|
|
242
|
+
reads.current.set(threadId, {
|
|
243
|
+
target: latest,
|
|
244
|
+
inFlight: null,
|
|
245
|
+
attempts: 0,
|
|
246
|
+
timer: null,
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
sendRead(threadId);
|
|
250
|
+
},
|
|
251
|
+
[publish, sendRead],
|
|
252
|
+
);
|
|
253
|
+
|
|
254
|
+
const fetchThreads = useCallback(
|
|
255
|
+
async (mode: 'initial' | 'user' | 'background') => {
|
|
256
|
+
if (!enabled) return;
|
|
257
|
+
const seq = (requestSeq.current += 1);
|
|
258
|
+
// With nothing cached there is no list to pull down, so a retry shows
|
|
259
|
+
// the skeleton rather than a spinner nobody can see.
|
|
260
|
+
const blank = rawThreads.current === null;
|
|
261
|
+
const spinner = mode === 'user' && !blank;
|
|
262
|
+
const skeleton = mode !== 'background' && blank;
|
|
263
|
+
if (spinner) setRefreshing(true);
|
|
264
|
+
if (skeleton) setLoading(true);
|
|
265
|
+
|
|
266
|
+
try {
|
|
267
|
+
const data = await getThreads(config, revenuecatId);
|
|
268
|
+
if (!mounted.current) return;
|
|
269
|
+
// A slower earlier request must never clobber a newer answer.
|
|
270
|
+
if (seq <= appliedSeq.current) return;
|
|
271
|
+
appliedSeq.current = seq;
|
|
272
|
+
|
|
273
|
+
rawThreads.current = data.threads;
|
|
274
|
+
watermarks.current = pruneWatermarks(watermarks.current, data.threads);
|
|
275
|
+
setErrorKey(null);
|
|
276
|
+
setServerLanguage(data.language);
|
|
277
|
+
setLanguageLoaded(true);
|
|
278
|
+
publish();
|
|
279
|
+
|
|
280
|
+
// Replies that arrived while the user is sitting in the thread are
|
|
281
|
+
// read on arrival — otherwise the badge climbed while they read.
|
|
282
|
+
const active = activeThreadIdRef.current;
|
|
283
|
+
if (active !== null) markThreadRead(active);
|
|
284
|
+
flushReads();
|
|
285
|
+
} catch (e) {
|
|
286
|
+
if (!mounted.current) return;
|
|
287
|
+
// A failed background poll must not blow away cached threads.
|
|
288
|
+
if (rawThreads.current === null) setErrorKey(loadErrorKey(e));
|
|
289
|
+
} finally {
|
|
290
|
+
if (mounted.current) {
|
|
291
|
+
if (spinner) setRefreshing(false);
|
|
292
|
+
if (skeleton) setLoading(false);
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
},
|
|
296
|
+
[config, enabled, revenuecatId, publish, markThreadRead, flushReads],
|
|
297
|
+
);
|
|
298
|
+
|
|
299
|
+
/** Badge-only refresh — the cheap endpoint, used while idle. */
|
|
300
|
+
const fetchCount = useCallback(async () => {
|
|
301
|
+
if (!enabled) return;
|
|
302
|
+
const seq = (requestSeq.current += 1);
|
|
303
|
+
const epoch = readEpoch.current;
|
|
304
|
+
try {
|
|
305
|
+
const data = await getUnreadCount(config, revenuecatId);
|
|
306
|
+
if (!mounted.current) return;
|
|
307
|
+
// This payload is just a number: there is no way to subtract reads
|
|
308
|
+
// that happened while it was in flight, so discard it instead.
|
|
309
|
+
if (readEpoch.current !== epoch) return;
|
|
310
|
+
if (seq <= appliedSeq.current) return;
|
|
311
|
+
appliedSeq.current = seq;
|
|
312
|
+
setTotalUnread(data.total_unread);
|
|
313
|
+
} catch {
|
|
314
|
+
// Best effort: the next tick or the next sheet open reconciles.
|
|
315
|
+
}
|
|
316
|
+
}, [config, enabled, revenuecatId]);
|
|
317
|
+
|
|
318
|
+
// Initial load.
|
|
319
|
+
useEffect(() => {
|
|
320
|
+
void fetchThreads('initial');
|
|
321
|
+
}, [fetchThreads]);
|
|
322
|
+
|
|
323
|
+
// Opening the sheet always shows fresh data.
|
|
324
|
+
useEffect(() => {
|
|
325
|
+
if (sheetOpen) void fetchThreads('background');
|
|
326
|
+
}, [sheetOpen, fetchThreads]);
|
|
327
|
+
|
|
328
|
+
// Opening a thread marks it read immediately, before the next poll.
|
|
329
|
+
useEffect(() => {
|
|
330
|
+
if (activeThreadId !== null) markThreadRead(activeThreadId);
|
|
331
|
+
}, [activeThreadId, markThreadRead]);
|
|
332
|
+
|
|
333
|
+
// Cadence follows what the user is looking at; the app returning to the
|
|
334
|
+
// foreground always ticks.
|
|
335
|
+
useEffect(() => {
|
|
336
|
+
if (!enabled) return;
|
|
337
|
+
const idle = !sheetOpen;
|
|
338
|
+
const intervalMs = idle
|
|
339
|
+
? idlePollMs
|
|
340
|
+
: activeThreadId !== null
|
|
341
|
+
? THREAD_POLL_MS
|
|
342
|
+
: SHEET_POLL_MS;
|
|
343
|
+
|
|
344
|
+
const tick = () => {
|
|
345
|
+
// While a receipt is unconfirmed the count-only endpoint would report
|
|
346
|
+
// replies the user has already read, so pay for the full payload.
|
|
347
|
+
if (idle && reads.current.size === 0) void fetchCount();
|
|
348
|
+
else void fetchThreads('background');
|
|
349
|
+
};
|
|
350
|
+
|
|
351
|
+
// Clamped so a typo like `pollIntervalMs={30}` can't hammer the API;
|
|
352
|
+
// 0 (or negative) disables idle polling entirely.
|
|
353
|
+
const timer =
|
|
354
|
+
intervalMs > 0
|
|
355
|
+
? setInterval(tick, Math.max(intervalMs, MIN_POLL_MS))
|
|
356
|
+
: null;
|
|
357
|
+
|
|
358
|
+
const appStateSub = AppState.addEventListener('change', (state) => {
|
|
359
|
+
if (state === 'active') tick();
|
|
360
|
+
});
|
|
361
|
+
|
|
362
|
+
return () => {
|
|
363
|
+
if (timer !== null) clearInterval(timer);
|
|
364
|
+
appStateSub.remove();
|
|
365
|
+
};
|
|
366
|
+
}, [
|
|
367
|
+
enabled,
|
|
368
|
+
sheetOpen,
|
|
369
|
+
activeThreadId,
|
|
370
|
+
idlePollMs,
|
|
371
|
+
fetchThreads,
|
|
372
|
+
fetchCount,
|
|
373
|
+
]);
|
|
374
|
+
|
|
375
|
+
const refresh = useCallback(() => {
|
|
376
|
+
void fetchThreads('user');
|
|
377
|
+
}, [fetchThreads]);
|
|
378
|
+
|
|
379
|
+
const reload = useCallback(() => fetchThreads('background'), [fetchThreads]);
|
|
380
|
+
|
|
381
|
+
return {
|
|
382
|
+
threads,
|
|
383
|
+
totalUnread,
|
|
384
|
+
loading,
|
|
385
|
+
refreshing,
|
|
386
|
+
errorKey,
|
|
387
|
+
serverLanguage,
|
|
388
|
+
languageLoaded,
|
|
389
|
+
refresh,
|
|
390
|
+
reload,
|
|
391
|
+
};
|
|
392
|
+
}
|
package/src/utils.ts
CHANGED
|
@@ -1,29 +1,67 @@
|
|
|
1
|
+
import type { Translator } from './i18n';
|
|
2
|
+
|
|
3
|
+
// Dates and times follow the language the user picked, not the device's:
|
|
4
|
+
// a sheet that says "Traduction…" above "2 days ago" reads as broken.
|
|
5
|
+
|
|
1
6
|
/** "2h ago" relative time — mirrors the web dashboard's formatting. */
|
|
2
|
-
export function relativeTime(iso: string): string {
|
|
7
|
+
export function relativeTime(iso: string, strings: Translator): string {
|
|
3
8
|
const then = new Date(iso).getTime();
|
|
4
9
|
const seconds = Math.max(0, Math.floor((Date.now() - then) / 1000));
|
|
5
|
-
if (seconds < 60) return '
|
|
10
|
+
if (seconds < 60) return strings.t('time.justNow');
|
|
6
11
|
const minutes = Math.floor(seconds / 60);
|
|
7
|
-
if (minutes < 60) return
|
|
12
|
+
if (minutes < 60) return strings.t('time.minutes', { count: minutes });
|
|
8
13
|
const hours = Math.floor(minutes / 60);
|
|
9
|
-
if (hours < 24) return
|
|
14
|
+
if (hours < 24) return strings.t('time.hours', { count: hours });
|
|
10
15
|
const days = Math.floor(hours / 24);
|
|
11
|
-
if (days < 7) return
|
|
16
|
+
if (days < 7) return strings.t('time.days', { count: days });
|
|
12
17
|
const weeks = Math.floor(days / 7);
|
|
13
|
-
if (weeks < 5) return
|
|
18
|
+
if (weeks < 5) return strings.t('time.weeks', { count: weeks });
|
|
14
19
|
const months = Math.floor(days / 30);
|
|
15
|
-
if (months < 12) return
|
|
16
|
-
return
|
|
20
|
+
if (months < 12) return strings.t('time.months', { count: months });
|
|
21
|
+
return strings.t('time.years', { count: Math.floor(days / 365) });
|
|
17
22
|
}
|
|
18
23
|
|
|
19
|
-
export function formatDate(iso: string): string {
|
|
20
|
-
return new Date(iso).toLocaleDateString(
|
|
24
|
+
export function formatDate(iso: string, strings: Translator): string {
|
|
25
|
+
return new Date(iso).toLocaleDateString(strings.locale, {
|
|
21
26
|
year: 'numeric',
|
|
22
27
|
month: 'short',
|
|
23
28
|
day: 'numeric',
|
|
24
29
|
});
|
|
25
30
|
}
|
|
26
31
|
|
|
32
|
+
/** "14:32" in the user's locale — shown under a group of messages. */
|
|
33
|
+
export function formatTime(iso: string, strings: Translator): string {
|
|
34
|
+
return new Date(iso).toLocaleTimeString(strings.locale, {
|
|
35
|
+
hour: 'numeric',
|
|
36
|
+
minute: '2-digit',
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function isSameDay(a: string, b: string): boolean {
|
|
41
|
+
const first = new Date(a);
|
|
42
|
+
const second = new Date(b);
|
|
43
|
+
return (
|
|
44
|
+
first.getFullYear() === second.getFullYear() &&
|
|
45
|
+
first.getMonth() === second.getMonth() &&
|
|
46
|
+
first.getDate() === second.getDate()
|
|
47
|
+
);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** Date separator label: Today / Yesterday / "Mon, 5 Jan". */
|
|
51
|
+
export function dayLabel(iso: string, strings: Translator): string {
|
|
52
|
+
const now = new Date();
|
|
53
|
+
if (isSameDay(iso, now.toISOString())) return strings.t('time.today');
|
|
54
|
+
const yesterday = new Date(now.getTime() - 86_400_000);
|
|
55
|
+
if (isSameDay(iso, yesterday.toISOString())) return strings.t('time.yesterday');
|
|
56
|
+
const date = new Date(iso);
|
|
57
|
+
return date.toLocaleDateString(strings.locale, {
|
|
58
|
+
weekday: 'short',
|
|
59
|
+
month: 'short',
|
|
60
|
+
day: 'numeric',
|
|
61
|
+
...(date.getFullYear() === now.getFullYear() ? {} : { year: 'numeric' }),
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
|
|
27
65
|
/** Dev-mode prop validation with actionable messages. */
|
|
28
66
|
export function validateProps(props: {
|
|
29
67
|
appName?: unknown;
|