Refactor status transitions in walletsReducer (#2282)

This commit is contained in:
ekzyis 2025-07-16 16:59:14 +02:00 committed by GitHub
parent 980f6da613
commit 0e71a85cd6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -27,9 +27,7 @@ export default function reducer (state, action) {
.sort((a, b) => a.name.localeCompare(b.name))
return {
...state,
status: statusLocked(state.status)
? state.status
: walletStatus(wallets),
status: transitionStatus(action, state, wallets.length > 0 ? Status.HAS_WALLETS : Status.NO_WALLETS),
wallets,
templates
}
@ -43,31 +41,32 @@ export default function reducer (state, action) {
case WRONG_KEY:
return {
...state,
status: Status.PASSPHRASE_REQUIRED
status: transitionStatus(action, state, Status.PASSPHRASE_REQUIRED)
}
case KEY_MATCH:
return {
...state,
status: state.status === Status.LOADING_WALLETS
? state.status
: walletStatus(state.wallets)
status: transitionStatus(action, state, state.wallets.length > 0 ? Status.HAS_WALLETS : Status.NO_WALLETS)
}
case NO_KEY:
return {
...state,
status: Status.WALLETS_UNAVAILABLE
status: transitionStatus(action, state, Status.WALLETS_UNAVAILABLE)
}
default:
return state
}
}
function statusLocked (status) {
return [Status.PASSPHRASE_REQUIRED, Status.WALLETS_UNAVAILABLE].includes(status)
}
function walletStatus (wallets) {
return wallets.length > 0
? Status.HAS_WALLETS
: Status.NO_WALLETS
function transitionStatus ({ type }, { status: from }, to) {
switch (type) {
case SET_WALLETS: {
return [Status.PASSPHRASE_REQUIRED, Status.WALLETS_UNAVAILABLE].includes(from) ? from : to
}
case KEY_MATCH: {
return from === Status.LOADING_WALLETS ? from : to
}
default:
return to
}
}