Files
cinny/src/app/hooks/usePresenceUpdater.ts
T
jared ca09e8e6ca
CI / Build & Quality Checks (push) Successful in 10m22s
Trigger Desktop Build / trigger (push) Successful in 5s
feat: presence fix, voice ringing fix, user private notes + doc updates
- usePresenceUpdater: replace stale closure with readStatus() called at
  invocation time so changing custom status in Profile Settings is never
  silently overwritten by subsequent activity events
- CallEmbedProvider: fix m.space.parent state-key lookup by switching
  getStateEvent → getStateEvents (plural); space channel voice rooms no
  longer trigger the incoming-call ring/animation
- Add useUserNotes hook (io.lotus.user_notes account data, reactive via
  useAccountDataCallback, 500-char limit, cross-device sync)
- UserRoomProfile: add UserPrivateNotes textarea with 800ms debounced
  auto-save, saving indicator, char counter when <100 chars remain;
  shown only when viewing another user's profile
- LOTUS_FEATURES.md: add Private Notes section, Status Revert fix note,
  animation improvements subsection, Seasonal Themes section
- LOTUS_BUGS.md: mark presence revert + voice ringing bugs as resolved
- README.md + landing/index.html: document all new June 2026 features

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-14 00:47:14 -04:00

134 lines
4.2 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;
const ACTIVITY_THROTTLE_MS = 1000;
export function usePresenceUpdater() {
const mx = useMatrixClient();
const [hidePresence] = useSetting(settingsAtom, 'hidePresence');
const [presenceStatus] = useSetting(settingsAtom, 'presenceStatus');
const idleTimerRef = useRef<ReturnType<typeof setTimeout> | undefined>(undefined);
const isIdleRef = useRef(false);
const lastActivityRef = useRef(0);
useEffect(() => {
const userId = mx.getUserId();
// Read status from localStorage at call time so manual updates from the
// Profile settings are never overwritten by a stale closure value.
const readStatus = () =>
userId ? (localStorage.getItem(`lotus-status-msg-${userId}`) ?? '') : '';
const setOnline = () => {
const status = readStatus();
return mx
.setPresence({
presence: 'online',
...(status ? { status_msg: status } : {}),
})
.catch(() => undefined);
};
const setUnavailable = (statusMsg?: string) => {
const status = readStatus();
return mx
.setPresence({
presence: 'unavailable',
...(statusMsg
? { status_msg: statusMsg }
: status
? { status_msg: status }
: {}),
})
.catch(() => undefined);
};
const setOffline = () =>
mx.setPresence({ presence: 'offline', status_msg: '' }).catch(() => undefined);
// Manual presence overrides — no activity tracking needed.
if (hidePresence || presenceStatus === 'invisible') {
setOffline();
return undefined;
}
if (presenceStatus === 'online') {
setOnline();
return undefined;
}
if (presenceStatus === 'idle') {
setUnavailable();
return undefined;
}
if (presenceStatus === 'dnd') {
setUnavailable('dnd');
return undefined;
}
// presenceStatus === 'auto' — original activity-tracking behavior.
const startIdleTimer = () => {
clearTimeout(idleTimerRef.current);
idleTimerRef.current = 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;
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();
}
};
const handlePageHide = () => {
const token = mx.getAccessToken();
const baseUrl = mx.getHomeserverUrl();
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);
};
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, presenceStatus]);
}