forked from Txio-labs/txio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtelemetry.ts
More file actions
79 lines (66 loc) · 1.93 KB
/
Copy pathtelemetry.ts
File metadata and controls
79 lines (66 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/**
* Minimal anonymous product telemetry.
* Events are queued in memory and flushed to localStorage for inspection.
* No network I/O — safe offline stub that still honors the Settings toggle.
*/
export type TelemetryEventName =
| 'app_boot'
| 'settings_changed'
| 'request_saved'
| 'request_sent'
| 'tab_opened';
export interface TelemetryEvent {
name: TelemetryEventName;
ts: number;
props?: Record<string, string | number | boolean | null>;
}
const QUEUE_KEY = 'txio_telemetry_queue';
const MAX_QUEUE = 100;
let enabled = true;
export const setTelemetryEnabled = (value: boolean): void => {
enabled = value;
};
export const isTelemetryEnabled = (): boolean => enabled;
const readQueue = (): TelemetryEvent[] => {
if (typeof window === 'undefined') return [];
try {
const raw = localStorage.getItem(QUEUE_KEY);
if (!raw) return [];
const parsed = JSON.parse(raw);
return Array.isArray(parsed) ? parsed : [];
} catch {
return [];
}
};
const writeQueue = (events: TelemetryEvent[]): void => {
if (typeof window === 'undefined') return;
try {
localStorage.setItem(
QUEUE_KEY,
JSON.stringify(events.slice(-MAX_QUEUE))
);
} catch {
// quota / private mode — drop silently
}
};
export const track = (
name: TelemetryEventName,
props?: TelemetryEvent['props']
): void => {
if (!enabled) return;
if (typeof window === 'undefined') return;
const event: TelemetryEvent = {
name,
ts: Date.now(),
...(props ? { props } : {})
};
const queue = readQueue();
queue.push(event);
writeQueue(queue);
};
/** Test/debug helper — not used by UI. */
export const getTelemetryQueue = (): TelemetryEvent[] => readQueue();
export const clearTelemetryQueue = (): void => {
if (typeof window === 'undefined') return;
localStorage.removeItem(QUEUE_KEY);
};