Skip to content

Commit

Permalink
chore(suite): do not remember selected fee
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasklim committed Jul 2, 2024
1 parent 96c4b6d commit 2c1b5c1
Show file tree
Hide file tree
Showing 9 changed files with 20 additions and 81 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
export const SET_LOCAL_CURRENCY = '@wallet-settings/set-local-currency';
export const SET_HIDE_BALANCE = '@wallet-settings/hide-balance';
export const CHANGE_NETWORKS = '@wallet-settings/change-networks';
export const SET_LAST_USED_FEE_LEVEL = '@wallet-settings/set-last-used-fee-level';
export const FROM_STORAGE = '@wallet-settings/from-storage';
export const SET_BITCOIN_AMOUNT_UNITS = '@suite/set-bitcoin-amount-units';
18 changes: 0 additions & 18 deletions packages/suite/src/actions/settings/walletSettingsActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,24 +78,6 @@ export const changeCoinVisibility =
});
};

export const setLastUsedFeeLevel =
(feeLevel?: FeeLevel) => (dispatch: Dispatch, getState: GetState) => {
const { selectedAccount } = getState().wallet;
if (selectedAccount.status !== 'loaded') return;
dispatch({
type: WALLET_SETTINGS.SET_LAST_USED_FEE_LEVEL,
symbol: selectedAccount.account.symbol,
feeLevel,
});
};

export const getLastUsedFeeLevel = () => (_: Dispatch, getState: GetState) => {
const { selectedAccount, settings } = getState().wallet;
if (selectedAccount.status !== 'loaded') return;

return settings.lastUsedFeeLevel[selectedAccount.account.symbol];
};

export const setBitcoinAmountUnits = (units: PROTO.AmountUnit): WalletSettingsAction => {
analytics.report({
type: EventType.SettingsGeneralChangeBitcoinUnit,
Expand Down
3 changes: 0 additions & 3 deletions packages/suite/src/components/wallet/Fees/CustomFee.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ interface CustomFeeProps<TFieldValues extends FormState> {
control: Control;
setValue: UseFormSetValue<TFieldValues>;
getValues: UseFormGetValues<TFieldValues>;
changeFeeLimit?: (value: string) => void;
composedFeePerByte: string;
}

Expand All @@ -61,7 +60,6 @@ export const CustomFee = <TFieldValues extends FormState>({
feeInfo,
register,
control,
changeFeeLimit,
composedFeePerByte,
...props
}: CustomFeeProps<TFieldValues>) => {
Expand Down Expand Up @@ -173,7 +171,6 @@ export const CustomFee = <TFieldValues extends FormState>({
inputState={getInputState(feeLimitError)}
name={FEE_LIMIT}
data-test={FEE_LIMIT}
onChange={changeFeeLimit}
rules={feeLimitRules}
/>
) : (
Expand Down
3 changes: 0 additions & 3 deletions packages/suite/src/components/wallet/Fees/Fees.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ export interface FeesProps<TFieldValues extends FormState> {
getValues: UseFormGetValues<TFieldValues>;
errors: FieldErrors<TFieldValues>;
changeFeeLevel: (level: FeeLevel['label']) => void;
changeFeeLimit?: (value: string) => void;
composedLevels?: PrecomposedLevels | PrecomposedLevelsCardano;
label?: ExtendedMessageDescriptor['id'];
rbfForm?: boolean;
Expand All @@ -120,7 +119,6 @@ export const Fees = <TFieldValues extends FormState>({
feeInfo,
control,
changeFeeLevel,
changeFeeLimit,
composedLevels,
label,
rbfForm,
Expand Down Expand Up @@ -220,7 +218,6 @@ export const Fees = <TFieldValues extends FormState>({
register={register}
getValues={getValues}
setValue={setValue}
changeFeeLimit={changeFeeLimit}
composedFeePerByte={
(transactionInfo as PrecomposedTransactionFinal)?.feePerByte
}
Expand Down
24 changes: 2 additions & 22 deletions packages/suite/src/hooks/wallet/form/useFees.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { useEffect, useRef } from 'react';
import { FieldPath, UseFormReturn } from 'react-hook-form';
import { FeeLevel } from '@trezor/connect';
import { setLastUsedFeeLevel } from 'src/actions/settings/walletSettingsActions';
import { useDispatch } from 'src/hooks/suite';
import {
FeeInfo,
Expand Down Expand Up @@ -80,21 +79,8 @@ export const useFees = <TFieldValues extends FormState>({
}

// compose
if (updateField) {
if (composeRequest) {
composeRequest(updateField);
}
// save last used fee
if (
saveLastUsedFeeRef.current &&
feePerUnit &&
!errors.feePerUnit &&
!errors.feeLimit
) {
dispatch(
setLastUsedFeeLevel({ label: 'custom', feePerUnit, feeLimit, blocks: -1 }),
);
}
if (updateField && composeRequest) {
composeRequest(updateField);
}
}, [dispatch, feePerUnit, feeLimit, errors.feePerUnit, errors.feeLimit, composeRequest]);

Expand Down Expand Up @@ -152,12 +138,6 @@ export const useFees = <TFieldValues extends FormState>({
// on change callback
if (onChange) onChange(selectedFeeRef.current, level);

// save last used fee
if (level !== 'custom' && saveLastUsedFeeRef.current) {
const nextLevel = feeInfo.levels.find(l => l.label === (level || 'normal'))!;
dispatch(setLastUsedFeeLevel(nextLevel));
}

selectedFeeRef.current = selectedFee;
};

Expand Down
29 changes: 9 additions & 20 deletions packages/suite/src/hooks/wallet/useSendForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ import {
saveSendFormDraftThunk,
signAndPushSendFormTransactionThunk,
} from 'src/actions/wallet/send/sendFormThunks';
import {
getLastUsedFeeLevel,
setLastUsedFeeLevel,
} from 'src/actions/settings/walletSettingsActions';
import { goto } from 'src/actions/suite/routerActions';
import { fillSendForm } from 'src/actions/suite/protocolActions';
import { AppState } from 'src/types/suite';
Expand Down Expand Up @@ -125,25 +121,12 @@ export const useSendForm = (props: UseSendFormProps): SendContextValues => {
// used in "loadDraft" useEffect and "importTransaction" callback
const getLoadedValues = useCallback(
(loadedState?: Partial<FormState>) => {
const feeEnhancement: Partial<FormState> = {};
if (!loadedState || !loadedState.selectedFee) {
const lastUsedFee = dispatch(getLastUsedFeeLevel());
if (lastUsedFee) {
feeEnhancement.selectedFee = lastUsedFee.label;
if (lastUsedFee.label === 'custom') {
feeEnhancement.feePerUnit = lastUsedFee.feePerUnit;
feeEnhancement.feeLimit = lastUsedFee.feeLimit;
}
}
}

return {
...getDefaultValues(localCurrencyOption, state.network),
...loadedState,
...feeEnhancement,
};
},
[dispatch, localCurrencyOption, state.network],
[localCurrencyOption, state.network],
);

// update custom values
Expand Down Expand Up @@ -223,7 +206,6 @@ export const useSendForm = (props: UseSendFormProps): SendContextValues => {
const resetContext = useCallback(() => {
setComposedLevels(undefined);
dispatch(removeSendFormDraftThunk()); // reset draft;
dispatch(setLastUsedFeeLevel()); // reset last known FeeLevel
setState(getStateFromProps(props)); // resetting state will trigger "loadDraft" useEffect block, which will reset FormState to default
}, [dispatch, props, setComposedLevels]);

Expand Down Expand Up @@ -359,7 +341,14 @@ export const useSendForm = (props: UseSendFormProps): SendContextValues => {
useEffect(() => {
if (!draftSaveRequest) return;
if (Object.keys(formState.errors).length === 0) {
dispatch(saveSendFormDraftThunk({ formState: getValues() }));
dispatch(
saveSendFormDraftThunk({
formState: {
...getValues(),
selectedFee: undefined,
},
}),
);
}
setDraftSaveRequest(false);
}, [dispatch, draftSaveRequest, setDraftSaveRequest, getValues, formState.errors]);
Expand Down
1 change: 0 additions & 1 deletion packages/suite/src/middlewares/wallet/storageMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,6 @@ const storageMiddleware = (api: MiddlewareAPI<Dispatch, AppState>) => {
case WALLET_SETTINGS.SET_HIDE_BALANCE:
case walletSettingsActions.setLocalCurrency.type:
case WALLET_SETTINGS.SET_BITCOIN_AMOUNT_UNITS:
case WALLET_SETTINGS.SET_LAST_USED_FEE_LEVEL:
api.dispatch(storageActions.saveWalletSettings());
break;

Expand Down
19 changes: 9 additions & 10 deletions packages/suite/src/storage/migrations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -812,16 +812,6 @@ export const migrate: OnUpgradeFunc<SuiteDBSchema> = async (
return tx;
});

await updateAll(transaction, 'walletSettings', walletSettings => {
Object.keys(walletSettings.lastUsedFeeLevel).forEach(coin => {
if (walletSettings.lastUsedFeeLevel[coin].label === 'low') {
delete walletSettings.lastUsedFeeLevel[coin];
}
});

return walletSettings;
});

await updateAll(transaction, 'suiteSettings', suiteSettings => {
// @ts-expect-error
delete suiteSettings.flags.showDashboardT2B1PromoBanner;
Expand Down Expand Up @@ -883,4 +873,13 @@ export const migrate: OnUpgradeFunc<SuiteDBSchema> = async (
return tx;
});
}

if (oldVersion < 47) {
await updateAll(transaction, 'walletSettings', walletSettings => {
// @ts-expect-error
delete walletSettings.lastUsedFeeLevel;

return walletSettings;
});
}
};
3 changes: 0 additions & 3 deletions suite-common/wallet-types/src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,4 @@ export interface WalletSettings {
discreetMode: boolean;
enabledNetworks: Network['symbol'][];
bitcoinAmountUnit: PROTO.AmountUnit;
lastUsedFeeLevel: {
[key: string]: Omit<FeeLevel, 'blocks'>; // Key: Network['symbol']
};
}

0 comments on commit 2c1b5c1

Please sign in to comment.