0f91af1df2
* move the gas estimation stuff into its own hook and report errors from the gas estimation * fix linter errors * show the swap callback error separately * rename some variables * use a manually specified key for gas estimates * flip price... thought i did this already * only show swap callback error if approval state is approved * some clean up to the swap components * stop proactively looking for gas estimates * improve some retry stuff, show errors inline * add another retry test * latest ethers * fix integration tests * simplify modal and fix jitter on open in mobile * refactor confirmation modal into pieces before creating the error content * finish refactoring of transaction confirmation modal * show error state in the transaction confirmation modal * fix lint errors * error not always relevant * fix lint errors, remove action item * move a lot of code into ConfirmSwapModal.tsx * show accept changes flow, not styled * Adjust styles for slippage error states * Add styles for updated price prompt * Add input/output highlighting * lint errors * fix link to wallets in modal * use total supply instead of reserves for `noLiquidity` (fixes #701) * bump the walletconnect version to the fixed alpha Co-authored-by: Callil Capuozzo <callil.capuozzo@gmail.com>
34 lines
1022 B
TypeScript
34 lines
1022 B
TypeScript
import { useEffect, useState } from 'react'
|
|
|
|
/**
|
|
* Returns the last value of type T that passes a filter function
|
|
* @param value changing value
|
|
* @param filterFn function that determines whether a given value should be considered for the last value
|
|
*/
|
|
export default function useLast<T>(
|
|
value: T | undefined | null,
|
|
filterFn?: (value: T | null | undefined) => boolean
|
|
): T | null | undefined {
|
|
const [last, setLast] = useState<T | null | undefined>(filterFn && filterFn(value) ? value : undefined)
|
|
useEffect(() => {
|
|
setLast(last => {
|
|
const shouldUse: boolean = filterFn ? filterFn(value) : true
|
|
if (shouldUse) return value
|
|
return last
|
|
})
|
|
}, [filterFn, value])
|
|
return last
|
|
}
|
|
|
|
function isDefined<T>(x: T | null | undefined): x is T {
|
|
return x !== null && x !== undefined
|
|
}
|
|
|
|
/**
|
|
* Returns the last truthy value of type T
|
|
* @param value changing value
|
|
*/
|
|
export function useLastTruthy<T>(value: T | undefined | null): T | null | undefined {
|
|
return useLast(value, isDefined)
|
|
}
|