1import create from 'zustand';
2import { PendingTransaction } from '@urbit/roller-api';
3import { toBN } from 'web3-utils';
6import { HOUR } from 'lib/utils/roller';
7import { Invite, Invites } from 'lib/types/Invite';
8import Point, { Points } from 'lib/types/Point';
9import { toL1Details } from 'lib/utils/point';
10import { PendingL1, PendingL1Txn } from 'lib/types/PendingL1Transaction';
12const getPointsAndList = (points: Points, point: Point) => ({
14 pointList: Object.values(points).sort((a, b) => a.value - b.value),
15 point: point.isDefault ? point : points[point.value],
18export const EMPTY_POINT = new Point({
20 details: toL1Details({}),
24export interface RollerStore {
26 nextBatchTime: number;
27 nextQuotaTime: number;
28 pendingTransactions: PendingTransaction[];
32 pendingL1ByPoint: PendingL1;
35 recentlyCompleted: number;
36 inviteGeneratingNum: number;
37 invitesLoading: boolean;
39 addInvite: (point: number, invite: Invite) => void;
40 setLoading: (loading: boolean) => void;
41 removeInvite: (point: number, planet: number) => void;
42 setInvites: (points: number, invites: Invite[]) => void;
43 setInviteGeneratingNum: (numGenerating: number) => void;
44 setInvitesLoading: (invitesLoading: boolean) => void;
45 setModalText: (modalText: string) => void;
46 setNextBatchTime: (nextBatchTime: number) => void;
47 setNextQuotaTime: (nextQuotaTime: number) => void;
48 setPendingTransactions: (pendingTransactions: PendingTransaction[]) => void;
49 setPoint: (point: number) => void;
50 setPoints: (points: Points) => void;
51 updateInvite: (point: number, invite: Invite) => void;
52 updatePoint: (point: Point) => void;
53 storePendingL1Txn: (txn: PendingL1Txn) => void;
54 deletePendingL1Txn: (txn: PendingL1Txn) => void;
55 setEthBalance: (ethBalance: BN) => void;
58export const useRollerStore = create<RollerStore>(set => ({
60 nextBatchTime: new Date().getTime() + HOUR,
61 nextQuotaTime: new Date().getTime() + 24 * HOUR,
62 pendingTransactions: [],
69 inviteGeneratingNum: 0,
70 invitesLoading: false,
72 setLoading: (loading: boolean) => set(() => ({ loading })),
73 setNextBatchTime: (nextBatchTime: number) => set(() => ({ nextBatchTime })),
74 setNextQuotaTime: (nextQuotaTime: number) => set(() => ({ nextQuotaTime })),
75 setPendingTransactions: (pendingTransactions: PendingTransaction[]) =>
77 const oldPending = state.pendingTransactions.length;
78 if (oldPending > 0 && pendingTransactions.length === 0) {
82 recentlyCompleted: oldPending,
85 return { ...state, pendingTransactions };
87 setPoint: (point: number) =>
88 set(state => ({ point: state.points[point] || EMPTY_POINT })),
89 setPoints: (points: Points) =>
90 set(({ point }) => getPointsAndList(points, point)),
91 setInvites: (point: number, invites: Invite[]) =>
93 const newInvites: Invites = {};
94 newInvites[point] = invites;
95 return { invites: Object.assign(state.invites, newInvites) };
97 setInviteGeneratingNum: (inviteGeneratingNum: number) =>
98 set(() => ({ inviteGeneratingNum })),
99 addInvite: (point: number, invite: Invite) => {
101 const newInvites: Invites = {};
102 const invites = state.invites[point];
104 if (!invites.find(inv => inv.planet === invite.planet)) {
105 newInvites[point] = [...invites, invite];
107 return { invites: Object.assign(state.invites, newInvites) };
110 updateInvite: (point: number, invite: Invite) =>
112 const newInvites: Invites = {};
113 newInvites[point] = state.invites[point]?.map(i =>
114 i.planet === invite.planet ? invite : i
116 return { invites: Object.assign(state.invites, newInvites) };
118 removeInvite: (point: number, planet: number) =>
120 const newInvites: Invites = {};
121 newInvites[point] = state.invites[point]?.filter(
122 i => i.planet !== planet
124 return { invites: Object.assign(state.invites, newInvites) };
126 setInvitesLoading: (invitesLoading: boolean) =>
127 set(() => ({ invitesLoading })),
128 updatePoint: (newPoint: Point) =>
129 set(({ point, points }) => {
130 const newPoints: Points = Object.assign({}, points);
131 newPoints[newPoint.value] = newPoint;
132 return getPointsAndList(newPoints, point);
134 setModalText: (modalText?: string) => set(() => ({ modalText })),
135 storePendingL1Txn: (txn: PendingL1Txn) =>
136 set(({ pendingL1ByPoint }) => {
137 const newPending = Object.assign({}, pendingL1ByPoint);
138 newPending[txn.point] = [...(pendingL1ByPoint[txn.point] || []), txn];
139 return { pendingL1ByPoint: newPending };
141 deletePendingL1Txn: (txn: PendingL1Txn) =>
142 set(({ pendingL1ByPoint }) => {
143 const newPending = Object.assign({}, pendingL1ByPoint);
144 newPending[txn.point] =
145 pendingL1ByPoint[txn.point]?.filter(({ id }) => id !== txn.id) || [];
146 return { pendingL1ByPoint: newPending };
148 setEthBalance: (ethBalance: BN) => set(() => ({ ethBalance })),
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list