diff --git a/apps/web/src/hooks/usePCSX.ts b/apps/web/src/hooks/usePCSX.ts index 91f853e625182..1ef6a34f902d7 100644 --- a/apps/web/src/hooks/usePCSX.ts +++ b/apps/web/src/hooks/usePCSX.ts @@ -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() { @@ -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) { diff --git a/apps/web/src/state/user/smartRouter.ts b/apps/web/src/state/user/smartRouter.ts index 90465fd1db335..6dfffb218711f 100644 --- a/apps/web/src/state/user/smartRouter.ts +++ b/apps/web/src/state/user/smartRouter.ts @@ -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('pcs:useStableSwap', true) const userUseV2SwapAtom = atomWithStorageWithErrorCatch('pcs:useV2Swap', true) @@ -9,7 +12,8 @@ const userUserSplitRouteAtom = atomWithStorageWithErrorCatch('pcs:useSp const userUseXAtom = atomWithStorageWithErrorCatch('pcs:useX', undefined) export function useUserXEnable() { - return useAtom(userUseXAtom) + const reset = useResetAtom(userUseXAtom) + return [...useAtom(userUseXAtom), reset] as const } export function useUserStableSwapEnable() { @@ -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 }