Merge pull request #4746 from urbit/lf/notifications-load

notifications: prevent race condition causing empty notifications
This commit is contained in:
matildepark 2021-04-12 11:32:12 -04:00 committed by GitHub
commit 9bb3cfb90f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 2 deletions

View File

@ -11,6 +11,7 @@ export function distanceToBottom(el: HTMLElement) {
export function useLazyScroll(
ref: RefObject<HTMLElement>,
ready: boolean,
margin: number,
count: number,
loadMore: () => Promise<boolean>
@ -41,7 +42,7 @@ export function useLazyScroll(
}, [count]);
useEffect(() => {
if (!ref.current || isDone) {
if (!ref.current || isDone || !ready) {
return;
}
const scroll = ref.current;
@ -57,7 +58,7 @@ export function useLazyScroll(
return () => {
ref.current?.removeEventListener('scroll', onScroll);
};
}, [ref?.current, count]);
}, [ref?.current, count, ready]);
return { isDone, isLoading };
}

View File

@ -25,6 +25,7 @@ import { Invites } from './invites';
import { useLazyScroll } from '~/logic/lib/useLazyScroll';
import useHarkState from '~/logic/state/hark';
import useInviteState from '~/logic/state/invite';
import useMetadataState from '~/logic/state/metadata';
type DatedTimebox = [BigInteger, Timebox];
@ -64,6 +65,10 @@ export default function Inbox(props: {
};
}, []);
const ready = useHarkState(
s => Object.keys(s.unreads.graph).length > 0
);
const notificationState = useHarkState(state => state.notifications);
const archivedNotifications = useHarkState(state => state.archivedNotifications);
@ -109,6 +114,7 @@ export default function Inbox(props: {
const { isDone, isLoading } = useLazyScroll(
scrollRef,
ready,
0.2,
_.flatten(notifications).length,
loadMore