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