Skip to content

Commit

Permalink
fix: Pcsx user settings not considered reset and not disabled on firs…
Browse files Browse the repository at this point in the history
…t click (#10874)

Issues:

If pcsx feature enabled and there is no user setting, disabling it not
make reset button appear.

Disabling the pcsx feature not work on first click and it has to be
clicked again

<!--
Before opening a pull request, please read the [contributing
guidelines](https://github.com/pancakeswap/pancake-frontend/blob/develop/CONTRIBUTING.md)
first
-->


<!-- start pr-codex -->

---

## PR-Codex overview
This PR focuses on enhancing the `usePCSX` hook and related state
management in the `smartRouter` module by adding a reset functionality
and improving the handling of user preferences.

### Detailed summary
- Updated `usePCSX` to return a reset function along with state.
- Introduced `useCallback` for setting user preferences in `usePCSX`.
- Modified `useUserXEnable` to return a reset function.
- Enhanced `useRoutingSettingChanged` to include reset functionality for
routing settings.

> ✨ Ask PR-Codex anything about this PR by commenting with `/codex {your
question}`

<!-- end pr-codex -->
  • Loading branch information
memoyil authored Oct 25, 2024
1 parent 9ca46f3 commit c36d84d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
13 changes: 9 additions & 4 deletions apps/web/src/hooks/usePCSX.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ChainId } from '@pancakeswap/chains'
import { EXPERIMENTAL_FEATURES } from 'config/experimentalFeatures'
import { SUPPORTED_CHAINS } from 'config/pcsx'
import { useExperimentalFeatureEnabled } from 'hooks/useExperimentalFeatureEnabled'
import { useMemo } from 'react'
import { useCallback, useMemo } from 'react'
import { useUserXEnable } from 'state/user/smartRouter'

export function usePCSXFeatureEnabled() {
Expand All @@ -11,10 +11,15 @@ export function usePCSXFeatureEnabled() {

export const usePCSX = () => {
const featureEnabled = usePCSXFeatureEnabled()
const [xEnabled, setX] = useUserXEnable()
const [xEnabled, setX, reset] = useUserXEnable()
const enabled = Boolean(xEnabled ?? featureEnabled)

return [enabled, setX] as const
const setUserX = useCallback(
(updater: boolean | ((current: boolean) => boolean)) => {
setX(typeof updater === 'function' ? updater(enabled) : updater)
},
[enabled],
)
return [enabled, setUserX, featureEnabled, reset] as const
}

export function usePCSXEnabledOnChain(chainId?: ChainId) {
Expand Down
14 changes: 12 additions & 2 deletions apps/web/src/state/user/smartRouter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { userSingleHopAtom } from '@pancakeswap/utils/user'
import { atom, useAtom, useAtomValue } from 'jotai'
import { useResetAtom } from 'jotai/utils'
import atomWithStorageWithErrorCatch from 'utils/atomWithStorageWithErrorCatch'
import { usePCSX } from 'hooks/usePCSX'
import { useCallback } from 'react'

const userUseStableSwapAtom = atomWithStorageWithErrorCatch<boolean>('pcs:useStableSwap', true)
const userUseV2SwapAtom = atomWithStorageWithErrorCatch<boolean>('pcs:useV2Swap', true)
Expand All @@ -9,7 +12,8 @@ const userUserSplitRouteAtom = atomWithStorageWithErrorCatch<boolean>('pcs:useSp
const userUseXAtom = atomWithStorageWithErrorCatch<boolean | undefined>('pcs:useX', undefined)

export function useUserXEnable() {
return useAtom(userUseXAtom)
const reset = useResetAtom(userUseXAtom)
return [...useAtom(userUseXAtom), reset] as const
}

export function useUserStableSwapEnable() {
Expand Down Expand Up @@ -56,5 +60,11 @@ const derivedRoutingSettingChangedAtom = atom(
)

export function useRoutingSettingChanged() {
return useAtom(derivedRoutingSettingChangedAtom)
const [enabled, _, featureEnabled, resetX] = usePCSX()
const [derivedRoutingSettingChanged, reset] = useAtom(derivedRoutingSettingChangedAtom)
const resetRoutingSettings = useCallback(() => {
reset()
resetX()
}, [reset, resetX])
return [derivedRoutingSettingChanged || enabled !== featureEnabled, resetRoutingSettings] as const
}

0 comments on commit c36d84d

Please sign in to comment.