99 lines
3.5 KiB
TypeScript
99 lines
3.5 KiB
TypeScript
|
|
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]);
|
||
|
|
}
|