@unisim/sdk 0.186.0 → 0.187.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 +48 -94
- package/dist/AppSettingsShell.d.ts +1 -1
- package/dist/ChangelogMenu.js +1 -1
- package/dist/ChangelogMenu.js.map +1 -1
- package/dist/SignInDialog.d.ts.map +1 -1
- package/dist/SignInDialog.js +37 -12
- package/dist/SignInDialog.js.map +1 -1
- package/dist/SuiteSwitcher.d.ts +3 -2
- package/dist/SuiteSwitcher.d.ts.map +1 -1
- package/dist/SuiteSwitcher.js +11 -5
- package/dist/SuiteSwitcher.js.map +1 -1
- package/dist/UniversalNavBar.d.ts +3 -3
- package/dist/UniversalNavBar.d.ts.map +1 -1
- package/dist/UniversalNavBar.js +9 -17
- package/dist/UniversalNavBar.js.map +1 -1
- package/dist/UserCountBreakdown.d.ts +9 -18
- package/dist/UserCountBreakdown.d.ts.map +1 -1
- package/dist/UserCountBreakdown.js +9 -403
- package/dist/UserCountBreakdown.js.map +1 -1
- package/dist/UserCountLine.d.ts +9 -49
- package/dist/UserCountLine.d.ts.map +1 -1
- package/dist/UserCountLine.js +8 -218
- package/dist/UserCountLine.js.map +1 -1
- package/dist/UserProfile.d.ts +4 -4
- package/dist/UserProfile.d.ts.map +1 -1
- package/dist/UserProfile.js +2 -3
- package/dist/UserProfile.js.map +1 -1
- package/dist/chipStyles.d.ts.map +1 -1
- package/dist/chipStyles.js +5 -0
- package/dist/chipStyles.js.map +1 -1
- package/dist/i18n.d.ts.map +1 -1
- package/dist/i18n.js +0 -223
- package/dist/i18n.js.map +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/dist/presence.d.ts +85 -152
- package/dist/presence.d.ts.map +1 -1
- package/dist/presence.js +208 -499
- package/dist/presence.js.map +1 -1
- package/dist/provider.d.ts +20 -10
- package/dist/provider.d.ts.map +1 -1
- package/dist/provider.js +39 -20
- package/dist/provider.js.map +1 -1
- package/dist/recentApp.d.ts +8 -0
- package/dist/recentApp.d.ts.map +1 -0
- package/dist/recentApp.js +42 -0
- package/dist/recentApp.js.map +1 -0
- package/package.json +3 -2
package/dist/presence.js
CHANGED
|
@@ -1,113 +1,116 @@
|
|
|
1
1
|
// ──────────────────────────────────────────────────────────────────────────────
|
|
2
|
-
//
|
|
2
|
+
// The suite user count is RETIRED (0.187.0) — and what replaced it.
|
|
3
3
|
//
|
|
4
|
-
// James, 2026-
|
|
5
|
-
// how many
|
|
6
|
-
//
|
|
7
|
-
//
|
|
4
|
+
// James, 2026-10-12, on the suite privacy audit: "we only need an indication
|
|
5
|
+
// of how many live where it matters e.g. bipolar anonymous which we have in a
|
|
6
|
+
// non invasive way. For the rest it doesn't really matter. Any stats we can
|
|
7
|
+
// collect in friendlier ways is better."
|
|
8
8
|
//
|
|
9
|
-
//
|
|
10
|
-
//
|
|
11
|
-
//
|
|
12
|
-
//
|
|
13
|
-
//
|
|
14
|
-
//
|
|
15
|
-
//
|
|
16
|
-
//
|
|
17
|
-
// * `
|
|
18
|
-
//
|
|
19
|
-
//
|
|
20
|
-
//
|
|
21
|
-
// (migration 0184), which the line shows in a popup on a long press. The
|
|
22
|
-
// rows deliberately do not add up to the suite total: one account using
|
|
23
|
-
// four apps is four rows here and one person there.
|
|
9
|
+
// So the "X users (Y live)" line, its breakdown popup and the presence beat
|
|
10
|
+
// behind them (0.145.0 – 0.186.0: an install id in localStorage, a beat every
|
|
11
|
+
// 45 s, linked to the account when signed in) are gone:
|
|
12
|
+
// * no beat is sent, whatever `presence` says — `startPresence` /
|
|
13
|
+
// `beatPresence` are no-ops kept so callers (Universal Family, Universal
|
|
14
|
+
// AI) still compile;
|
|
15
|
+
// * the read functions answer null / nothing, and the components render
|
|
16
|
+
// nothing;
|
|
17
|
+
// * the permanent `unisim:install-id` and the cached figures are DELETED
|
|
18
|
+
// from the browser on load (`forgetLegacyInstallId`);
|
|
19
|
+
// * server side, migration 0276 makes `app_presence_beat` a no-op for every
|
|
20
|
+
// SDK already shipped, and empties app_presence.
|
|
24
21
|
//
|
|
25
|
-
// The
|
|
26
|
-
//
|
|
22
|
+
// ── The friendlier figure: a daily open count with no identifier ─────────────
|
|
23
|
+
// `countDailyOpen(supabase, product)` adds ONE to today's total for this
|
|
24
|
+
// product and kind of device (web / ios / android / windows / macos / linux),
|
|
25
|
+
// the first time the app is opened that UTC day on this device. The "already
|
|
26
|
+
// counted today" note — `unisim:opened:<product>` = the date — stays in this
|
|
27
|
+
// browser and is never sent. The RPC (`app_open_count`, 0276) takes the
|
|
28
|
+
// product and the platform and nothing else; the server keeps per-day totals
|
|
29
|
+
// only, which identify nobody. God mode reads them.
|
|
27
30
|
//
|
|
28
|
-
//
|
|
29
|
-
//
|
|
30
|
-
//
|
|
31
|
-
//
|
|
32
|
-
//
|
|
33
|
-
// used to be one. In an installed app (Capacitor, Electron) the beat goes at
|
|
34
|
-
// once: somebody chose that icon and waited for it to open, which is a good
|
|
35
|
-
// deal more deliberate than arriving at a URL.
|
|
31
|
+
// Same rules as before for what counts: our own traffic (test browsers, dev
|
|
32
|
+
// servers, and any install a platform administrator has signed in on — see
|
|
33
|
+
// isOwnDevice) is left out, and on the web the count waits for the first real
|
|
34
|
+
// interaction — a load on its own is not an open. An installed app counts when
|
|
35
|
+
// it starts.
|
|
36
36
|
//
|
|
37
|
-
// ⚠️
|
|
38
|
-
//
|
|
39
|
-
//
|
|
40
|
-
//
|
|
41
|
-
// ── web / native (0.158.0, migration 0187) ───────────────────────────────────
|
|
42
|
-
// Each beat says which kind of device it came from, and the count functions
|
|
43
|
-
// put each PERSON in one bucket — the platform of their most recent beat — so
|
|
44
|
-
// web + native is at most the total rather than counting a phone-and-laptop
|
|
45
|
-
// person twice. Anyone last seen before 0.158.0 shipped is in neither bucket,
|
|
46
|
-
// so the two can add up to less than the total; the popup's "How we count a
|
|
47
|
-
// user" fold says so. `null` means the figure is not available at all: an
|
|
48
|
-
// older database, or a row whose total comes from an app's own account count
|
|
49
|
-
// (migration 0185).
|
|
50
|
-
//
|
|
51
|
-
// ⚠️ Nothing React in this file, on purpose: Universal AI (Svelte) and Universal
|
|
52
|
-
// Family (no <UniversalProvider>) call these directly. The provider starts
|
|
53
|
-
// presence for every other app — see `presence` in UniversalConfig.
|
|
37
|
+
// ⚠️ Nothing React in this file, on purpose: Universal AI (Svelte) and
|
|
38
|
+
// Universal Family (no <UniversalProvider>) call it directly. The provider
|
|
39
|
+
// counts for every other app — see `openCount` in UniversalConfig.
|
|
54
40
|
// ──────────────────────────────────────────────────────────────────────────────
|
|
55
|
-
import { t } from './i18n.js';
|
|
56
41
|
import { capacitorPlatform, isNativeShell } from './sessionStorage.js';
|
|
57
|
-
|
|
42
|
+
import { isPlatformAdminIdentity } from './platformAdmin.js';
|
|
43
|
+
/** ⚠️ Legacy: the permanent per-browser id SDKs before 0.187.0 kept. Deleted on load. */
|
|
58
44
|
export const INSTALL_ID_KEY = 'unisim:install-id';
|
|
59
|
-
/**
|
|
60
|
-
export const PRESENCE_BEAT_MS = 45_000;
|
|
61
|
-
/** ⚠️ **Legacy, and only ever deleted now.** It used to remember whichever
|
|
62
|
-
* figure a viewer last switched the line to, which meant one tap made every
|
|
63
|
-
* app on that browser open on its own figure for ever. The line now always
|
|
64
|
-
* opens on the SUITE figure; `forgetStoredScope` in UserCountLine.tsx clears
|
|
65
|
-
* this on mount. Kept exported (and cleared) so browsers that stored a choice
|
|
66
|
-
* do not carry a dead key around. */
|
|
45
|
+
/** ⚠️ Legacy: kept only so it can be cleared. */
|
|
67
46
|
export const COUNT_SCOPE_KEY = 'unisim:user-count-scope';
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
const c = globalThis.crypto;
|
|
73
|
-
if (c && typeof c.randomUUID === 'function')
|
|
74
|
-
return c.randomUUID();
|
|
75
|
-
// RFC 4122 v4 from getRandomValues, or Math.random as a last resort — the id
|
|
76
|
-
// only has to be unlikely to collide, not unguessable.
|
|
77
|
-
const b = new Uint8Array(16);
|
|
78
|
-
if (c && typeof c.getRandomValues === 'function')
|
|
79
|
-
c.getRandomValues(b);
|
|
80
|
-
else
|
|
81
|
-
for (let i = 0; i < 16; i++)
|
|
82
|
-
b[i] = Math.floor(Math.random() * 256);
|
|
83
|
-
b[6] = (b[6] & 0x0f) | 0x40;
|
|
84
|
-
b[8] = (b[8] & 0x3f) | 0x80;
|
|
85
|
-
const h = Array.from(b, (x) => x.toString(16).padStart(2, '0')).join('');
|
|
86
|
-
return `${h.slice(0, 8)}-${h.slice(8, 12)}-${h.slice(12, 16)}-${h.slice(16, 20)}-${h.slice(20)}`;
|
|
87
|
-
}
|
|
88
|
-
const UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
47
|
+
/** ⚠️ Retired with the beat; kept for compatibility. */
|
|
48
|
+
export const PRESENCE_BEAT_MS = 45_000;
|
|
49
|
+
/** localStorage key prefix for "this app was counted today" — the date only. */
|
|
50
|
+
export const OPENED_KEY_PREFIX = 'unisim:opened:';
|
|
89
51
|
/**
|
|
90
|
-
*
|
|
91
|
-
*
|
|
92
|
-
*
|
|
52
|
+
* localStorage key set once a platform administrator has signed in on this
|
|
53
|
+
* install (universal-platform 0285). From then on the install is ours and is
|
|
54
|
+
* never counted, signed in or not. Stays in this browser; never sent.
|
|
93
55
|
*/
|
|
94
|
-
|
|
95
|
-
|
|
56
|
+
export const OWN_DEVICE_KEY = 'unisim:own-device';
|
|
57
|
+
const LEGACY_KEYS = [
|
|
58
|
+
INSTALL_ID_KEY,
|
|
59
|
+
COUNT_SCOPE_KEY,
|
|
60
|
+
'unisim:presence-day-id',
|
|
61
|
+
'unisim:user-counts:by-product',
|
|
62
|
+
'unisim:user-counts:universal-ids',
|
|
63
|
+
];
|
|
64
|
+
/**
|
|
65
|
+
* Delete everything the retired user count left in this browser: the
|
|
66
|
+
* permanent install id and the cached figures. The provider calls it on
|
|
67
|
+
* mount in every app. Safe to call anywhere, any number of times.
|
|
68
|
+
*/
|
|
69
|
+
export function forgetLegacyInstallId() {
|
|
96
70
|
if (typeof window === 'undefined')
|
|
97
|
-
return
|
|
71
|
+
return;
|
|
98
72
|
try {
|
|
99
|
-
const
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
memoryInstallId ??= randomUuid();
|
|
108
|
-
return memoryInstallId;
|
|
73
|
+
const ls = window.localStorage;
|
|
74
|
+
for (const k of LEGACY_KEYS)
|
|
75
|
+
ls.removeItem(k);
|
|
76
|
+
for (let i = ls.length - 1; i >= 0; i--) {
|
|
77
|
+
const k = ls.key(i);
|
|
78
|
+
if (k && k.startsWith('unisim:user-counts:'))
|
|
79
|
+
ls.removeItem(k);
|
|
80
|
+
}
|
|
109
81
|
}
|
|
82
|
+
catch { /* ignore */ }
|
|
110
83
|
}
|
|
84
|
+
/** @deprecated Retired in 0.187.0: there is no install id any more. Always null. */
|
|
85
|
+
export function getInstallId() { return null; }
|
|
86
|
+
/** @deprecated Retired in 0.187.0: sends nothing. */
|
|
87
|
+
export function beatPresence(_supabase, _product) {
|
|
88
|
+
return Promise.resolve(false);
|
|
89
|
+
}
|
|
90
|
+
/** @deprecated Retired in 0.187.0: sends nothing; returns a no-op stop function. */
|
|
91
|
+
export function startPresence(_supabase, _product, _options) {
|
|
92
|
+
return () => { };
|
|
93
|
+
}
|
|
94
|
+
/** @deprecated Retired in 0.187.0: always null. */
|
|
95
|
+
export function cachedUserCounts(_product) { return null; }
|
|
96
|
+
/** @deprecated Retired in 0.187.0: always null. */
|
|
97
|
+
export async function fetchUserCounts(_supabase, _product, _scope = 'app') { return null; }
|
|
98
|
+
/** @deprecated Retired in 0.187.0: always null. */
|
|
99
|
+
export function cachedUserCountsByProduct() { return null; }
|
|
100
|
+
/** @deprecated Retired in 0.187.0: always null. */
|
|
101
|
+
export async function fetchUserCountsByProduct(_supabase) { return null; }
|
|
102
|
+
/** @deprecated Retired in 0.187.0: always null. */
|
|
103
|
+
export function cachedUniversalIdCounts() { return null; }
|
|
104
|
+
/** @deprecated Retired in 0.187.0: always null. */
|
|
105
|
+
export async function fetchUniversalIdCounts(_supabase) { return null; }
|
|
106
|
+
/** @deprecated Retired in 0.187.0: always ''. */
|
|
107
|
+
export function formatUserCounts(_language, _counts, _scope = 'app', _appName) { return ''; }
|
|
108
|
+
/** @deprecated Retired in 0.187.0: always []. */
|
|
109
|
+
export function userCountParts(_language, _counts) { return []; }
|
|
110
|
+
/** @deprecated Retired in 0.187.0: always []. */
|
|
111
|
+
export function universalIdParts(_language, _counts) { return []; }
|
|
112
|
+
/** @deprecated Retired in 0.187.0: always []. */
|
|
113
|
+
export function userCountFigures(_language, _counts) { return []; }
|
|
111
114
|
// ── Which kind of device this is ─────────────────────────────────────────────
|
|
112
115
|
/**
|
|
113
116
|
* True in anything the person INSTALLED — a Capacitor container or an Electron
|
|
@@ -208,11 +211,56 @@ export function isOwnTraffic() {
|
|
|
208
211
|
return false;
|
|
209
212
|
if (typeof navigator !== 'undefined' && navigator.webdriver === true)
|
|
210
213
|
return true;
|
|
214
|
+
if (isOwnDevice())
|
|
215
|
+
return true;
|
|
211
216
|
const loc = window.location;
|
|
212
217
|
if (isInstalledApp())
|
|
213
218
|
return !!loc?.port && /^https?:$/.test(loc.protocol ?? '') && isDevHost(loc.hostname ?? '');
|
|
214
219
|
return isDevHost(loc?.hostname ?? '');
|
|
215
220
|
}
|
|
221
|
+
// ── A platform administrator's install is ours too (with 0285) ──────────────
|
|
222
|
+
/**
|
|
223
|
+
* James, 2026-10-12: "Yes, exclude admins" — leave the platform
|
|
224
|
+
* administrators, and every install they have signed in on, out of the
|
|
225
|
+
* figures. The server already ignores an open from a signed-in administrator
|
|
226
|
+
* (universal-platform 0285); this remembers the install, so its later opens
|
|
227
|
+
* while signed out or as a guest are not counted either.
|
|
228
|
+
*
|
|
229
|
+
* ⚠️ An install's opens BEFORE an administrator first signs in on it are
|
|
230
|
+
* counted: nothing marks it yet.
|
|
231
|
+
*/
|
|
232
|
+
export function isOwnDevice() {
|
|
233
|
+
if (typeof window === 'undefined')
|
|
234
|
+
return false;
|
|
235
|
+
try {
|
|
236
|
+
return window.localStorage.getItem(OWN_DEVICE_KEY) === '1';
|
|
237
|
+
}
|
|
238
|
+
catch {
|
|
239
|
+
return false;
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
/** Mark this install as ours when `user` is a platform administrator. Returns whether it did. */
|
|
243
|
+
export function noteOwnDevice(user) {
|
|
244
|
+
if (typeof window === 'undefined' || !isPlatformAdminIdentity(user))
|
|
245
|
+
return false;
|
|
246
|
+
try {
|
|
247
|
+
window.localStorage.setItem(OWN_DEVICE_KEY, '1');
|
|
248
|
+
}
|
|
249
|
+
catch { /* ignore */ }
|
|
250
|
+
return true;
|
|
251
|
+
}
|
|
252
|
+
/** The signed-in user from the client's stored session — a local read, no request. */
|
|
253
|
+
async function sessionUser(supabase) {
|
|
254
|
+
try {
|
|
255
|
+
if (typeof supabase?.auth?.getSession !== 'function')
|
|
256
|
+
return null;
|
|
257
|
+
const { data } = await supabase.auth.getSession();
|
|
258
|
+
return data?.session?.user ?? null;
|
|
259
|
+
}
|
|
260
|
+
catch {
|
|
261
|
+
return null;
|
|
262
|
+
}
|
|
263
|
+
}
|
|
216
264
|
function isDevHost(host) {
|
|
217
265
|
const h = host.toLowerCase().replace(/^\[|\]$/g, '');
|
|
218
266
|
if (h === 'localhost' || h.endsWith('.localhost') || h.endsWith('.local'))
|
|
@@ -290,430 +338,91 @@ export function onFirstInteraction(fn) {
|
|
|
290
338
|
disarmInteraction();
|
|
291
339
|
};
|
|
292
340
|
}
|
|
293
|
-
// The
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
let warned = false;
|
|
298
|
-
/** Say it once — a refused beat repeats every 45 s. */
|
|
299
|
-
function warnOnce(what, err) {
|
|
300
|
-
if (warned)
|
|
301
|
-
return;
|
|
302
|
-
warned = true;
|
|
303
|
-
console.warn(`[unisim/sdk] user count: ${what} failed (not retried loudly)`, err);
|
|
304
|
-
}
|
|
305
|
-
/**
|
|
306
|
-
* ⚠️ **The one ordering hazard in this release.** PostgREST resolves an RPC by
|
|
307
|
-
* its ARGUMENT NAMES, so sending `p_platform` to a database that has not had
|
|
308
|
-
* migration 0187 applied is not ignored — it is "function not found", and every
|
|
309
|
-
* beat fails. The SDK ships to npm and reaches apps on their own schedule,
|
|
310
|
-
* which is exactly the window where that can happen, so a beat refused for that
|
|
311
|
-
* reason is retried WITHOUT the platform and the argument is dropped for the
|
|
312
|
-
* rest of the page. The count keeps growing; it simply records no platform
|
|
313
|
-
* until the migration lands, which is the same state every pre-0187 row is in.
|
|
314
|
-
*
|
|
315
|
-
* `null` while nothing has been refused — the check is only worth making once.
|
|
316
|
-
*/
|
|
317
|
-
let sendPlatform = null;
|
|
318
|
-
/** PostgREST's "no function with these arguments", and Postgres' own. */
|
|
319
|
-
function isUnknownFunction(error) {
|
|
320
|
-
const code = error?.code;
|
|
321
|
-
return code === 'PGRST202' || code === '42883';
|
|
341
|
+
// ── The daily open count ─────────────────────────────────────────────────────
|
|
342
|
+
/** Today's date in UTC, `YYYY-MM-DD`. */
|
|
343
|
+
function utcDay(now = new Date()) {
|
|
344
|
+
return now.toISOString().slice(0, 10);
|
|
322
345
|
}
|
|
346
|
+
const counting = new Set();
|
|
323
347
|
/**
|
|
324
|
-
*
|
|
348
|
+
* Add this open to today's total for `product`, once per UTC day on this
|
|
349
|
+
* device. Never throws; resolves whether a count was sent. See the header for
|
|
350
|
+
* what is (and is not) sent.
|
|
325
351
|
*/
|
|
326
|
-
export function
|
|
327
|
-
|
|
352
|
+
export async function countDailyOpen(supabase, product) {
|
|
353
|
+
if (typeof window === 'undefined' || typeof supabase?.rpc !== 'function')
|
|
354
|
+
return false;
|
|
328
355
|
if (isOwnTraffic())
|
|
329
|
-
return
|
|
330
|
-
const
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
return false;
|
|
349
|
-
}
|
|
350
|
-
beatLanded.add(product);
|
|
351
|
-
return true;
|
|
352
|
-
}
|
|
353
|
-
if (error) {
|
|
354
|
-
warnOnce('presence beat', error);
|
|
356
|
+
return false;
|
|
357
|
+
const key = OPENED_KEY_PREFIX + product;
|
|
358
|
+
const today = utcDay();
|
|
359
|
+
try {
|
|
360
|
+
if (window.localStorage.getItem(key) === today)
|
|
361
|
+
return false;
|
|
362
|
+
}
|
|
363
|
+
catch {
|
|
364
|
+
// No storage at all: counting every load would inflate the figure, so
|
|
365
|
+
// a browser that cannot remember is not counted.
|
|
366
|
+
return false;
|
|
367
|
+
}
|
|
368
|
+
if (counting.has(product))
|
|
369
|
+
return false;
|
|
370
|
+
counting.add(product);
|
|
371
|
+
try {
|
|
372
|
+
// A platform administrator is not a user (0285 also refuses it
|
|
373
|
+
// server side), and neither is any install they have signed in on.
|
|
374
|
+
if (noteOwnDevice(await sessionUser(supabase)))
|
|
355
375
|
return false;
|
|
376
|
+
const platform = devicePlatform();
|
|
377
|
+
const { error } = await supabase.rpc('app_open_count', { p_product: product, p_platform: platform });
|
|
378
|
+
if (error)
|
|
379
|
+
return false;
|
|
380
|
+
try {
|
|
381
|
+
window.localStorage.setItem(key, today);
|
|
356
382
|
}
|
|
357
|
-
|
|
358
|
-
sendPlatform = true;
|
|
359
|
-
beatLanded.add(product);
|
|
383
|
+
catch { /* ignore */ }
|
|
360
384
|
return true;
|
|
361
|
-
}
|
|
362
|
-
|
|
363
|
-
|
|
385
|
+
}
|
|
386
|
+
catch {
|
|
387
|
+
return false;
|
|
388
|
+
}
|
|
389
|
+
finally {
|
|
390
|
+
counting.delete(product);
|
|
391
|
+
}
|
|
364
392
|
}
|
|
365
393
|
/**
|
|
366
|
-
*
|
|
367
|
-
*
|
|
368
|
-
*
|
|
369
|
-
*
|
|
370
|
-
* A hidden page does not beat: a backgrounded tab is not someone using the app,
|
|
371
|
-
* and it drops out of "live" within two minutes on its own.
|
|
394
|
+
* Count today's open — on the web at the first interaction, in an installed
|
|
395
|
+
* app straight away — and again if the app is still open (or brought back)
|
|
396
|
+
* after midnight UTC. Returns a stop function.
|
|
372
397
|
*/
|
|
373
|
-
export function
|
|
398
|
+
export function startOpenCount(supabase, product) {
|
|
374
399
|
if (typeof window === 'undefined' || isOwnTraffic())
|
|
375
400
|
return () => { };
|
|
376
|
-
const
|
|
377
|
-
const
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
tick(); };
|
|
381
|
-
let timer = null;
|
|
401
|
+
const run = () => { void countDailyOpen(supabase, product); };
|
|
402
|
+
const onVisible = () => { if (document.visibilityState !== 'hidden')
|
|
403
|
+
run(); };
|
|
404
|
+
let started = false;
|
|
382
405
|
const begin = () => {
|
|
383
|
-
if (
|
|
406
|
+
if (started)
|
|
384
407
|
return;
|
|
385
|
-
|
|
386
|
-
|
|
408
|
+
started = true;
|
|
409
|
+
run();
|
|
387
410
|
document.addEventListener('visibilitychange', onVisible);
|
|
388
411
|
};
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
412
|
+
const wait = isInstalledApp() ? (begin(), () => { }) : onFirstInteraction(begin);
|
|
413
|
+
// An administrator signing in later in this visit marks the install too, so
|
|
414
|
+
// tomorrow's open (signed in or not) is not counted.
|
|
415
|
+
let unwatch = () => { };
|
|
416
|
+
try {
|
|
417
|
+
const sub = supabase?.auth?.onAuthStateChange?.((_event, session) => { noteOwnDevice(session?.user); });
|
|
418
|
+
if (sub?.data?.subscription)
|
|
419
|
+
unwatch = () => sub.data.subscription.unsubscribe();
|
|
420
|
+
}
|
|
421
|
+
catch { /* a client without auth: nothing to watch */ }
|
|
393
422
|
return () => {
|
|
394
423
|
wait();
|
|
395
|
-
|
|
396
|
-
window.clearInterval(timer);
|
|
397
|
-
timer = null;
|
|
398
|
-
}
|
|
424
|
+
unwatch();
|
|
399
425
|
document.removeEventListener('visibilitychange', onVisible);
|
|
400
426
|
};
|
|
401
427
|
}
|
|
402
|
-
/** A `{ total, live, web, native }` out of anything, or null. The two splits
|
|
403
|
-
* are `null` rather than 0 when absent: 0 is a claim ("nobody is on the web"),
|
|
404
|
-
* null is the absence of one. */
|
|
405
|
-
function readCounts(row) {
|
|
406
|
-
const r = row;
|
|
407
|
-
const total = Number(r?.total);
|
|
408
|
-
const live = Number(r?.live);
|
|
409
|
-
if (!Number.isFinite(total) || !Number.isFinite(live))
|
|
410
|
-
return null;
|
|
411
|
-
return {
|
|
412
|
-
total,
|
|
413
|
-
live,
|
|
414
|
-
web: optionalCount(r?.web),
|
|
415
|
-
native: optionalCount(r?.native),
|
|
416
|
-
ios: optionalCount(r?.ios),
|
|
417
|
-
android: optionalCount(r?.android),
|
|
418
|
-
desktop: optionalCount(r?.desktop),
|
|
419
|
-
};
|
|
420
|
-
}
|
|
421
|
-
function optionalCount(value) {
|
|
422
|
-
if (value === null || value === undefined)
|
|
423
|
-
return null;
|
|
424
|
-
const n = Number(value);
|
|
425
|
-
return Number.isFinite(n) ? n : null;
|
|
426
|
-
}
|
|
427
|
-
/** The last counts this origin read for `product` (or, with 'suite', for the
|
|
428
|
-
* whole suite), or null. */
|
|
429
|
-
export function cachedUserCounts(product) {
|
|
430
|
-
if (typeof window === 'undefined')
|
|
431
|
-
return null;
|
|
432
|
-
try {
|
|
433
|
-
// ⚠️ An entry written before 0.158.0 has no web/native in it. readCounts
|
|
434
|
-
// answers null for both rather than 0, so the cached figure paints without
|
|
435
|
-
// a split and gains one when the fresh read lands.
|
|
436
|
-
return readCounts(JSON.parse(window.localStorage.getItem(countsKey(product)) ?? 'null'));
|
|
437
|
-
}
|
|
438
|
-
catch { /* ignore */ }
|
|
439
|
-
return null;
|
|
440
|
-
}
|
|
441
|
-
/**
|
|
442
|
-
* Read `{ total, live, web, native }` for `product`. Waits (up to 3 s) for this
|
|
443
|
-
* page's pending beat so the reader is in the figure. Resolves null on any
|
|
444
|
-
* failure — the caller hides its line rather than showing a wrong number.
|
|
445
|
-
*/
|
|
446
|
-
export async function fetchUserCounts(supabase, product, scope = 'app') {
|
|
447
|
-
if (typeof supabase?.rpc !== 'function')
|
|
448
|
-
return null;
|
|
449
|
-
const beat = pendingBeat.get(product);
|
|
450
|
-
if (beat)
|
|
451
|
-
await Promise.race([beat, new Promise((r) => setTimeout(r, 3000))]);
|
|
452
|
-
try {
|
|
453
|
-
const { data, error } = scope === 'suite'
|
|
454
|
-
? await supabase.rpc('suite_user_counts')
|
|
455
|
-
: await supabase.rpc('app_user_counts', { p_product: product });
|
|
456
|
-
if (error) {
|
|
457
|
-
warnOnce('count read', error);
|
|
458
|
-
return null;
|
|
459
|
-
}
|
|
460
|
-
const read = readCounts(Array.isArray(data) ? data[0] : data);
|
|
461
|
-
if (!read)
|
|
462
|
-
return null;
|
|
463
|
-
// Somebody is reading this, and if their beat landed they are live — a
|
|
464
|
-
// count that raced the beat must not tell them "(0 live)".
|
|
465
|
-
const self = beatLanded.has(product) ? 1 : 0;
|
|
466
|
-
const counts = {
|
|
467
|
-
...read,
|
|
468
|
-
live: Math.max(read.live, self),
|
|
469
|
-
total: Math.max(read.total, read.live, self),
|
|
470
|
-
};
|
|
471
|
-
try {
|
|
472
|
-
window.localStorage.setItem(countsKey(scope === 'suite' ? 'suite' : product), JSON.stringify(counts));
|
|
473
|
-
}
|
|
474
|
-
catch { /* ignore */ }
|
|
475
|
-
return counts;
|
|
476
|
-
}
|
|
477
|
-
catch (err) {
|
|
478
|
-
warnOnce('count read', err);
|
|
479
|
-
return null;
|
|
480
|
-
}
|
|
481
|
-
}
|
|
482
|
-
/** The last breakdown this origin read, or null. Same job as
|
|
483
|
-
* {@link cachedUserCounts}: the popup paints the previous answer at once and
|
|
484
|
-
* swaps it for the fresh one when it lands, rather than opening empty. */
|
|
485
|
-
export function cachedUserCountsByProduct() {
|
|
486
|
-
if (typeof window === 'undefined')
|
|
487
|
-
return null;
|
|
488
|
-
try {
|
|
489
|
-
const raw = JSON.parse(window.localStorage.getItem(BREAKDOWN_KEY) ?? 'null');
|
|
490
|
-
const rows = Array.isArray(raw) ? raw.map(asProductCounts).filter(isRow) : [];
|
|
491
|
-
return rows.length > 0 ? rows : null;
|
|
492
|
-
}
|
|
493
|
-
catch { /* ignore */ }
|
|
494
|
-
return null;
|
|
495
|
-
}
|
|
496
|
-
function isRow(row) {
|
|
497
|
-
return row !== null;
|
|
498
|
-
}
|
|
499
|
-
function asProductCounts(row) {
|
|
500
|
-
const product = row?.product;
|
|
501
|
-
if (typeof product !== 'string')
|
|
502
|
-
return null;
|
|
503
|
-
const counts = readCounts(row);
|
|
504
|
-
return counts ? { product, ...counts } : null;
|
|
505
|
-
}
|
|
506
|
-
/**
|
|
507
|
-
* `{ product, total, live, web, native }` for every product anybody has ever
|
|
508
|
-
* opened, biggest first — the breakdown behind the suite figure (migration
|
|
509
|
-
* 0184).
|
|
510
|
-
*
|
|
511
|
-
* Resolves null on any failure, exactly like {@link fetchUserCounts}, so a
|
|
512
|
-
* caller shows nothing rather than a wrong number. ⚠️ **That includes an app
|
|
513
|
-
* running against a database where 0184 has not been applied yet** — the SDK
|
|
514
|
-
* ships to npm and reaches consumers on their own schedule, so "the function
|
|
515
|
-
* is not there" is a normal state, not an error to shout about. It warns once,
|
|
516
|
-
* like every other read here.
|
|
517
|
-
*
|
|
518
|
-
* ⚠️ The rows do NOT sum to `fetchUserCounts(_, _, 'suite').total`: an account
|
|
519
|
-
* using four apps is four rows here and one person there. Never add them up to
|
|
520
|
-
* produce a total — the suite figure is the total.
|
|
521
|
-
*
|
|
522
|
-
* ⚠️ `web`/`native` are null on a row whose total comes from that app's own
|
|
523
|
-
* account counter (migration 0185, the Bipolar apps): the counter has no
|
|
524
|
-
* platform in it, so a split beside it would describe a different, smaller
|
|
525
|
-
* population than the total next to it.
|
|
526
|
-
*/
|
|
527
|
-
export async function fetchUserCountsByProduct(supabase) {
|
|
528
|
-
if (typeof supabase?.rpc !== 'function')
|
|
529
|
-
return null;
|
|
530
|
-
try {
|
|
531
|
-
const { data, error } = await supabase.rpc('suite_user_counts_by_product');
|
|
532
|
-
if (error) {
|
|
533
|
-
warnOnce('count breakdown read', error);
|
|
534
|
-
return null;
|
|
535
|
-
}
|
|
536
|
-
const rows = (Array.isArray(data) ? data : []);
|
|
537
|
-
const counts = [];
|
|
538
|
-
for (const row of rows) {
|
|
539
|
-
const parsed = asProductCounts(row);
|
|
540
|
-
if (parsed)
|
|
541
|
-
counts.push(parsed);
|
|
542
|
-
}
|
|
543
|
-
if (counts.length === 0)
|
|
544
|
-
return null;
|
|
545
|
-
// The server already orders by total; sorting again costs nothing and means
|
|
546
|
-
// the popup is right even if a future caller hands rows over unordered.
|
|
547
|
-
counts.sort((a, b) => b.total - a.total || a.product.localeCompare(b.product));
|
|
548
|
-
try {
|
|
549
|
-
window.localStorage.setItem(BREAKDOWN_KEY, JSON.stringify(counts));
|
|
550
|
-
}
|
|
551
|
-
catch { /* ignore */ }
|
|
552
|
-
return counts;
|
|
553
|
-
}
|
|
554
|
-
catch (err) {
|
|
555
|
-
warnOnce('count breakdown read', err);
|
|
556
|
-
return null;
|
|
557
|
-
}
|
|
558
|
-
}
|
|
559
|
-
const ID_COUNTS_KEY = 'unisim:user-counts:universal-ids';
|
|
560
|
-
function readIdCounts(row) {
|
|
561
|
-
const r = row;
|
|
562
|
-
const created = Number(r?.created);
|
|
563
|
-
const live = Number(r?.live);
|
|
564
|
-
if (!Number.isFinite(created) || !Number.isFinite(live))
|
|
565
|
-
return null;
|
|
566
|
-
return { created, live };
|
|
567
|
-
}
|
|
568
|
-
/** The last Universal ID counts this origin read, or null. */
|
|
569
|
-
export function cachedUniversalIdCounts() {
|
|
570
|
-
if (typeof window === 'undefined')
|
|
571
|
-
return null;
|
|
572
|
-
try {
|
|
573
|
-
return readIdCounts(JSON.parse(window.localStorage.getItem(ID_COUNTS_KEY) ?? 'null'));
|
|
574
|
-
}
|
|
575
|
-
catch { /* ignore */ }
|
|
576
|
-
return null;
|
|
577
|
-
}
|
|
578
|
-
/**
|
|
579
|
-
* `{ created, live }` for Universal IDs — the row at the top of the breakdown
|
|
580
|
-
* (James, 2026-09-27). Resolves null on any failure, including a database
|
|
581
|
-
* without migration 0188, so the popup leaves the row out rather than showing
|
|
582
|
-
* a wrong number.
|
|
583
|
-
*/
|
|
584
|
-
export async function fetchUniversalIdCounts(supabase) {
|
|
585
|
-
if (typeof supabase?.rpc !== 'function')
|
|
586
|
-
return null;
|
|
587
|
-
try {
|
|
588
|
-
const { data, error } = await supabase.rpc('universal_id_counts');
|
|
589
|
-
if (error) {
|
|
590
|
-
warnOnce('universal id read', error);
|
|
591
|
-
return null;
|
|
592
|
-
}
|
|
593
|
-
const counts = readIdCounts(Array.isArray(data) ? data[0] : data);
|
|
594
|
-
if (!counts)
|
|
595
|
-
return null;
|
|
596
|
-
try {
|
|
597
|
-
window.localStorage.setItem(ID_COUNTS_KEY, JSON.stringify(counts));
|
|
598
|
-
}
|
|
599
|
-
catch { /* ignore */ }
|
|
600
|
-
return counts;
|
|
601
|
-
}
|
|
602
|
-
catch (err) {
|
|
603
|
-
warnOnce('universal id read', err);
|
|
604
|
-
return null;
|
|
605
|
-
}
|
|
606
|
-
}
|
|
607
|
-
/** "52 created", "2 live" — the Universal IDs row's figures, as separate
|
|
608
|
-
* phrases for the same reason as {@link userCountParts}. */
|
|
609
|
-
export function universalIdParts(language, counts) {
|
|
610
|
-
const nf = numberFormat(language);
|
|
611
|
-
return [
|
|
612
|
-
t(language, 'users.part_created').replace('{n}', nf.format(counts.created)),
|
|
613
|
-
t(language, 'users.part_live').replace('{n}', nf.format(counts.live)),
|
|
614
|
-
];
|
|
615
|
-
}
|
|
616
|
-
/**
|
|
617
|
-
* "1,234 PDF users (5 live)" — or, for the 'suite' scope, "1,234 UNI·SIM Users
|
|
618
|
-
* (5 live)" (James, 2026-09-27) — in `language`, with the numbers grouped the
|
|
619
|
-
* way that language groups them.
|
|
620
|
-
*
|
|
621
|
-
* `appName` is the app's short name ("PDF", "Fire Assess"; see
|
|
622
|
-
* `productShortName`). It is passed in because this file imports nothing
|
|
623
|
-
* React, and the names live beside the switcher. Without it the app line reads
|
|
624
|
-
* "1,234 users (5 live)".
|
|
625
|
-
*/
|
|
626
|
-
export function formatUserCounts(language, counts, scope = 'app', appName) {
|
|
627
|
-
const nf = numberFormat(language);
|
|
628
|
-
const one = counts.total === 1;
|
|
629
|
-
const key = scope === 'suite'
|
|
630
|
-
? (one ? 'users.suite_one' : 'users.suite_other')
|
|
631
|
-
: (one ? 'users.count_one' : 'users.count_other');
|
|
632
|
-
return t(language, key)
|
|
633
|
-
.replace('{total}', nf.format(counts.total))
|
|
634
|
-
.replace('{live}', nf.format(counts.live))
|
|
635
|
-
.replace('{app}', appName?.trim() || '')
|
|
636
|
-
// No name: "1,234 users", "1 234 utilisateurs de (…)". Tidy what the
|
|
637
|
-
// empty slot leaves behind rather than keeping a second set of strings.
|
|
638
|
-
.replace(/\s{2,}/g, ' ')
|
|
639
|
-
.replace(/ (de|di|von) (?=\()/, ' ');
|
|
640
|
-
}
|
|
641
|
-
/**
|
|
642
|
-
* The figures behind one row of the breakdown, as separate phrases — "1,234
|
|
643
|
-
* users", "900 web", "334 native", "5 live" — so the caller can lay them out
|
|
644
|
-
* and let them WRAP rather than measuring a single string (James, 2026-09-21:
|
|
645
|
-
* "show web | native | live if there's enough space").
|
|
646
|
-
*
|
|
647
|
-
* The split is left out entirely when it is not available, rather than shown as
|
|
648
|
-
* "0 web · 0 native": see {@link UserCounts.web}.
|
|
649
|
-
*/
|
|
650
|
-
export function userCountParts(language, counts) {
|
|
651
|
-
const nf = numberFormat(language);
|
|
652
|
-
const say = (key, n) => t(language, key).replace('{n}', nf.format(n));
|
|
653
|
-
return splitFigures(counts).map(([kind, n]) => say(PART_KEYS[kind](n), n));
|
|
654
|
-
}
|
|
655
|
-
const PART_KEYS = {
|
|
656
|
-
total: (n) => (n === 1 ? 'users.part_total_one' : 'users.part_total'),
|
|
657
|
-
web: () => 'users.part_web',
|
|
658
|
-
native: () => 'users.part_native',
|
|
659
|
-
ios: () => 'users.part_ios',
|
|
660
|
-
android: () => 'users.part_android',
|
|
661
|
-
desktop: () => 'users.part_desktop',
|
|
662
|
-
live: () => 'users.part_live',
|
|
663
|
-
};
|
|
664
|
-
const FIGURE_EMOJI = {
|
|
665
|
-
total: '🧑🤝🧑', web: '🕸️', native: '📱', ios: '🍎', android: '🤖', desktop: '💻', live: '🟢',
|
|
666
|
-
};
|
|
667
|
-
/**
|
|
668
|
-
* Which figures a row shows, in order. The native figure is broken into iOS /
|
|
669
|
-
* Android where the database gives that split (0261, 0.180.10) — plus the
|
|
670
|
-
* desktop apps, but only when somebody is actually on one, so a row of phone
|
|
671
|
-
* apps does not carry a "0 💻" that reads like a missing count.
|
|
672
|
-
*/
|
|
673
|
-
function splitFigures(counts) {
|
|
674
|
-
const out = [['total', counts.total]];
|
|
675
|
-
if (typeof counts.web === 'number' && typeof counts.native === 'number') {
|
|
676
|
-
out.push(['web', counts.web]);
|
|
677
|
-
if (typeof counts.ios === 'number' && typeof counts.android === 'number') {
|
|
678
|
-
out.push(['ios', counts.ios], ['android', counts.android]);
|
|
679
|
-
const desktop = typeof counts.desktop === 'number'
|
|
680
|
-
? counts.desktop
|
|
681
|
-
: counts.native - counts.ios - counts.android;
|
|
682
|
-
if (desktop > 0)
|
|
683
|
-
out.push(['desktop', desktop]);
|
|
684
|
-
}
|
|
685
|
-
else {
|
|
686
|
-
out.push(['native', counts.native]);
|
|
687
|
-
}
|
|
688
|
-
}
|
|
689
|
-
out.push(['live', counts.live]);
|
|
690
|
-
return out;
|
|
691
|
-
}
|
|
692
|
-
/**
|
|
693
|
-
* James, 2026-09-29: "use emojis to make it more condensed: 57 🧑🤝🧑 | 51 🕸️ |
|
|
694
|
-
* 6 ☎️ | 1 🟢" — and later that day, "replace red telephone counter with
|
|
695
|
-
* 📱" (0.168.0). The same figures as {@link userCountParts} (and
|
|
696
|
-
* {@link universalIdParts} when `counts` is a Universal ID count), each as a
|
|
697
|
-
* number and an emoji, with the worded phrase kept as its spoken label so a
|
|
698
|
-
* screen reader still says "51 web" rather than "51 spider web".
|
|
699
|
-
*/
|
|
700
|
-
export function userCountFigures(language, counts) {
|
|
701
|
-
const nf = numberFormat(language);
|
|
702
|
-
const words = 'created' in counts ? universalIdParts(language, counts) : userCountParts(language, counts);
|
|
703
|
-
const figures = 'created' in counts
|
|
704
|
-
? [['🪪', counts.created], ['🟢', counts.live]]
|
|
705
|
-
: splitFigures(counts).map(([kind, n]) => [FIGURE_EMOJI[kind], n]);
|
|
706
|
-
const emoji = figures.map(([e]) => e);
|
|
707
|
-
const numbers = figures.map(([, n]) => n);
|
|
708
|
-
const text = (n, i) => `${nf.format(n)} ${emoji[i]}`;
|
|
709
|
-
return numbers.map((n, i) => ({ text: text(n, i), label: words[i] ?? text(n, i) }));
|
|
710
|
-
}
|
|
711
|
-
function numberFormat(language) {
|
|
712
|
-
try {
|
|
713
|
-
return new Intl.NumberFormat(language ?? undefined);
|
|
714
|
-
}
|
|
715
|
-
catch {
|
|
716
|
-
return new Intl.NumberFormat('en');
|
|
717
|
-
}
|
|
718
|
-
}
|
|
719
428
|
//# sourceMappingURL=presence.js.map
|