1import create from 'zustand';
2import { PendingTransaction } from '@urbit/roller-api';
4import { HOUR } from 'lib/utils/roller';
5import { Invite, Invites } from 'lib/types/Invite';
6import Point, { Points } from 'lib/types/Point';
7import { toL1Details } from 'lib/utils/point';
8import { PendingL1, PendingL1Txn } from 'lib/types/PendingL1Transaction';
10const getPointsAndList = (points: Points, point: Point) => ({
12 pointList: Object.values(points).sort((a, b) => a.value - b.value),
13 point: point.isDefault ? point : points[point.value],
16export const EMPTY_POINT = new Point({
18 details: toL1Details({}),
22export interface RollerStore {
24 nextBatchTime: number;
25 nextQuotaTime: number;
26 pendingTransactions: PendingTransaction[];
30 pendingL1ByPoint: PendingL1;
33 recentlyCompleted: number;
34 inviteGeneratingNum: number;
35 invitesLoading: boolean;
36 setLoading: (loading: boolean) => void;
37 removeInvite: (point: number, planet: number) => void;
38 setInvites: (points: number, invites: Invite[]) => void;
39 setInviteGeneratingNum: (numGenerating: number) => void;
40 setInvitesLoading: (invitesLoading: boolean) => void;
41 setModalText: (modalText: string) => void;
42 setNextBatchTime: (nextBatchTime: number) => void;
43 setNextQuotaTime: (nextQuotaTime: number) => void;
44 setPendingTransactions: (pendingTransactions: PendingTransaction[]) => void;
45 setPoint: (point: number) => void;
46 setPoints: (points: Points) => void;
47 updateInvite: (point: number, invite: Invite) => void;
48 updatePoint: (point: Point) => void;
49 storePendingL1Txn: (txn: PendingL1Txn) => void;
50 deletePendingL1Txn: (txn: PendingL1Txn) => void;
53export const useRollerStore = create<RollerStore>(set => ({
55 nextBatchTime: new Date().getTime() + HOUR,
56 nextQuotaTime: new Date().getTime() + 24 * HOUR,
57 pendingTransactions: [],
64 inviteGeneratingNum: 0,
65 invitesLoading: false,
66 setLoading: (loading: boolean) => set(() => ({ loading })),
67 setNextBatchTime: (nextBatchTime: number) => set(() => ({ nextBatchTime })),
68 setNextQuotaTime: (nextQuotaTime: number) => set(() => ({ nextQuotaTime })),
69 setPendingTransactions: (pendingTransactions: PendingTransaction[]) =>
71 const oldPending = state.pendingTransactions.length;
72 if (oldPending > 0 && pendingTransactions.length === 0) {
76 recentlyCompleted: oldPending,
79 return { ...state, pendingTransactions };
81 setPoint: (point: number) =>
82 set(state => ({ point: state.points[point] || EMPTY_POINT })),
83 setPoints: (points: Points) =>
84 set(({ point }) => getPointsAndList(points, point)),
85 setInvites: (point: number, invites: Invite[]) =>
87 const newInvites: Invites = {};
88 newInvites[point] = invites;
89 return { invites: Object.assign(state.invites, newInvites) };
91 setInviteGeneratingNum: (inviteGeneratingNum: number) =>
92 set(() => ({ inviteGeneratingNum })),
93 updateInvite: (point: number, invite: Invite) =>
95 const newInvites: Invites = {};
96 newInvites[point] = state.invites[point]?.map(i =>
97 i.planet === invite.planet ? invite : i
99 return { invites: Object.assign(state.invites, newInvites) };
101 removeInvite: (point: number, planet: number) =>
103 const newInvites: Invites = {};
104 newInvites[point] = state.invites[point]?.filter(
105 i => i.planet !== planet
107 return { invites: Object.assign(state.invites, newInvites) };
109 setInvitesLoading: (invitesLoading: boolean) =>
110 set(() => ({ invitesLoading })),
111 updatePoint: (newPoint: Point) =>
112 set(({ point, points }) => {
113 const newPoints: Points = Object.assign({}, points);
114 newPoints[newPoint.value] = newPoint;
115 return getPointsAndList(newPoints, point);
117 setModalText: (modalText?: string) => set(() => ({ modalText })),
118 storePendingL1Txn: (txn: PendingL1Txn) =>
119 set(({ pendingL1ByPoint }) => {
120 const newPending = Object.assign({}, pendingL1ByPoint);
121 newPending[txn.point] = [...(pendingL1ByPoint[txn.point] || []), txn];
122 return { pendingL1ByPoint: newPending };
124 deletePendingL1Txn: (txn: PendingL1Txn) =>
125 set(({ pendingL1ByPoint }) => {
126 const newPending = Object.assign({}, pendingL1ByPoint);
127 newPending[txn.point] =
128 pendingL1ByPoint[txn.point]?.filter(({ id }) => id !== txn.id) || [];
129 return { pendingL1ByPoint: newPending };
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