* building send and replace settings context * adding translations * fixing double parse * adding spend and replace flags to send txs * adding listener to handle sar payments * add tx descripion handler for sar payments in restore * making sure sar key is fetched during app load * split toast manager and toast continer * adding tests * adding custom sar payment title to toast message * removing spark sdk navigation * adding sarPayment mark to transaction table * pass poolInfoRef from context to remove fetch if value is already set * remove spark naviagtion listener * fix btc -> usd swaps not calling sar * finalize spend and replace * fix timeout mismatch * adding usablePaymentMethod to narrow eligiblility * block requsets if account switches * remove duplicate storage and keep all referances to keyscontext * steralize writes to fix overlap issue
101 lines
2.4 KiB
JavaScript
101 lines
2.4 KiB
JavaScript
import React, {
|
|
createContext,
|
|
useContext,
|
|
useReducer,
|
|
useCallback,
|
|
useMemo,
|
|
} from 'react';
|
|
|
|
const ToastStateContext = createContext();
|
|
const ToastActionsContext = createContext();
|
|
|
|
const initialState = {
|
|
toasts: [],
|
|
};
|
|
|
|
const toastReducer = (state, action) => {
|
|
switch (action.type) {
|
|
case 'ADD_TOAST':
|
|
return {
|
|
...state,
|
|
toasts: [...state.toasts, action.payload],
|
|
};
|
|
case 'REMOVE_TOAST':
|
|
return {
|
|
...state,
|
|
toasts: state.toasts.filter(toast => toast.id !== action.payload),
|
|
};
|
|
case 'CLEAR_TOASTS':
|
|
return {
|
|
...state,
|
|
toasts: [],
|
|
};
|
|
default:
|
|
return state;
|
|
}
|
|
};
|
|
|
|
export const ToastProvider = ({ children }) => {
|
|
const [state, dispatch] = useReducer(toastReducer, initialState);
|
|
|
|
const showToast = useCallback(toast => {
|
|
const id = Date.now() + Math.random();
|
|
const toastWithId = {
|
|
id,
|
|
type: 'success',
|
|
duration: 3000,
|
|
position: 'top',
|
|
...toast,
|
|
};
|
|
|
|
dispatch({ type: 'ADD_TOAST', payload: toastWithId });
|
|
|
|
if (toastWithId.duration > 0) {
|
|
setTimeout(() => {
|
|
dispatch({ type: 'REMOVE_TOAST', payload: id });
|
|
}, toastWithId.duration);
|
|
}
|
|
|
|
return id;
|
|
}, []);
|
|
|
|
const hideToast = useCallback(id => {
|
|
dispatch({ type: 'REMOVE_TOAST', payload: id });
|
|
}, []);
|
|
|
|
const clearToasts = useCallback(() => {
|
|
dispatch({ type: 'CLEAR_TOASTS' });
|
|
}, []);
|
|
|
|
const actionsValue = useMemo(
|
|
() => ({ showToast, hideToast, clearToasts }),
|
|
[showToast, hideToast, clearToasts],
|
|
);
|
|
const stateValue = useMemo(() => ({ toasts: state.toasts }), [state.toasts]);
|
|
|
|
return (
|
|
<ToastActionsContext.Provider value={actionsValue}>
|
|
<ToastStateContext.Provider value={stateValue}>
|
|
{children}
|
|
</ToastStateContext.Provider>
|
|
</ToastActionsContext.Provider>
|
|
);
|
|
};
|
|
|
|
export const useToast = () => {
|
|
const stateContext = useContext(ToastStateContext);
|
|
const actionsContext = useContext(ToastActionsContext);
|
|
if (!stateContext || !actionsContext) {
|
|
throw new Error('useToast must be used within a ToastProvider');
|
|
}
|
|
return { ...stateContext, ...actionsContext };
|
|
};
|
|
|
|
export const useToastActions = () => {
|
|
const context = useContext(ToastActionsContext);
|
|
if (!context) {
|
|
throw new Error('useToastActions must be used within a ToastProvider');
|
|
}
|
|
return context;
|
|
};
|