-
Notifications
You must be signed in to change notification settings - Fork 580
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: init zustand & new pending tx state (#5383)
* add zustand * init zustand * core pendingTx state * lint * bump prettier * Revert "bump prettier" This reverts commit 1effe54. * remove prettierignore file and merge develop * fix format --------- Co-authored-by: Matthew Wall <matthew.wallt@gmail.com>
- Loading branch information
1 parent
6d580bb
commit 3a84d51
Showing
6 changed files
with
117 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { PersistOptions, persist } from 'zustand/middleware'; | ||
import create, { Mutate, StoreApi } from 'zustand/vanilla'; | ||
|
||
import { persistStorage } from './persistStorage'; | ||
|
||
type Initializer<TState> = Parameters<typeof persist<TState>>[0]; | ||
export type StoreWithPersist<TState> = Mutate<StoreApi<TState>, [['zustand/persist', unknown]]> & { | ||
initializer: Initializer<TState>; | ||
}; | ||
|
||
export function createStore<TState>( | ||
initializer: Initializer<TState>, | ||
{ persist: persistOptions }: { persist?: PersistOptions<TState> } = {} | ||
) { | ||
const name = `rainbow.zustand.${persistOptions?.name}`; | ||
return Object.assign( | ||
create( | ||
persist(initializer, { | ||
...persistOptions, | ||
name, | ||
getStorage: () => persistStorage, | ||
}) | ||
), | ||
{ initializer } | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { zustandStorage } from '@/storage/legacy'; | ||
import { StateStorage } from 'zustand/middleware'; | ||
|
||
export const persistStorage: StateStorage = { | ||
getItem: async (key: string): Promise<string | null> => { | ||
return zustandStorage.get([key]); | ||
}, | ||
setItem: async (key: string, value: string): Promise<void> => { | ||
await zustandStorage.set([key], value); | ||
}, | ||
removeItem: async (key: string): Promise<void> => { | ||
await zustandStorage.remove([key]); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { RainbowTransaction } from '@/entities/transactions'; | ||
import { createStore } from './internal/createStore'; | ||
import create from 'zustand'; | ||
|
||
export interface PendingTransactionsState { | ||
pendingTransactions: Record<string, RainbowTransaction[]>; | ||
addPendingTransaction: ({ address, pendingTransaction }: { address: string; pendingTransaction: RainbowTransaction }) => void; | ||
updatePendingTransaction: ({ address, pendingTransaction }: { address: string; pendingTransaction: RainbowTransaction }) => void; | ||
setPendingTransactions: ({ address, pendingTransactions }: { address: string; pendingTransactions: RainbowTransaction[] }) => void; | ||
clearPendingTransactions: () => void; | ||
} | ||
|
||
export const pendingTransactionsStore = createStore<PendingTransactionsState>( | ||
(set, get) => ({ | ||
pendingTransactions: {}, | ||
addPendingTransaction: ({ address, pendingTransaction }) => { | ||
const { pendingTransactions: currentPendingTransactions } = get(); | ||
const addressPendingTransactions = currentPendingTransactions[address] || []; | ||
set({ | ||
pendingTransactions: { | ||
...currentPendingTransactions, | ||
[address]: [...addressPendingTransactions, pendingTransaction], | ||
}, | ||
}); | ||
}, | ||
updatePendingTransaction: ({ address, pendingTransaction }) => { | ||
const { pendingTransactions: currentPendingTransactions } = get(); | ||
const addressPendingTransactions = currentPendingTransactions[address] || []; | ||
|
||
set({ | ||
pendingTransactions: { | ||
...currentPendingTransactions, | ||
[address]: [ | ||
...addressPendingTransactions.filter(tx => { | ||
if (tx.network === pendingTransaction.network) { | ||
return tx.nonce !== pendingTransaction.nonce; | ||
} | ||
return true; | ||
}), | ||
pendingTransaction, | ||
], | ||
}, | ||
}); | ||
}, | ||
setPendingTransactions: ({ address, pendingTransactions }) => { | ||
const { pendingTransactions: currentPendingTransactions } = get(); | ||
set({ | ||
pendingTransactions: { | ||
...currentPendingTransactions, | ||
[address]: [...pendingTransactions], | ||
}, | ||
}); | ||
}, | ||
clearPendingTransactions: () => { | ||
set({ pendingTransactions: {} }); | ||
}, | ||
}), | ||
{ | ||
persist: { | ||
name: 'pendingTransactions', | ||
version: 1, | ||
}, | ||
} | ||
); | ||
|
||
export const usePendingTransactionsStore = create(pendingTransactionsStore); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters