Skip to content

Commit

Permalink
aaaaaaa
Browse files Browse the repository at this point in the history
  • Loading branch information
greg-schrammel committed Mar 25, 2024
1 parent 8784ce8 commit 8d35099
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 71 deletions.
70 changes: 44 additions & 26 deletions src/entries/popup/hooks/useGas.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable no-nested-ternary */
import { TransactionRequest } from '@ethersproject/abstract-provider';
import { CrosschainQuote, Quote, QuoteError } from '@rainbow-me/swaps';
import { useCallback, useEffect, useMemo, useState } from 'react';
import { useCallback, useEffect, useMemo, useReducer, useState } from 'react';
import { Address } from 'wagmi';

import { gasUnits } from '~/core/references';
Expand Down Expand Up @@ -204,8 +204,6 @@ const useGas = ({
storeGasFeeParamsBySpeed?.custom,
]);

const [selectedSpeed, setSelectedSpeed] = useState<GasSpeed>(defaultSpeed);

const gasFeeParamsBySpeed:
| GasFeeParamsBySpeed
| GasFeeLegacyParamsBySpeed
Expand Down Expand Up @@ -247,29 +245,49 @@ const useGas = ({
storeGasFeeParamsBySpeed.custom,
]);

useEffect(() => {
if (prevDefaultSpeed !== defaultSpeed) {
setSelectedSpeed(defaultSpeed);
}
}, [defaultSpeed, prevDefaultSpeed]);

useEffect(() => {
if (
enabled &&
gasFeeParamsBySpeed?.[selectedSpeed] &&
gasFeeParamsChanged(selectedGas, gasFeeParamsBySpeed?.[selectedSpeed])
) {
setSelectedGas({
selectedGas: gasFeeParamsBySpeed[selectedSpeed],
});
}
}, [
enabled,
gasFeeParamsBySpeed,
selectedGas,
selectedSpeed,
setSelectedGas,
]);
const [selectedSpeed, setSelectedSpeed] = useReducer<
(s: GasSpeed, sa: GasSpeed) => GasSpeed
>((s, selectedSpeed) => {
if (!gasFeeParamsBySpeed?.[selectedSpeed]) return s;
setSelectedGas({ selectedGas: gasFeeParamsBySpeed[selectedSpeed] });
return selectedSpeed;
}, defaultSpeed || GasSpeed.NORMAL);

// useEffect(() => {
// if (prevDefaultSpeed !== defaultSpeed) {
// setSelectedSpeed(defaultSpeed);
// }
// }, [defaultSpeed, prevDefaultSpeed]);

// // useEffect(() => {
// if (
// enabled &&
// gasFeeParamsBySpeed?.[selectedSpeed] &&
// gasFeeParamsChanged(selectedGas, gasFeeParamsBySpeed?.[selectedSpeed])
// ) {
// console.log(
// 'AAAAA',
// {
// a: [selectedGas, gasFeeParamsBySpeed?.[selectedSpeed]],
// selectedSpeed,
// },
// gasFeeParamsChanged(selectedGas, gasFeeParamsBySpeed?.[selectedSpeed]),
// );

// // if (i === 0) {
// setSelectedGas({
// selectedGas: gasFeeParamsBySpeed[selectedSpeed],
// });
// // i++;
// // }
// }
// // }, [
// // enabled,
// // gasFeeParamsBySpeed,
// // selectedGas,
// // selectedSpeed,
// // setSelectedGas,
// // ]);

useEffect(() => {
if (
Expand Down
47 changes: 2 additions & 45 deletions src/entries/popup/pages/speedUpAndCancelSheet/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { TransactionRequest } from '@ethersproject/abstract-provider';
import BigNumber from 'bignumber.js';
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
import { useCallback, useMemo, useState } from 'react';
import {
Address,
useAccount,
Expand All @@ -11,7 +11,6 @@ import {

import { i18n } from '~/core/languages';
import { useGasStore } from '~/core/state';
import { useSelectedTransactionStore } from '~/core/state/selectedTransaction';
import { ChainId } from '~/core/types/chains';
import {
GasSpeed,
Expand Down Expand Up @@ -69,40 +68,6 @@ type SpeedUpAndCancelSheetProps = {
transaction: RainbowTransaction;
};

function useWhyDidYouUpdate(name, props) {
// Get a mutable ref object where we can store props ...
// ... for comparison next time this hook runs.
const previousProps = useRef() as any;

useEffect(() => {
if (previousProps.current) {
// Get all keys from previous and current props
const allKeys = Object.keys({ ...previousProps.current, ...props });
// Use this object to keep track of changed props
const changesObj = {};
// Iterate through keys
allKeys.forEach((key) => {
// If previous is different from current
if (previousProps.current[key] !== props[key]) {
// Add to changesObj
changesObj[key] = {
from: previousProps.current[key],
to: props[key],
};
}
});

// If changesObj not empty then output to console
if (Object.keys(changesObj).length) {
console.log('[why-did-you-update]', name, changesObj);
}
}

// Finally update previousProps with current props for next hook call
previousProps.current = props;
});
}

// governs type of sheet displayed on top of MainLayout
// we should centralize this type if we add additional
// sheet modes to the main layout
Expand All @@ -113,7 +78,7 @@ export function SpeedUpAndCancelSheet({
onClose,
transaction,
}: SpeedUpAndCancelSheetProps) {
const { setSelectedTransaction } = useSelectedTransactionStore();
// const { setSelectedTransaction } = useSelectedTransactionStore();
const { selectedGas } = useGasStore();
const [sending, setSending] = useState(false);

Expand All @@ -125,14 +90,6 @@ export function SpeedUpAndCancelSheet({
});
const cancel = currentSheet === 'cancel';

useWhyDidYouUpdate('SpeedUpAndCancelSheet', {
currentSheet,
onClose,
transaction,
selectedGas,
sending,
});

const onExecuteTransaction = () => {
if (cancel) handleCancellation();
else handleSpeedUp();
Expand Down

0 comments on commit 8d35099

Please sign in to comment.