notifications: prevent race condition causing empty notifications

Fixes urbit/landscape#741
This commit is contained in:
Liam Fitzgerald 2021-04-09 11:42:09 +10:00
parent 7340f3f7e5
commit 027fd225a9
No known key found for this signature in database
GPG Key ID: D390E12C61D1CFFB
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