on master help sign in
invites: only add after we verify ownership so count is more stable
807883bHunter Miller
src/lib/useRoller.ts+13−15
@@ -1,4 +1,4 @@
1−import { useCallback, useEffect, useMemo, useState } from 'react';
1+import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
2import * as azimuth from 'azimuth-js';
2import * as azimuth from 'azimuth-js';
3import * as need from 'lib/need';
3import * as need from 'lib/need';
4import SecureLS from 'secure-ls';
4import SecureLS from 'secure-ls';
@@ -92,13 +92,12 @@ export default function useRoller() {
92 const { setNextRoll } = useTimerStore();
92 const { setNextRoll } = useTimerStore();
93
93
94 const {
94 const {
95− invitesLoading,
96 nextBatchTime,
95 nextBatchTime,
97 point,
96 point,
98 points,
97 points,
99 pointList,
98 pointList,
100 nextQuotaTime,
99 nextQuotaTime,
101− removeInvite,
100+ addInvite,
102 setNextBatchTime,
101 setNextBatchTime,
103 setNextQuotaTime,
102 setNextQuotaTime,
104 setPendingTransactions,
103 setPendingTransactions,
@@ -107,11 +106,11 @@ export default function useRoller() {
107 setInvitesLoading,
106 setInvitesLoading,
108 setModalText,
107 setModalText,
109 setPoints,
108 setPoints,
110− updateInvite,
111 updatePoint,
109 updatePoint,
112 storePendingL1Txn,
110 storePendingL1Txn,
113 deletePendingL1Txn,
111 deletePendingL1Txn,
114 } = useRollerStore();
112 } = useRollerStore();
113+ const generatingInvites = useRef(false);
115 const [config, setConfig] = useState<Config | null>(null);
114 const [config, setConfig] = useState<Config | null>(null);
116
115
117 const options: Options = useMemo(() => {
116 const options: Options = useMemo(() => {
@@ -360,10 +359,11 @@ export default function useRoller() {
360 };
359 };
361
360
362 const getInvites = useCallback(async () => {
361 const getInvites = useCallback(async () => {
363− if (invitesLoading) {
362+ if (generatingInvites.current) {
364 return;
363 return;
365 }
364 }
366
365
366+ generatingInvites.current = true;
367 try {
367 try {
368 setInvitesLoading(true);
368 setInvitesLoading(true);
369 const curPoint: number = Number(need.point(pointCursor));
369 const curPoint: number = Number(need.point(pointCursor));
@@ -397,10 +397,7 @@ export default function useRoller() {
397 .filter((p: number) => !pendingSpawns.has(p))
397 .filter((p: number) => !pendingSpawns.has(p))
398 .sort();
398 .sort();
399
399
400− setInvites(
400+ setInvites(curPoint, []);
401− curPoint,
402− invitePlanets.map(p => inviteTemplate(p))
403− );
404
401
405 const newInvites: Invite[] = [];
402 const newInvites: Invite[] = [];
406 // Iterate over all of the stored invites, generating wallet info as necessary
403 // Iterate over all of the stored invites, generating wallet info as necessary
@@ -422,23 +419,23 @@ export default function useRoller() {
422 planetInfo.ownership?.owner?.address &&
419 planetInfo.ownership?.owner?.address &&
423 planetInfo.dominion === POINT_DOMINIONS.L2
420 planetInfo.dominion === POINT_DOMINIONS.L2
424 ) {
421 ) {
422+ addInvite(curPoint, invite);
425 newInvites.push(invite);
423 newInvites.push(invite);
426− updateInvite(curPoint, invite);
427− } else {
428− removeInvite(curPoint, invite.planet);
429 }
424 }
430
425
431 if (storedInvite) {
426 if (storedInvite) {
432 setStoredInvite(ls, invite);
427 setStoredInvite(ls, invite);
433 }
428 }
434 }
429 }
435− setInvites(curPoint, newInvites);
430+
436 setInvitesLoading(false);
431 setInvitesLoading(false);
437 return newInvites;
432 return newInvites;
438 }
433 }
439 } catch (error) {
434 } catch (error) {
440 console.warn('ERROR GETTING INVITES', error);
435 console.warn('ERROR GETTING INVITES', error);
441 }
436 }
437+
438+ generatingInvites.current = false;
442 }, [
439 }, [
443 api,
440 api,
444 authToken,
441 authToken,
@@ -447,12 +444,11 @@ export default function useRoller() {
447 ls,
444 ls,
448 point,
445 point,
449 pointList,
446 pointList,
450− removeInvite,
447+ addInvite,
451 setInvites,
448 setInvites,
452 setInvitesLoading,
449 setInvitesLoading,
453 setInviteGeneratingNum,
450 setInviteGeneratingNum,
454 setPendingTransactions,
451 setPendingTransactions,
455− updateInvite,
456 ]);
452 ]);
457
453
458 const generateInviteCodes = useCallback(
454 const generateInviteCodes = useCallback(
@@ -571,6 +567,7 @@ export default function useRoller() {
571 }
567 }
572
568
573 setStoredInvites(ls, invites);
569 setStoredInvites(ls, invites);
570+ console.log('getting invites from code generation');
574 getInvites();
571 getInvites();
575 showNotification(
572 showNotification(
576 `Your invite${invites.length > 1 ? 's have' : ' has'} been generated!`
573 `Your invite${invites.length > 1 ? 's have' : ' has'} been generated!`
@@ -1053,6 +1050,7 @@ export default function useRoller() {
1053
1050
1054 setTimeout(() => {
1051 setTimeout(() => {
1055 if (!point.isDefault) {
1052 if (!point.isDefault) {
1053+ console.log('getting invites from batch time');
1056 getInvites(); // This will also get pending txns
1054 getInvites(); // This will also get pending txns
1057 }
1055 }
1058 }, TEN_SECONDS); // Should this be more like a minute?
1056 }, TEN_SECONDS); // Should this be more like a minute?
src/store/rollerStore.ts+12
@@ -36,6 +36,7 @@ export interface RollerStore {
36 inviteGeneratingNum: number;
36 inviteGeneratingNum: number;
37 invitesLoading: boolean;
37 invitesLoading: boolean;
38 ethBalance: BN;
38 ethBalance: BN;
39+ addInvite: (point: number, invite: Invite) => void;
39 setLoading: (loading: boolean) => void;
40 setLoading: (loading: boolean) => void;
40 removeInvite: (point: number, planet: number) => void;
41 removeInvite: (point: number, planet: number) => void;
41 setInvites: (points: number, invites: Invite[]) => void;
42 setInvites: (points: number, invites: Invite[]) => void;
@@ -95,6 +96,17 @@ export const useRollerStore = create<RollerStore>(set => ({
95 }),
96 }),
96 setInviteGeneratingNum: (inviteGeneratingNum: number) =>
97 setInviteGeneratingNum: (inviteGeneratingNum: number) =>
97 set(() => ({ inviteGeneratingNum })),
98 set(() => ({ inviteGeneratingNum })),
99+ addInvite: (point: number, invite: Invite) => {
100+ set(state => {
101+ const newInvites: Invites = {};
102+ const invites = state.invites[point];
103+
104+ if (!invites.find(inv => inv.planet === invite.planet)) {
105+ newInvites[point] = [...invites, invite];
106+ }
107+ return { invites: Object.assign(state.invites, newInvites) };
108+ });
109+ },
98 updateInvite: (point: number, invite: Invite) =>
110 updateInvite: (point: number, invite: Invite) =>
99 set(state => {
111 set(state => {
100 const newInvites: Invites = {};
112 const newInvites: Invites = {};
src/views/Invite/Cohort.tsx+1−4
@@ -98,10 +98,7 @@ export default function InviteCohort() {
98 const [page, setPage] = useState(0);
98 const [page, setPage] = useState(0);
99
99
100 const { construct, unconstruct, completed, bind } = useIssueChild();
100 const { construct, unconstruct, completed, bind } = useIssueChild();
101− const invitePoints: Invite[] = useMemo(() => invites[point.value] || [], [
101+ const invitePoints = invites[point.value];
102− invites,
103− point,
104− ]);
105
102
106 // Set up the invite spawn if on L1
103 // Set up the invite spawn if on L1
107 useEffect(() => {
104 useEffect(() => {