Skip to content

Commit 0312002

Browse files
committed
feat: Add swap reset
1 parent 781a52d commit 0312002

File tree

9 files changed

+118
-71
lines changed

9 files changed

+118
-71
lines changed

apps/web/src/components/Menu/GlobalSettings/SettingsModal.tsx

+2-5
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,7 @@ import { useActiveChainId } from 'hooks/useActiveChainId'
2929
import useTheme from 'hooks/useTheme'
3030
import { useWebNotifications } from 'hooks/useWebNotifications'
3131
import { ReactNode, Suspense, lazy, useCallback, useState } from 'react'
32-
import {
33-
useGlobalSettingsChanged,
34-
useSubgraphHealthIndicatorManager,
35-
useUserUsernameVisibility,
36-
} from 'state/user/hooks'
32+
import { useSubgraphHealthIndicatorManager, useUserUsernameVisibility } from 'state/user/hooks'
3733
import { useUserShowTestnet } from 'state/user/hooks/useUserShowTestnet'
3834
import { useUserTokenRisk } from 'state/user/hooks/useUserTokenRisk'
3935
import {
@@ -46,6 +42,7 @@ import {
4642
} from 'state/user/smartRouter'
4743
import { usePCSX, usePCSXFeatureEnabled } from 'hooks/usePCSX'
4844
import { styled } from 'styled-components'
45+
import { useGlobalSettingsChanged } from 'hooks/useGlobalSettingsChanged'
4946
import GasSettings from './GasSettings'
5047

5148
const WebNotiToggle = lazy(() => import('./WebNotiToggle'))

apps/web/src/components/Menu/GlobalSettings/SettingsModalV2/index.tsx

+9-3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { useExpertMode, useUserExpertModeAcknowledgement } from '@pancakeswap/ut
1818

1919
import { ReactNode, useCallback, useId, useState } from 'react'
2020
import { useRoutingSettingChanged } from 'state/user/smartRouter'
21+
import { useSwapSettingsChanged } from 'hooks/useSwapSettingsChanged'
2122
import SettingsModal from '../SettingsModal'
2223
import { SettingsMode } from '../types'
2324
import { CustomizeRoutingTab } from './CustomizeRoutingTab'
@@ -55,6 +56,7 @@ export const SettingsModalV2 = ({
5556
const { t } = useTranslation()
5657
const { isMobile } = useMatchBreakpoints()
5758

59+
const { isSwapSettingsChanged, resetSettings } = useSwapSettingsChanged()
5860
const [showExpertModeAcknowledgement, setShowExpertModeAcknowledgement] = useUserExpertModeAcknowledgement()
5961
const [expertMode, setExpertMode] = useExpertMode()
6062
const [isRoutingSettingChange, reset] = useRoutingSettingChanged()
@@ -153,16 +155,20 @@ export const SettingsModalV2 = ({
153155
return <SettingsModal onDismiss={onDismissGlobalSettings} />
154156
}
155157

158+
const showResetButton = Boolean(
159+
activeTabIndex === TabIndex.CUSTOMIZE_ROUTING ? isRoutingSettingChange : isSwapSettingsChanged,
160+
)
161+
const onReset = activeTabIndex === TabIndex.CUSTOMIZE_ROUTING ? reset : resetSettings
162+
156163
return (
157164
<MotionModal
158165
minWidth={[null, null, '420px']}
159166
minHeight={isMobile ? '500px' : undefined}
160167
headerPadding="2px 14px 0 24px"
161168
headerRightSlot={
162-
activeTabIndex === TabIndex.CUSTOMIZE_ROUTING &&
163-
isRoutingSettingChange && (
169+
showResetButton && (
164170
<TabContent type="to_right">
165-
<Button ml="8px" variant="text" scale="sm" onClick={reset}>
171+
<Button ml="8px" variant="text" scale="sm" onClick={onReset}>
166172
{t('Reset')}
167173
</Button>
168174
</TabContent>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { useUserShowTestnet } from 'state/user/hooks/useUserShowTestnet'
2+
import { useUserTokenRisk } from 'state/user/hooks/useUserTokenRisk'
3+
import { useWebNotifications } from 'hooks/useWebNotifications'
4+
import { useAllowNotifications } from 'state/notifications/hooks'
5+
import { useCallback } from 'react'
6+
import { useGasPriceManager, useSubgraphHealthIndicatorManager, useUserUsernameVisibility } from 'state/user/hooks'
7+
8+
export function useGlobalSettingsChanged() {
9+
const [subgraphHealth, setSubgraphHealth, defaultSubgraphHealthValue] = useSubgraphHealthIndicatorManager()
10+
const [userUsernameVisibility, setUsernameVisibility, defaultUserUsernameVisibilityValue] =
11+
useUserUsernameVisibility()
12+
const [showTestnet, setShowTestnet, defaultShowTestnetValue] = useUserShowTestnet()
13+
const [tokenRisk, setTokenRisk, defaultTokenRiskValue] = useUserTokenRisk()
14+
const { enabled: notificationsEnabled, defaultValue: defaultNotificationsValue } = useWebNotifications()
15+
const [, setAllowNotifications] = useAllowNotifications()
16+
const [gasPrice, setGasPrice, defaultGasPrice] = useGasPriceManager()
17+
18+
const resetSettings = useCallback(() => {
19+
setSubgraphHealth(defaultSubgraphHealthValue)
20+
setUsernameVisibility(defaultUserUsernameVisibilityValue)
21+
setShowTestnet(defaultShowTestnetValue)
22+
setTokenRisk(defaultTokenRiskValue)
23+
setAllowNotifications(defaultNotificationsValue ?? true)
24+
setGasPrice(defaultGasPrice)
25+
}, [
26+
setShowTestnet,
27+
setTokenRisk,
28+
setAllowNotifications,
29+
setGasPrice,
30+
setSubgraphHealth,
31+
setUsernameVisibility,
32+
defaultNotificationsValue,
33+
defaultShowTestnetValue,
34+
defaultSubgraphHealthValue,
35+
defaultTokenRiskValue,
36+
defaultUserUsernameVisibilityValue,
37+
defaultGasPrice,
38+
])
39+
40+
return {
41+
isGlobalSettingsChanged:
42+
subgraphHealth !== defaultSubgraphHealthValue ||
43+
userUsernameVisibility !== defaultUserUsernameVisibilityValue ||
44+
showTestnet !== defaultShowTestnetValue ||
45+
tokenRisk !== defaultTokenRiskValue ||
46+
notificationsEnabled !== defaultNotificationsValue ||
47+
gasPrice !== defaultGasPrice,
48+
resetSettings,
49+
}
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { useAudioPlay, useExpertMode, useUserSlippage } from '@pancakeswap/utils/user'
2+
import { useCallback } from 'react'
3+
import { useGasPriceManager } from 'state/user/hooks'
4+
import { useUserTransactionTTL } from 'hooks/useTransactionDeadline'
5+
import { useRoutingSettingChanged } from 'state/user/smartRouter'
6+
7+
export function useSwapSettingsChanged() {
8+
const [gasPrice, setGasPrice, defaultGasPrice] = useGasPriceManager()
9+
const [userSlippage, setUserSlippage, defaultUserSlippage] = useUserSlippage()
10+
const [userTTL, setUserTTL, defaultUserTTL] = useUserTransactionTTL()
11+
const [audioPlay, setAudioPlay, defaultAudioPlay] = useAudioPlay()
12+
const [expertMode, setExpertMode, defaultExpertMode] = useExpertMode()
13+
const [isRoutingSettingChange, reset] = useRoutingSettingChanged()
14+
15+
const resetSettings = useCallback(() => {
16+
setUserSlippage(defaultUserSlippage)
17+
setUserTTL(defaultUserTTL)
18+
setAudioPlay(defaultAudioPlay)
19+
setExpertMode(defaultExpertMode)
20+
setGasPrice(defaultGasPrice)
21+
reset()
22+
}, [
23+
setGasPrice,
24+
setExpertMode,
25+
setAudioPlay,
26+
setUserTTL,
27+
setUserSlippage,
28+
defaultGasPrice,
29+
defaultExpertMode,
30+
defaultAudioPlay,
31+
defaultUserTTL,
32+
defaultUserSlippage,
33+
reset,
34+
])
35+
36+
return {
37+
isSwapSettingsChanged:
38+
isRoutingSettingChange ||
39+
userSlippage !== defaultUserSlippage ||
40+
userTTL !== defaultUserTTL ||
41+
audioPlay !== defaultAudioPlay ||
42+
expertMode !== defaultExpertMode ||
43+
gasPrice !== defaultGasPrice,
44+
resetSettings,
45+
}
46+
}

apps/web/src/hooks/useTransactionDeadline.ts

+3-10
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,9 @@ const userTxTTLAtom = atomWithStorage<number | undefined>('pcs:user:tx-ttl', und
1414
export function useUserTransactionTTL() {
1515
const { chainId } = useActiveChainId()
1616
const [userTTL, setTTL] = useAtom(userTxTTLAtom)
17-
const ttl = useMemo(
18-
() =>
19-
userTTL === undefined
20-
? chainId && L2_CHAIN_IDS.includes(chainId)
21-
? L2_DEADLINE_FROM_NOW
22-
: DEFAULT_DEADLINE_FROM_NOW
23-
: userTTL,
24-
[userTTL, chainId],
25-
)
26-
return [ttl, setTTL] as const
17+
const defaultValue = chainId && L2_CHAIN_IDS.includes(chainId) ? L2_DEADLINE_FROM_NOW : DEFAULT_DEADLINE_FROM_NOW
18+
const ttl = useMemo(() => (userTTL === undefined ? defaultValue : userTTL), [userTTL, defaultValue])
19+
return [ttl, setTTL, defaultValue] as const
2720
}
2821

2922
// combines the block timestamp with the user setting to give the deadline that should be used for any submitted transaction

apps/web/src/state/user/hooks/index.tsx

-49
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,6 @@ import { safeGetAddress } from 'utils'
1616
import { Hex, hexToBigInt } from 'viem'
1717
import { useWalletClient } from 'wagmi'
1818
import { initialState } from 'state/user/reducer'
19-
import { useUserShowTestnet } from 'state/user/hooks/useUserShowTestnet'
20-
import { useUserTokenRisk } from 'state/user/hooks/useUserTokenRisk'
21-
import { useWebNotifications } from 'hooks/useWebNotifications'
22-
import { useAllowNotifications } from 'state/notifications/hooks'
2319
import { useUserChart } from './useUserChart'
2420
import {
2521
FarmStakedOnly,
@@ -477,51 +473,6 @@ export function useTrackedTokenPairs(): [ERC20Token, ERC20Token][] {
477473
}, [combinedList])
478474
}
479475

480-
export function useGlobalSettingsChanged() {
481-
const [subgraphHealth, setSubgraphHealth, defaultSubgraphHealthValue] = useSubgraphHealthIndicatorManager()
482-
const [userUsernameVisibility, setUsernameVisibility, defaultUserUsernameVisibilityValue] =
483-
useUserUsernameVisibility()
484-
const [showTestnet, setShowTestnet, defaultShowTestnetValue] = useUserShowTestnet()
485-
const [tokenRisk, setTokenRisk, defaultTokenRiskValue] = useUserTokenRisk()
486-
const { enabled: notificationsEnabled, defaultValue: defaultNotificationsValue } = useWebNotifications()
487-
const [, setAllowNotifications] = useAllowNotifications()
488-
const [gasPrice, setGasPrice, defaultGasPrice] = useGasPriceManager()
489-
490-
const resetSettings = useCallback(() => {
491-
setSubgraphHealth(defaultSubgraphHealthValue)
492-
setUsernameVisibility(defaultUserUsernameVisibilityValue)
493-
setShowTestnet(defaultShowTestnetValue)
494-
setTokenRisk(defaultTokenRiskValue)
495-
setAllowNotifications(defaultNotificationsValue ?? true)
496-
setGasPrice(defaultGasPrice)
497-
}, [
498-
setShowTestnet,
499-
setTokenRisk,
500-
setAllowNotifications,
501-
setGasPrice,
502-
defaultGasPrice,
503-
defaultNotificationsValue,
504-
defaultShowTestnetValue,
505-
defaultSubgraphHealthValue,
506-
defaultTokenRiskValue,
507-
defaultUserUsernameVisibilityValue,
508-
gasPrice,
509-
setSubgraphHealth,
510-
setUsernameVisibility,
511-
])
512-
513-
return {
514-
isGlobalSettingsChanged:
515-
subgraphHealth !== defaultSubgraphHealthValue ||
516-
userUsernameVisibility !== defaultUserUsernameVisibilityValue ||
517-
showTestnet !== defaultShowTestnetValue ||
518-
tokenRisk !== defaultTokenRiskValue ||
519-
notificationsEnabled !== defaultNotificationsValue ||
520-
gasPrice !== defaultGasPrice,
521-
resetSettings,
522-
}
523-
}
524-
525476
/**
526477
* @deprecated
527478
*/

packages/utils/user/audioPlay.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import { useAtom } from 'jotai'
22
import { atomWithStorage } from 'jotai/utils'
33

4-
const userAudioPlayAtom = atomWithStorage('pcs:audio-play-2', false)
4+
const DEFAULT_VALUE = false
5+
6+
const userAudioPlayAtom = atomWithStorage('pcs:audio-play-2', DEFAULT_VALUE)
57

68
export function useAudioPlay() {
7-
return useAtom(userAudioPlayAtom)
9+
return [...useAtom(userAudioPlayAtom), DEFAULT_VALUE] as const
810
}

packages/utils/user/expertMode.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import { useAtom, useAtomValue } from 'jotai'
22
import { atomWithStorage } from 'jotai/utils'
33

4+
const DEFAULT_EXPERT_VALUE = false
5+
46
const userExpertModeAtom = atomWithStorage<boolean>('pcs:expert-mode', false)
57
const userExpertModeAcknowledgementAtom = atomWithStorage<boolean>('pcs:expert-mode-acknowledgement', true)
68

79
export function useExpertMode() {
8-
return useAtom(userExpertModeAtom)
10+
return [...useAtom(userExpertModeAtom), DEFAULT_EXPERT_VALUE] as const
911
}
1012

1113
export function useIsExpertMode() {

packages/utils/user/slippage.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const userSlippageAtomWithLocalStorage = atom(
1616
)
1717

1818
export const useUserSlippage = () => {
19-
return useAtom(userSlippageAtomWithLocalStorage)
19+
return [...useAtom(userSlippageAtomWithLocalStorage), INITIAL_ALLOWED_SLIPPAGE] as const
2020
}
2121

2222
// Derived atom for slippage as a Percent

0 commit comments

Comments
 (0)