|
| 1 | +import {castDraft, produce} from 'immer' |
| 2 | +import _ from 'lodash' |
| 3 | +import React from 'react' |
| 4 | + |
| 5 | +import {YoroiSignedTx, YoroiUnsignedTx} from '../../../yoroi-wallets/types/yoroi' |
| 6 | + |
| 7 | +export const useReviewTx = () => React.useContext(ReviewTxContext) |
| 8 | + |
| 9 | +export const ReviewTxProvider = ({ |
| 10 | + children, |
| 11 | + initialState, |
| 12 | +}: { |
| 13 | + children: React.ReactNode |
| 14 | + initialState?: Partial<ReviewTxState> |
| 15 | +}) => { |
| 16 | + const [state, dispatch] = React.useReducer(reviewTxReducer, { |
| 17 | + ...defaultState, |
| 18 | + ...initialState, |
| 19 | + }) |
| 20 | + |
| 21 | + const actions = React.useRef<ReviewTxActions>({ |
| 22 | + unsignedTxChanged: (unsignedTx: ReviewTxState['unsignedTx']) => |
| 23 | + dispatch({type: ReviewTxActionType.UnsignedTxChanged, unsignedTx}), |
| 24 | + cborChanged: (cbor: ReviewTxState['cbor']) => dispatch({type: ReviewTxActionType.CborChanged, cbor}), |
| 25 | + operationsChanged: (operations: ReviewTxState['operations']) => |
| 26 | + dispatch({type: ReviewTxActionType.OperationsChanged, operations}), |
| 27 | + onSuccessChanged: (onSuccess: ReviewTxState['onSuccess']) => |
| 28 | + dispatch({type: ReviewTxActionType.OnSuccessChanged, onSuccess}), |
| 29 | + onErrorChanged: (onError: ReviewTxState['onError']) => dispatch({type: ReviewTxActionType.OnErrorChanged, onError}), |
| 30 | + }).current |
| 31 | + |
| 32 | + const context = React.useMemo( |
| 33 | + () => ({ |
| 34 | + ...state, |
| 35 | + ...actions, |
| 36 | + }), |
| 37 | + [state, actions], |
| 38 | + ) |
| 39 | + |
| 40 | + return <ReviewTxContext.Provider value={context}>{children}</ReviewTxContext.Provider> |
| 41 | +} |
| 42 | + |
| 43 | +const reviewTxReducer = (state: ReviewTxState, action: ReviewTxAction) => { |
| 44 | + return produce(state, (draft) => { |
| 45 | + switch (action.type) { |
| 46 | + case ReviewTxActionType.UnsignedTxChanged: |
| 47 | + draft.unsignedTx = castDraft(action.unsignedTx) |
| 48 | + break |
| 49 | + |
| 50 | + case ReviewTxActionType.CborChanged: |
| 51 | + draft.cbor = action.cbor |
| 52 | + break |
| 53 | + |
| 54 | + case ReviewTxActionType.OperationsChanged: |
| 55 | + draft.operations = action.operations |
| 56 | + break |
| 57 | + |
| 58 | + case ReviewTxActionType.OnSuccessChanged: |
| 59 | + draft.onSuccess = action.onSuccess |
| 60 | + break |
| 61 | + |
| 62 | + case ReviewTxActionType.OnErrorChanged: |
| 63 | + draft.onError = action.onError |
| 64 | + break |
| 65 | + |
| 66 | + default: |
| 67 | + throw new Error('[ReviewTxContext] invalid action') |
| 68 | + } |
| 69 | + }) |
| 70 | +} |
| 71 | + |
| 72 | +type ReviewTxAction = |
| 73 | + | { |
| 74 | + type: ReviewTxActionType.UnsignedTxChanged |
| 75 | + unsignedTx: ReviewTxState['unsignedTx'] |
| 76 | + } |
| 77 | + | { |
| 78 | + type: ReviewTxActionType.CborChanged |
| 79 | + cbor: ReviewTxState['cbor'] |
| 80 | + } |
| 81 | + | { |
| 82 | + type: ReviewTxActionType.OperationsChanged |
| 83 | + operations: ReviewTxState['operations'] |
| 84 | + } |
| 85 | + | { |
| 86 | + type: ReviewTxActionType.OnSuccessChanged |
| 87 | + onSuccess: ReviewTxState['onSuccess'] |
| 88 | + } |
| 89 | + | { |
| 90 | + type: ReviewTxActionType.OnErrorChanged |
| 91 | + onError: ReviewTxState['onError'] |
| 92 | + } |
| 93 | + |
| 94 | +export type ReviewTxState = { |
| 95 | + unsignedTx: YoroiUnsignedTx | null |
| 96 | + cbor: string | null |
| 97 | + operations: Array<React.ReactNode> | null |
| 98 | + onSuccess: ((signedTx: YoroiSignedTx) => void) | null |
| 99 | + onError: (() => void) | null |
| 100 | +} |
| 101 | + |
| 102 | +type ReviewTxActions = { |
| 103 | + unsignedTxChanged: (unsignedTx: ReviewTxState['unsignedTx']) => void |
| 104 | + cborChanged: (cbor: ReviewTxState['cbor']) => void |
| 105 | + operationsChanged: (operations: ReviewTxState['operations']) => void |
| 106 | + onSuccessChanged: (onSuccess: ReviewTxState['onSuccess']) => void |
| 107 | + onErrorChanged: (onError: ReviewTxState['onError']) => void |
| 108 | +} |
| 109 | + |
| 110 | +const defaultState: ReviewTxState = Object.freeze({ |
| 111 | + unsignedTx: null, |
| 112 | + cbor: null, |
| 113 | + operations: null, |
| 114 | + onSuccess: null, |
| 115 | + onError: null, |
| 116 | +}) |
| 117 | + |
| 118 | +function missingInit() { |
| 119 | + console.error('[ReviewTxContext] missing initialization') |
| 120 | +} |
| 121 | + |
| 122 | +const initialReviewTxContext: ReviewTxContext = { |
| 123 | + ...defaultState, |
| 124 | + unsignedTxChanged: missingInit, |
| 125 | + cborChanged: missingInit, |
| 126 | + operationsChanged: missingInit, |
| 127 | + onSuccessChanged: missingInit, |
| 128 | + onErrorChanged: missingInit, |
| 129 | +} |
| 130 | + |
| 131 | +enum ReviewTxActionType { |
| 132 | + UnsignedTxChanged = 'unsignedTxChanged', |
| 133 | + CborChanged = 'cborChanged', |
| 134 | + OperationsChanged = 'operationsChanged', |
| 135 | + OnSuccessChanged = 'onSuccessChanged', |
| 136 | + OnErrorChanged = 'onErrorChanged', |
| 137 | +} |
| 138 | + |
| 139 | +type ReviewTxContext = ReviewTxState & ReviewTxActions |
| 140 | + |
| 141 | +const ReviewTxContext = React.createContext<ReviewTxContext>(initialReviewTxContext) |
0 commit comments