From b2bd1b9a035f8f8972a7835390da4d6a006a53a5 Mon Sep 17 00:00:00 2001 From: Oskar <43062492+oskarvu@users.noreply.github.com> Date: Tue, 17 Dec 2024 14:32:39 +0700 Subject: [PATCH] Scroll to actions after hitting borrow button (#493) * Scroll to actions after hitting borrow button * Replace useEffect with ref callback to focus on actions panel * Move function declaration --- .../easy-borrow/EasyBorrowContainer.tsx | 4 ++-- .../easy-borrow/components/EasyBorrowPanel.tsx | 4 ++-- .../features/easy-borrow/logic/useEasyBorrow.ts | 17 +++++++++-------- .../views/EasyBorrowView.stories.tsx | 5 ++--- .../easy-borrow/views/EasyBorrowView.tsx | 2 +- 5 files changed, 16 insertions(+), 16 deletions(-) diff --git a/packages/app/src/features/easy-borrow/EasyBorrowContainer.tsx b/packages/app/src/features/easy-borrow/EasyBorrowContainer.tsx index 3a33050af..1bd0c4558 100644 --- a/packages/app/src/features/easy-borrow/EasyBorrowContainer.tsx +++ b/packages/app/src/features/easy-borrow/EasyBorrowContainer.tsx @@ -25,7 +25,7 @@ function EasyBorrowContainer() { borrowDetails, guestMode, openSandboxModal, - healthFactorPanelRef, + focusOnActionsPanel, riskAcknowledgement, actionsContext, } = useEasyBorrow() @@ -53,7 +53,7 @@ function EasyBorrowContainer() { guestMode={guestMode} openConnectModal={openConnectModal} openSandboxModal={openSandboxModal} - healthFactorPanelRef={healthFactorPanelRef} + focusOnActionsPanel={focusOnActionsPanel} riskAcknowledgement={riskAcknowledgement} actionsContext={actionsContext} /> diff --git a/packages/app/src/features/easy-borrow/components/EasyBorrowPanel.tsx b/packages/app/src/features/easy-borrow/components/EasyBorrowPanel.tsx index 2b80d3295..ca035e95d 100644 --- a/packages/app/src/features/easy-borrow/components/EasyBorrowPanel.tsx +++ b/packages/app/src/features/easy-borrow/components/EasyBorrowPanel.tsx @@ -35,7 +35,7 @@ export interface EasyBorrowPanelProps { guestMode: boolean openConnectModal: () => void openSandboxModal: () => void - healthFactorPanelRef: React.RefObject + focusOnActionsPanel: (node: HTMLDivElement | null) => void actionsContext: InjectedActionsContext } @@ -75,7 +75,7 @@ export function EasyBorrowPanel(props: EasyBorrowPanelProps) { )} )} - + void - healthFactorPanelRef: React.RefObject + focusOnActionsPanel: (node: HTMLDivElement | null) => void actionsContext: InjectedActionsContext } @@ -85,7 +85,6 @@ export function useEasyBorrow(): UseEasyBorrowResults { const upgradeOptions = useUpgradeOptions({ chainId, daiSymbol }) const [pageStatus, setPageStatus] = useState('form') - const healthFactorPanelRef = useRef(null) const userPositions = imputeNativeAsset(marketInfo, nativeAssetInfo) const alreadyDeposited = useConditionalFreeze( @@ -197,11 +196,13 @@ export function useEasyBorrow(): UseEasyBorrowResults { }, [account.chainId], ) - useEffect(() => { - if (pageStatus === 'confirmation') { - healthFactorPanelRef.current?.scrollIntoView({ behavior: 'smooth', block: 'start' }) + + const focusOnActionsPanel = useCallback((node: HTMLDivElement | null) => { + if (!node) { + return } - }, [pageStatus]) + node.scrollIntoView({ behavior: 'smooth', block: 'start' }) + }, []) function openSandboxModal(): void { openDialog(sandboxDialogConfig, { mode: 'ephemeral' } as const) @@ -243,7 +244,7 @@ export function useEasyBorrow(): UseEasyBorrowResults { borrowDetails, guestMode, openSandboxModal, - healthFactorPanelRef, + focusOnActionsPanel, riskAcknowledgement, actionsContext: { marketInfo, diff --git a/packages/app/src/features/easy-borrow/views/EasyBorrowView.stories.tsx b/packages/app/src/features/easy-borrow/views/EasyBorrowView.stories.tsx index 2d885fd50..e19074a0b 100644 --- a/packages/app/src/features/easy-borrow/views/EasyBorrowView.stories.tsx +++ b/packages/app/src/features/easy-borrow/views/EasyBorrowView.stories.tsx @@ -10,7 +10,6 @@ import { tokens } from '@sb/tokens' import { getMobileStory, getTabletStory } from '@sb/viewports' import { Meta, StoryObj } from '@storybook/react' import BigNumber from 'bignumber.js' -import { useRef } from 'react' import { useForm } from 'react-hook-form' import { withRouter } from 'storybook-addon-remix-react-router' import { EasyBorrowFormSchema } from '../logic/form/validation' @@ -85,6 +84,7 @@ function EasyBorrowViewStory(props: EasyBorrowViewStoryProps) { const setDesiredLoanToValue = () => {} const openConnectModal = () => {} const openSandboxModal = () => {} + const focusOnActionsPanel = () => {} /* eslint-enable func-style */ const pageStatus = { @@ -94,7 +94,6 @@ function EasyBorrowViewStory(props: EasyBorrowViewStoryProps) { goToSuccessScreen: () => {}, submitForm: () => {}, } - const healthFactorPanelRef = useRef(null) return ( diff --git a/packages/app/src/features/easy-borrow/views/EasyBorrowView.tsx b/packages/app/src/features/easy-borrow/views/EasyBorrowView.tsx index 6358ebcf7..e296136d0 100644 --- a/packages/app/src/features/easy-borrow/views/EasyBorrowView.tsx +++ b/packages/app/src/features/easy-borrow/views/EasyBorrowView.tsx @@ -28,7 +28,7 @@ export interface EasyBorrowViewProps { guestMode: boolean openConnectModal: () => void openSandboxModal: () => void - healthFactorPanelRef: React.RefObject + focusOnActionsPanel: (node: HTMLDivElement | null) => void actionsContext: InjectedActionsContext }