Skip to content

Commit

Permalink
Merge pull request #204 from lidofinance/feature/si-1068-proposal-mod…
Browse files Browse the repository at this point in the history
…ify-behavior-on-faq-click-following

FAQ click-following navigation
  • Loading branch information
jake4take authored Jan 29, 2024
2 parents 5a6030f + 31e068f commit b668bb2
Show file tree
Hide file tree
Showing 12 changed files with 194 additions and 43 deletions.
10 changes: 7 additions & 3 deletions features/withdrawals/request/form/bunker-info.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import Link from 'next/link';

import { useInpageNavigation } from 'providers/inpage-navigation';
import { InfoBoxStyled } from 'features/withdrawals/shared';

export const BunkerInfo = () => {
const { navigateInpageAnchor } = useInpageNavigation();

return (
<InfoBoxStyled>
Lido protocol is in &quot;Bunker mode&quot;. The withdrawal requests are
slowed down until the consequences of the incident that caused
&quot;Bunker mode&quot; are not resolved. For more details,{' '}
<Link href="#bunkerModeScenarios">see here</Link>.
<a href="#bunkerModeScenarios" onClick={navigateInpageAnchor}>
see here
</a>
.
</InfoBoxStyled>
);
};
29 changes: 15 additions & 14 deletions features/withdrawals/request/form/options/lido-option.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import Link from 'next/link';
import { useWatch } from 'react-hook-form';
import { formatEther } from '@ethersproject/units';

import { Tooltip, Question } from '@lidofinance/lido-ui';
import { TOKENS } from '@lido-sdk/constants';

import { useEthAmountByStethWsteth } from 'features/withdrawals/hooks';
import { useInpageNavigation } from 'providers/inpage-navigation';
import { RequestFormInputType } from 'features/withdrawals/request/request-form-context';

