feat(frontend): analytics shows minutes online by hour of week and by day, sessions with start and end, says when last seen is hidden, chat status is live and in russian
This commit is contained in:
1 parent
f44597b2dc
commit
73b8903de1
11 files changed
+1275
-373
No files matched your search
@@ -0,0 +1,73 @@
|
||||
import { getCurrentPresence } from "#lib/api/endpoints.js";
|
||||
import type { PresenceSample } from "#lib/api/types.js";
|
||||
import { events } from "#lib/stores/events.svelte.js";
|
||||
|
||||
const CLOCK_INTERVAL = 30_000;
|
||||
|
||||
function createPresence() {
|
||||
let peerId: number | null = null;
|
||||
let key: string | null = null;
|
||||
let sample = $state<PresenceSample | null>(null);
|
||||
let now = $state(Date.now());
|
||||
let clock: ReturnType<typeof setInterval> | undefined;
|
||||
let token = 0;
|
||||
|
||||
async function refresh() {
|
||||
const id = peerId;
|
||||
token += 1;
|
||||
const current = token;
|
||||
if (id === null) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const next = await getCurrentPresence(id);
|
||||
if (current === token) {
|
||||
sample = next;
|
||||
now = Date.now();
|
||||
}
|
||||
} catch {
|
||||
if (current === token) {
|
||||
sample = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
events.subscribe((event) => {
|
||||
if (event.type === "presence" && event.peer_id === peerId) {
|
||||
token += 1;
|
||||
({ sample } = event);
|
||||
now = Date.now();
|
||||
}
|
||||
});
|
||||
events.onReconnect(() => {
|
||||
refresh();
|
||||
});
|
||||
|
||||
return {
|
||||
follow(accountId: number | null, id: number | null) {
|
||||
const next =
|
||||
accountId === null || id === null ? null : `${accountId}:${id}`;
|
||||
if (next === key) {
|
||||
return;
|
||||
}
|
||||
key = next;
|
||||
peerId = next === null ? null : id;
|
||||
sample = null;
|
||||
clearInterval(clock);
|
||||
if (peerId !== null) {
|
||||
clock = setInterval(() => {
|
||||
now = Date.now();
|
||||
}, CLOCK_INTERVAL);
|
||||
refresh();
|
||||
}
|
||||
},
|
||||
get now(): number {
|
||||
return now;
|
||||
},
|
||||
get sample(): PresenceSample | null {
|
||||
return sample;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export const presence = createPresence();
|
||||
Reference in new issue
Block a user