feat: full Discord-style presence tracking
CI / Build & Quality Checks (push) Failing after 5m42s

- Announce online immediately on app startup
- Idle detection: unavailable after 10 min of no input, online on return
- Tab visibility: unavailable when hidden, online when focused again
- Page close: offline via fetch+keepalive (survives unload without bfcache penalty)
- hidePresence setting: broadcasts offline and stops all tracking
- Added 'Hide Online Status' toggle in General settings

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-05-28 19:28:52 -04:00
parent f3023b34c8
commit f184f72286
4 changed files with 115 additions and 0 deletions
+98
View File
@@ -0,0 +1,98 @@
import { useEffect, useRef } from 'react';
import { useMatrixClient } from './useMatrixClient';
import { useSetting } from '../state/hooks/settings';
import { settingsAtom } from '../state/settings';
const IDLE_TIMEOUT_MS = 10 * 60 * 1000; // go unavailable after 10 min of no input
const ACTIVITY_THROTTLE_MS = 1000; // process activity events at most once per second
export function usePresenceUpdater() {
const mx = useMatrixClient();
const [hidePresence] = useSetting(settingsAtom, 'hidePresence');
const idleTimerRef = useRef<ReturnType<typeof setTimeout> | undefined>(undefined);
const isIdleRef = useRef(false);
const lastActivityRef = useRef(0);
useEffect(() => {
const setOnline = () =>
mx.setPresence({ presence: 'online' }).catch(() => undefined);
const setUnavailable = () =>
mx.setPresence({ presence: 'unavailable' }).catch(() => undefined);
// When the user hides presence, broadcast offline and do nothing else.
if (hidePresence) {
mx.setPresence({ presence: 'offline' }).catch(() => undefined);
return undefined;
}
const startIdleTimer = () => {
clearTimeout(idleTimerRef.current);
idleTimerRef.current = window.setTimeout(() => {
isIdleRef.current = true;
setUnavailable();
}, IDLE_TIMEOUT_MS);
};
const handleActivity = () => {
const now = Date.now();
if (now - lastActivityRef.current < ACTIVITY_THROTTLE_MS) return;
lastActivityRef.current = now;
// Coming back from idle while the tab is visible — go back online.
if (isIdleRef.current && !document.hidden) {
isIdleRef.current = false;
setOnline();
}
startIdleTimer();
};
const handleVisibilityChange = () => {
if (document.hidden) {
clearTimeout(idleTimerRef.current);
setUnavailable();
} else {
isIdleRef.current = false;
lastActivityRef.current = Date.now();
setOnline();
startIdleTimer();
}
};
// Best-effort offline on page close. fetch+keepalive survives the page unload
// unlike a regular async call, and avoids the bfcache penalty of beforeunload.
const handlePageHide = () => {
const userId = mx.getUserId();
const token = mx.getAccessToken();
// MatrixClient exposes baseUrl as a public property
const baseUrl = (mx as unknown as { baseUrl: string }).baseUrl;
if (!userId || !token || !baseUrl) return;
fetch(`${baseUrl}/_matrix/client/v3/presence/${encodeURIComponent(userId)}/status`, {
method: 'PUT',
headers: {
Authorization: `Bearer ${token}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({ presence: 'offline' }),
keepalive: true,
}).catch(() => undefined);
};
// Announce online immediately when the client mounts.
setOnline();
startIdleTimer();
const activityEvents = ['mousemove', 'keydown', 'touchstart', 'click', 'scroll'] as const;
activityEvents.forEach((e) => window.addEventListener(e, handleActivity, { passive: true }));
document.addEventListener('visibilitychange', handleVisibilityChange);
window.addEventListener('pagehide', handlePageHide);
return () => {
clearTimeout(idleTimerRef.current);
activityEvents.forEach((e) => window.removeEventListener(e, handleActivity));
document.removeEventListener('visibilitychange', handleVisibilityChange);
window.removeEventListener('pagehide', handlePageHide);
};
}, [mx, hidePresence]);
}