import {
Expand All @@ -21,6 +21,8 @@ import {
} from './styles';

const TooltipWithdrawalAmount = () => {
const { navigateInpageAnchor } = useInpageNavigation();

return (
<Tooltip
data-testid="lidoOptionToolTip"
Expand All @@ -29,19 +31,18 @@ const TooltipWithdrawalAmount = () => {
<>
The final amount of claimable ETH can differ
<br /> For more info, please read{' '}
<Link href="#amountDifferentFromRequested">
<a
data-testid="lidoOptionToolTipFAQ"
aria-hidden="true"
onClick={() =>
trackMatomoEvent(
MATOMO_CLICK_EVENTS_TYPES.withdrawalFAQtooltipEthAmount,
)
}
>
FAQ
</a>
</Link>
<a
data-testid="lidoOptionToolTipFAQ"
href="#amountDifferentFromRequested"
onClick={(e) => {
trackMatomoEvent(
MATOMO_CLICK_EVENTS_TYPES.withdrawalFAQtooltipEthAmount,
);
navigateInpageAnchor(e);
}}
>
FAQ
</a>
</>
}
>
Expand Down
28 changes: 14 additions & 14 deletions features/withdrawals/request/wallet/wallet-queue-tooltip.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Question, Tooltip } from '@lidofinance/lido-ui';
import Link from 'next/link';

import { FormatToken } from 'shared/formatters';
import { useWaitingTime } from 'features/withdrawals/hooks';
Expand All @@ -10,10 +9,12 @@ import {
} from 'config/trackMatomoEvent';
import { QueueInfoStyled, DataTableRowStyled } from './styles';
import { useRequestFormData } from '../request-form-context';
import { useInpageNavigation } from 'providers/inpage-navigation';

export const WalletQueueTooltip = () => {
const waitingTime = useWaitingTime('');
const { unfinalizedStETH } = useRequestFormData();
const { navigateInpageAnchor } = useInpageNavigation();

const queueInfo = (
<QueueInfoStyled>
Expand All @@ -38,19 +39,18 @@ export const WalletQueueTooltip = () => {
<>
The withdrawal request time depends on the mode, overall amount of stETH
in queue and{' '}
<Link href="#withdrawalsPeriod">
<a
aria-hidden="true"
data-testid="otherFactorsLink"
onClick={() =>
trackMatomoEvent(
MATOMO_CLICK_EVENTS_TYPES.withdrawalOtherFactorsTooltipMode,
)
}
>
other factors
</a>
</Link>
<a
href="#withdrawalsPeriod"
data-testid="otherFactorsLink"
onClick={(e) => {
trackMatomoEvent(
MATOMO_CLICK_EVENTS_TYPES.withdrawalOtherFactorsTooltipMode,
);
navigateInpageAnchor(e);
}}
>
other factors
</a>
.{queueInfo}
</>
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Accordion } from '@lidofinance/lido-ui';
import { AccordionNavigatable } from 'shared/components/accordion-navigatable';

export const BunkerModeReasons: React.FC = () => {
return (
<Accordion
<AccordionNavigatable
summary="What scenarios can cause Bunker mode?"
id="bunkerModeScenarios"
>
Expand All @@ -22,6 +22,6 @@ export const BunkerModeReasons: React.FC = () => {
the end of&nbsp;it.
</li>
</ol>
</Accordion>
</AccordionNavigatable>
);
};
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Accordion } from '@lidofinance/lido-ui';
import { AccordionNavigatable } from 'shared/components/accordion-navigatable';

type ClaimableAmountDifferenceProps = {
title: string;
Expand All @@ -8,12 +8,12 @@ export const ClaimableAmountDifference: React.FC<
ClaimableAmountDifferenceProps
> = ({ title }) => {
return (
<Accordion summary={title} id="amountDifferentFromRequested">
<AccordionNavigatable summary={title} id="amountDifferentFromRequested">
<p>
The amount you can claim may differ from your initial request due to a
slashing occurrence and penalties. For these reasons, the total
claimable reward amount could be reduced.
</p>
</Accordion>
</AccordionNavigatable>
);
};
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Accordion } from '@lidofinance/lido-ui';
import { AccordionNavigatable } from 'shared/components/accordion-navigatable';

export const WithdrawalPeriodCircumstances: React.FC = () => {
return (
<Accordion
<AccordionNavigatable
summary="What are the factors affecting the withdrawal time?"
id="withdrawalsPeriod"
>
Expand All @@ -12,6 +12,6 @@ export const WithdrawalPeriodCircumstances: React.FC = () => {
<li>Exit queue on the Beacon chain.</li>
<li>Demand for staking and unstaking.</li>
</ul>
</Accordion>
</AccordionNavigatable>
);
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"@lidofinance/api-rpc": "^0.28.0",
"@lidofinance/eth-api-providers": "^0.28.0",
"@lidofinance/eth-providers": "^0.28.0",
"@lidofinance/lido-ui": "^3.18.2",
"@lidofinance/lido-ui": "^3.20.1",
"@lidofinance/next-api-wrapper": "^0.28.0",
"@lidofinance/next-ip-rate-limit": "^0.28.0",
"@lidofinance/next-pages": "^0.28.0",
Expand Down
5 changes: 4 additions & 1 deletion providers/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { GlobalStyle } from 'styles';
import { AppFlagProvider } from './app-flag';
import { ClientConfigProvider } from './client-config';
import { IPFSInfoBoxStatusesProvider } from './ipfs-info-box-statuses';
import { InpageNavigationProvider } from './inpage-navigation';
import ModalProvider from './modals';
import Web3Provider from './web3';

Expand All @@ -17,7 +18,9 @@ export const Providers: FC<PropsWithChildren> = ({ children }) => (
<GlobalStyle />
<Web3Provider>
<IPFSInfoBoxStatusesProvider>
<ModalProvider>{children}</ModalProvider>
<InpageNavigationProvider>
<ModalProvider>{children}</ModalProvider>
</InpageNavigationProvider>
</IPFSInfoBoxStatusesProvider>
</Web3Provider>
</CookieThemeProvider>
Expand Down
103 changes: 103 additions & 0 deletions providers/inpage-navigation.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import {
FC,
PropsWithChildren,
createContext,
useContext,
useState,
useMemo,
useCallback,
useEffect,
} from 'react';
import invariant from 'tiny-invariant';
import { dynamics } from 'config';
import { useRouter } from 'next/router';

export type InpageNavigationContextValue = {
hashNav: string;
navigateInpageAnchor: (e: React.MouseEvent<HTMLAnchorElement>) => void;
resetInpageAnchor: () => void;
resetSpecificAnchor: (hash: string) => void;
};

const InpageNavigationContext =
createContext<InpageNavigationContextValue | null>(null);
InpageNavigationContext.displayName = 'InpageNavigationContext';

// IPFS-compatible hash-based in-page navigation
export const InpageNavigationProvider: FC<PropsWithChildren> = ({
children,
}) => {
const { asPath } = useRouter();
const [hashNav, setHash] = useState('');

useEffect(() => {
if (dynamics.ipfsMode) return; // Hash is reserved in ipfs mode, ignored here
const hash = asPath.split('#')[1];
setHash(hash);
}, [asPath]);

const navigateInpageAnchor = useCallback(
(e: React.MouseEvent<HTMLAnchorElement>) => {
const href = e.currentTarget.getAttribute('href');
if (!href) return;
const hash = href.split('#')[1];
e.preventDefault();

// Remember the hash
setHash(hash);

// Perform animated scroll
document.getElementById(hash)?.scrollIntoView({
behavior: 'smooth',
});

// Change the hash for non-ipfs ui, without scrolling the page
// We have done animated scroll already on next step
if (!dynamics.ipfsMode) {
history.pushState({}, '', `#${hash}`);
}
},
[],
);

const resetInpageAnchor = useCallback(() => {
setHash('');
if (!dynamics.ipfsMode) {
const hashTrimmed = asPath.split('#')[0];
history.pushState({}, '', hashTrimmed);
}
}, [asPath]);

const resetSpecificAnchor = useCallback(
(hash: string) => {
if (hash !== hashNav) return;
resetInpageAnchor();
},
[resetInpageAnchor, hashNav],
);

const value = useMemo(
() => ({
hashNav,
navigateInpageAnchor,
resetInpageAnchor,
resetSpecificAnchor,
}),
[hashNav, navigateInpageAnchor, resetInpageAnchor, resetSpecificAnchor],
);

return (
<InpageNavigationContext.Provider value={value}>
{children}
</InpageNavigationContext.Provider>
);
};

export const useInpageNavigation = () => {
const value = useContext(InpageNavigationContext);
invariant(
value !== null,
'useInpageNavigation was used used outside of InpageNavigationProvider',
);
return value;
};
25 changes: 25 additions & 0 deletions shared/components/accordion-navigatable/accordion-navigatable.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Accordion } from '@lidofinance/lido-ui';
import { useInpageNavigation } from 'providers/inpage-navigation';
import { useCallback } from 'react';

type AccordionNavigatableProps = React.ComponentProps<typeof Accordion> & {
id: string;
};

export const AccordionNavigatable = (props: AccordionNavigatableProps) => {
const { onCollapse, id } = props;
const { hashNav, resetSpecificAnchor } = useInpageNavigation();

const handleCollapse = useCallback(() => {
resetSpecificAnchor(id);
onCollapse?.();
}, [resetSpecificAnchor, id, onCollapse]);

return (
<Accordion
defaultExpanded={hashNav === id}
{...props}
onCollapse={handleCollapse}
/>
);
};
1 change: 1 addition & 0 deletions shared/components/accordion-navigatable/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './accordion-navigatable';
16 changes: 15 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2224,7 +2224,7 @@
resolved "https://registry.yarnpkg.com/@lidofinance/eth-providers/-/eth-providers-0.28.0.tgz#069dfcd66f97418298646abd2742e8e997c6fbbb"
integrity sha512-oH2nU7jVPkn1mii56vp3gBFZNFR/LysxiKBUwV2freFoizQHNLeWWjU1b8a+l1I020LzbxHcatOkV6z9ur5YfA==

"@lidofinance/lido-ui@^3.18.0", "@lidofinance/lido-ui@^3.18.2":
"@lidofinance/lido-ui@^3.18.0":
version "3.18.2"
resolved "https://registry.yarnpkg.com/@lidofinance/lido-ui/-/lido-ui-3.18.2.tgz#7eaa79270b9f23129d776ebe2a083f4329fe9ec6"
integrity sha512-/MXmkHmliyQ5S7/TQW87XMYLd7May8R+rZ7JHbiZzaLEzUwixlLyFvWpmtX2CKJ4JL13KKWke/06gbNG/8wC5A==
Expand All @@ -2238,6 +2238,20 @@
ua-parser-js "^1.0.35"
use-callback-ref "1.2.5"

"@lidofinance/lido-ui@^3.20.1":
version "3.20.1"
resolved "https://registry.yarnpkg.com/@lidofinance/lido-ui/-/lido-ui-3.20.1.tgz#43034832a0eb1c1efd7e7f526300908516c3df11"
integrity sha512-ZSv3JBWbX28/IZTXuu9Na1sTtNymXE6izEVAmSBK/LShQny1YbkXJAfkkFYcHAhm+bY04kHWGfXrjNNXPgI9hA==
dependencies:
"@styled-system/should-forward-prop" "5.1.5"
react-collapsed "3.0.2"
react-jazzicon "^1.0.4"
react-toastify "7.0.4"
react-transition-group "4"
styled-system "5.1.5"
ua-parser-js "^1.0.35"
use-callback-ref "1.2.5"

"@lidofinance/next-api-wrapper@^0.28.0", "@lidofinance/next-api-wrapper@~0.28.0":
version "0.28.0"
resolved "https://registry.yarnpkg.com/@lidofinance/next-api-wrapper/-/next-api-wrapper-0.28.0.tgz#76fc32dafdbb1188d0a0ca5afd23bdf13e14d35d"
Expand Down

0 comments on commit b668bb2

Please sign in to comment.