import React, { Context, createContext, useCallback, useContext, useReducer } from 'react' import { OrderedMap, OrderedSet } from 'immutable' /** A transaction managed by the transaction manager. * * @typeParam M - the metadata type. */ export interface ManagedTransaction { /** The transaction hash. */ hash: string /** Metadata associated with the transaction. */ metadata?: M } /** Transaction manager state. */ export interface TransactionManagerState { /** The transactions being managed. */ transactions: ManagedTransaction[] /** Hashes of the transactions being managed. */ hashes: string[] /** Add a transaction to the managed transactions. */ addTransaction: ({ hash, metadata }: { hash: string; metadata?: M }) => void /** Remove a transaction from the managed transactions. */ removeTransaction: ({ hash }: { hash: string }) => void } /** Transaction manager context. */ export const TransactionManagerContext = createContext>({ transactions: [], hashes: [], addTransaction: ({ hash: _hash, metadata: _metadata }) => undefined, removeTransaction: ({ hash: _hash }) => undefined, }) /** * Hook to manage transaction across different components. * * @remarks * * This hook only manages the hashes of the transactions, use the * `useTransactions` hook to fetch the state of the transactions. * * @example * This example shows how to fetch the state of tracked transactions * and how to add new transactions to the manager. * * Notice that the transaction manager only accepts the same transaction once. * ```tsx * function Component() { * const { hashes, addTransaction } = useTransactionManager() * const transactions = useTransactions({ hashes }) * * return ( * <> * *
    * {transactions.map(({ data }, i) => ( *
  • {data && data.transaction_hash}
  • * ))} *
* * ) * } * ``` */ export function useTransactionManager(): TransactionManagerState { const context = useContext>( TransactionManagerContext as unknown as Context> ) if (!context) { throw new Error('useTransactionManager must be used inside TransactionManagerProvider') } return context } export interface TransactionManagerProviderProps { children: React.ReactNode } /** Context provider for `useTransactionManager`. */ export function TransactionManagerProvider({ children, }: TransactionManagerProviderProps) { const [state, dispatch] = useReducer(reducer, { hashes: OrderedSet(), transactions: OrderedMap>(), }) const addTransaction = useCallback( ({ hash, metadata }: { hash: string; metadata?: M }) => { dispatch({ type: 'add_transaction', hash, metadata }) }, [dispatch] ) const removeTransaction = useCallback( ({ hash }: { hash: string }) => { dispatch({ type: 'remove_transaction', hash }) }, [dispatch] ) const Provider = TransactionManagerContext.Provider as unknown as React.Provider< TransactionManagerState > const transactions = (state as unknown as InternalState).transactions return ( {children} ) } interface InternalState { hashes: OrderedSet transactions: OrderedMap> } interface AddTransaction { type: 'add_transaction' hash: string metadata?: M } interface RemoveTransaction { type: 'remove_transaction' hash: string } type InternalAction = AddTransaction | RemoveTransaction function reducer( state: InternalState, action: InternalAction ): InternalState { switch (action.type) { case 'add_transaction': { const hashes = state.hashes.add(action.hash) const transactions = state.transactions.set(action.hash, { hash: action.hash, metadata: action.metadata, }) return { ...state, hashes, transactions } } case 'remove_transaction': { const hashes = state.hashes.remove(action.hash) const transactions = state.transactions.delete(action.hash) return { ...state, hashes, transactions } } default: { return state } } }