--- /dev/null
+import { useEffect } from 'react';
+
+export const useDocumentFocusChange = (onChange: (focus: boolean) => void) => {
+ useEffect(() => {
+ let localFocus = document.hasFocus();
+
+ const handleFocus = () => {
+ if (document.hasFocus()) {
+ if (localFocus) return;
+ localFocus = true;
+ onChange(localFocus);
+ } else if (localFocus) {
+ localFocus = false;
+ onChange(localFocus);
+ }
+ };
+
+ document.addEventListener('focusin', handleFocus);
+ document.addEventListener('focusout', handleFocus);
+ return () => {
+ document.removeEventListener('focusin', handleFocus);
+ document.removeEventListener('focusout', handleFocus);
+ };
+ }, [onChange]);
+};
import initMatrix from '../../../client/initMatrix';
import { useKeyDown } from '../../hooks/useKeyDown';
import cons from '../../../client/state/cons';
+import { useDocumentFocusChange } from '../../hooks/useDocumentFocusChange';
const TimelineFloat = as<'div', css.TimelineFloatVariants>(
({ position, className, ...props }, ref) => (
// keep paginating timeline and conditionally mark as read
// otherwise we update timeline without paginating
// so timeline can be updated with evt like: edits, reactions etc
- if (atBottomRef.current && document.hasFocus()) {
- if (!unreadInfo) {
- markAsRead(mEvt.getRoomId());
+ if (atBottomRef.current) {
+ if (document.hasFocus() && (!unreadInfo || mEvt.getSender() === mx.getUserId())) {
+ requestAnimationFrame(() => markAsRead(mEvt.getRoomId()));
}
- scrollToBottomRef.current.count += 1;
- scrollToBottomRef.current.smooth = true;
+ if (document.hasFocus()) {
+ scrollToBottomRef.current.count += 1;
+ scrollToBottomRef.current.smooth = true;
+ }
setTimeline((ct) => ({
...ct,
range: {
setUnreadInfo(getRoomUnreadInfo(room));
}
},
- [room, unreadInfo]
+ [mx, room, unreadInfo]
)
);
const tryAutoMarkAsRead = useCallback(() => {
if (!unreadInfo) {
- markAsRead(room.roomId);
+ requestAnimationFrame(() => markAsRead(room.roomId));
return;
}
const evtTimeline = getEventTimeline(room, unreadInfo.readUptoEventId);
const latestTimeline = evtTimeline && getFirstLinkedTimeline(evtTimeline, Direction.Forward);
if (latestTimeline === room.getLiveTimeline()) {
- markAsRead(room.roomId);
+ requestAnimationFrame(() => markAsRead(room.roomId));
}
}, [room, unreadInfo]);
useCallback(() => atBottomAnchorRef.current, [])
);
+ useDocumentFocusChange(
+ useCallback(
+ (inFocus) => {
+ if (inFocus && atBottomRef.current) {
+ tryAutoMarkAsRead();
+ }
+ },
+ [tryAutoMarkAsRead]
+ )
+ );
+
// Handle up arrow edit
useKeyDown(
window,