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