From a45c1bd2984147777d1005d003b02c398b3ce223 Mon Sep 17 00:00:00 2001 From: KaffinPX Date: Fri, 27 Sep 2024 01:47:52 +0300 Subject: [PATCH] Some fixes for addresses on Context --- src/contexts/Kaspa.tsx | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/src/contexts/Kaspa.tsx b/src/contexts/Kaspa.tsx index 8dfa246..34203d0 100644 --- a/src/contexts/Kaspa.tsx +++ b/src/contexts/Kaspa.tsx @@ -34,12 +34,18 @@ export const KaspaContext = createContext<{ request: (method: M, params: RequestMappings[M]) => Promise } | undefined>(undefined) -type Action = { type: K, payload: IKaspa[K] } +type Action = { + type: K; + payload: IKaspa[K] | ((oldState: IKaspa) => IKaspa[K]) +} + +function kaspaReducer (state: IKaspa, action: Action): IKaspa { + const { type, payload } = action -function kaspaReducer(state: IKaspa, action: Action): IKaspa { - return { - ...state, - [action.type]: action.payload + if (typeof payload === 'function') { + return { ...state, [ type ]: payload(state) } + } else { + return { ...state, [ type ]: payload } } } @@ -99,10 +105,10 @@ export function KaspaProvider ({ children }: { children: ReactNode }) { case 'account:addresses': dispatch({ type: 'addresses', - payload: [ - kaspa.addresses[0].concat(message.data[0]), - kaspa.addresses[1].concat(message.data[1]) - ] + payload: ({ addresses }) => [ + addresses[0].concat(message.data[0]), + addresses[1].concat(message.data[1]), + ], }) break case 'provider:connection':