mirror of
https://github.com/arkorty/B.Tech-Project-III.git
synced 2026-04-19 12:41:48 +00:00
init
This commit is contained in:
52
dmtp/client/hooks/useTransactions.ts
Normal file
52
dmtp/client/hooks/useTransactions.ts
Normal file
@@ -0,0 +1,52 @@
|
||||
'use client';
|
||||
|
||||
import { create } from 'zustand';
|
||||
|
||||
export interface Transaction {
|
||||
hash: string;
|
||||
status: 'pending' | 'success' | 'failed';
|
||||
description: string;
|
||||
timestamp: number;
|
||||
}
|
||||
|
||||
interface TransactionState {
|
||||
transactions: Transaction[];
|
||||
currentTx: Transaction | null;
|
||||
|
||||
addTransaction: (tx: Omit<Transaction, 'timestamp'>) => void;
|
||||
updateTransaction: (hash: string, updates: Partial<Transaction>) => void;
|
||||
clearTransactions: () => void;
|
||||
}
|
||||
|
||||
export const useTransactions = create<TransactionState>((set) => ({
|
||||
transactions: [],
|
||||
currentTx: null,
|
||||
|
||||
addTransaction: (tx) => {
|
||||
const transaction: Transaction = {
|
||||
...tx,
|
||||
timestamp: Date.now(),
|
||||
};
|
||||
|
||||
set((state) => ({
|
||||
transactions: [transaction, ...state.transactions],
|
||||
currentTx: transaction,
|
||||
}));
|
||||
},
|
||||
|
||||
updateTransaction: (hash, updates) => {
|
||||
set((state) => ({
|
||||
transactions: state.transactions.map((tx) =>
|
||||
tx.hash === hash ? { ...tx, ...updates } : tx
|
||||
),
|
||||
currentTx:
|
||||
state.currentTx?.hash === hash
|
||||
? { ...state.currentTx, ...updates }
|
||||
: state.currentTx,
|
||||
}));
|
||||
},
|
||||
|
||||
clearTransactions: () => {
|
||||
set({ transactions: [], currentTx: null });
|
||||
},
|
||||
}));
|
||||
Reference in New Issue
Block a user