on ca31bdf help sign in
bridge/src/store/roller.ts
62 lines 2.2 kB history raw
1import create from 'zustand';
2import { L2Point, PendingTransaction } from '@urbit/roller-api';
4import { HOUR, isL2, isL2Spawn } from 'lib/utils/roller';
5import { Invite } from 'lib/types/Invite';
7export interface RollerStore {
8 nextBatchTime: number;
9 nextRoll: string;
10 pendingTransactions: PendingTransaction[];
11 currentPoint: L2Point | null;
12 currentL2: boolean;
13 currentL2Spawn: boolean;
14 invitePoints: number[];
15 invites: Invite[];
16 recentlyCompleted: number;
17 inviteGeneratingNum: number;
18 setNextBatchTime: (nextBatchTime: number) => void;
19 setNextRoll: (nextRoll: string) => void;
20 setPendingTransactions: (pendingTransactions: PendingTransaction[]) => void;
21 setCurrentPoint: (currentPoint: L2Point) => void;
22 setInvitePoints: (points: number[]) => void;
23 setInvites: (invites: Invite[]) => void;
24 setInviteGeneratingNum: (numGenerating: number) => void;
27export const useRollerStore = create<RollerStore>(set => ({
28 nextBatchTime: new Date().getTime() + HOUR,
29 nextRoll: '0h 00m 00s',
30 pendingTransactions: [],
31 currentPoint: null,
32 currentL2: false,
33 currentL2Spawn: false,
34 invitePoints: [],
35 invites: [],
36 recentlyCompleted: 0,
37 inviteGeneratingNum: 0,
38 setNextBatchTime: (nextBatchTime: number) => set(() => ({ nextBatchTime })),
39 setNextRoll: (nextRoll: string) => set(() => ({ nextRoll })),
40 setPendingTransactions: (pendingTransactions: PendingTransaction[]) =>
41 set(state => {
42 const oldPending = state.pendingTransactions.length;
43 if (oldPending > 0 && pendingTransactions.length === 0) {
44 return {
45 ...state,
46 pendingTransactions,
47 recentlyCompleted: oldPending,
48 };
49 }
50 return { ...state, pendingTransactions };
51 }),
52 setCurrentPoint: (currentPoint: L2Point) =>
53 set(() => ({
54 currentPoint,
55 currentL2: isL2(currentPoint?.dominion),
56 currentL2Spawn: isL2Spawn(currentPoint?.dominion),
57 })),
58 setInvitePoints: (invitePoints: number[]) => set(() => ({ invitePoints })),
59 setInvites: (invites: Invite[]) => set(() => ({ invites })),
60 setInviteGeneratingNum: (inviteGeneratingNum: number) =>
61 set(() => ({ inviteGeneratingNum })),
62}));