-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3437 from cowprotocol/hotfix/1.49.7
fix: hotfix 1.49.7
- Loading branch information
Showing
10 changed files
with
612 additions
and
22 deletions.
There are no files selected for viewing
62 changes: 52 additions & 10 deletions
62
apps/cowswap-frontend/src/modules/appData/updater/AppDataHooksUpdater.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,67 @@ | ||
import { useEffect, useRef } from 'react' | ||
import { useEffect, useMemo, useRef } from 'react' | ||
|
||
import { PermitHookData } from '@cowprotocol/permit-utils' | ||
|
||
import { useAccountAgnosticPermitHookData } from 'modules/permit' | ||
import { useDerivedSwapInfo } from 'modules/swap/hooks/useSwapState' | ||
|
||
import { useLimitHasEnoughAllowance } from '../../limitOrders/hooks/useTradeFlowContext' | ||
import { useSwapEnoughAllowance } from '../../swap/hooks/useSwapFlowContext' | ||
import { useUpdateAppDataHooks } from '../hooks' | ||
import { buildAppDataHooks } from '../utils/buildAppDataHooks' | ||
|
||
// const count = 0 | ||
|
||
function usePermitDataIfNotAllowance(): PermitHookData | undefined { | ||
const permitHookData = useAccountAgnosticPermitHookData() || {} | ||
|
||
// Remove permitData if the user has enough allowance for the current trade | ||
const swapHasEnoughAllowance = useSwapEnoughAllowance() | ||
const limitHasEnoughAllowance = useLimitHasEnoughAllowance() | ||
const shouldUsePermit = swapHasEnoughAllowance === false || limitHasEnoughAllowance === false | ||
|
||
const { target, callData, gasLimit }: Partial<PermitHookData> = permitHookData || {} | ||
|
||
return useMemo(() => { | ||
if (!target || !callData || !gasLimit) { | ||
return undefined | ||
} | ||
|
||
return shouldUsePermit ? { target, callData, gasLimit } : undefined | ||
}, [shouldUsePermit, target, callData, gasLimit]) | ||
} | ||
|
||
export function AppDataHooksUpdater(): null { | ||
const { v2Trade } = useDerivedSwapInfo() | ||
const updateAppDataHooks = useUpdateAppDataHooks() | ||
const permitHookData = useAccountAgnosticPermitHookData() | ||
|
||
// To avoid dumb re-renders | ||
const ref = useRef(permitHookData) | ||
ref.current = permitHookData | ||
const stableRef = JSON.stringify(permitHookData) | ||
const permitData = usePermitDataIfNotAllowance() | ||
const permitDataPrev = useRef<PermitHookData | undefined>(undefined) | ||
const hasTradeInfo = !!v2Trade | ||
|
||
useEffect(() => { | ||
if (stableRef) { | ||
const hooks = buildAppDataHooks(ref.current ? [ref.current] : undefined) | ||
if ( | ||
!hasTradeInfo || // If there's no trade info, wait until we have one to update the hooks (i.e. missing quote) | ||
JSON.stringify(permitDataPrev.current) === JSON.stringify(permitData) // Or if the permit data has not changed | ||
) { | ||
return undefined | ||
} | ||
|
||
const hooks = buildAppDataHooks({ | ||
preInteractionHooks: permitData ? [permitData] : undefined, | ||
}) | ||
|
||
if (hooks) { | ||
// Update the hooks | ||
console.log('[AppDataHooksUpdater]: Set hooks', hooks) | ||
updateAppDataHooks(hooks) | ||
permitDataPrev.current = permitData | ||
} else { | ||
// There was a hook data, but not any more. The hook needs to be removed | ||
console.log('[AppDataHooksUpdater] Clear hooks') | ||
updateAppDataHooks(undefined) | ||
permitDataPrev.current = undefined | ||
} | ||
}, [stableRef, updateAppDataHooks]) | ||
}, [updateAppDataHooks, permitData, hasTradeInfo]) | ||
|
||
return null | ||
} |
9 changes: 6 additions & 3 deletions
9
apps/cowswap-frontend/src/modules/appData/utils/buildAppDataHooks.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
f22c3cf
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
widget-configurator – ./
widget.cow.fi
widget-configurator-git-main-cowswap.vercel.app
widget-configurator-cowswap.vercel.app
widget-configurator-seven.vercel.app
f22c3cf
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
cosmos – ./
cowswap-seven.vercel.app
cosmos-cowswap.vercel.app
cosmos-git-main-cowswap.vercel.app
cosmos.cow.fi