Skip to content

Commit

Permalink
Some fixes for addresses on Context
Browse files Browse the repository at this point in the history
  • Loading branch information
KaffinPX committed Sep 26, 2024
1 parent 0282d37 commit a45c1bd
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions src/contexts/Kaspa.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,18 @@ export const KaspaContext = createContext<{
request: <M extends keyof RequestMappings>(method: M, params: RequestMappings[M]) => Promise<ResponseMappings[M]>
} | undefined>(undefined)

type Action<K extends keyof IKaspa> = { type: K, payload: IKaspa[K] }
type Action<K extends keyof IKaspa> = {
type: K;
payload: IKaspa[K] | ((oldState: IKaspa) => IKaspa[K])
}

function kaspaReducer (state: IKaspa, action: Action<keyof IKaspa>): IKaspa {
const { type, payload } = action

function kaspaReducer(state: IKaspa, action: Action<keyof IKaspa>): IKaspa {
return {
...state,
[action.type]: action.payload
if (typeof payload === 'function') {
return { ...state, [ type ]: payload(state) }
} else {
return { ...state, [ type ]: payload }
}
}

Expand Down Expand Up @@ -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':
Expand Down

0 comments on commit a45c1bd

Please sign in to comment.