Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(extension): [LW-9230] add multi delegation and dapp issues modal #1133

Merged
merged 6 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { useTranslation } from 'react-i18next';
import { BrowserViewSections } from '@lib/scripts/types';
import { useWalletActivities } from '@hooks/useWalletActivities';
import {
MULTIDELEGATION_DAPP_COMPATIBILITY_LS_KEY,
MULTIDELEGATION_FIRST_VISIT_LS_KEY,
MULTIDELEGATION_FIRST_VISIT_SINCE_PORTFOLIO_PERSISTENCE_LS_KEY,
STAKING_BROWSER_PREFERENCES_LS_KEY
Expand Down Expand Up @@ -86,6 +87,8 @@ export const MultiDelegationStakingPopup = (): JSX.Element => {
MULTIDELEGATION_FIRST_VISIT_LS_KEY,
true
);
const [multidelegationDAppCompatibility, { updateLocalStorage: setMultidelegationDAppCompatibility }] =
useLocalStorage(MULTIDELEGATION_DAPP_COMPATIBILITY_LS_KEY, true);
const [
multidelegationFirstVisitSincePortfolioPersistence,
{ updateLocalStorage: setMultidelegationFirstVisitSincePortfolioPersistence }
Expand All @@ -109,6 +112,8 @@ export const MultiDelegationStakingPopup = (): JSX.Element => {
setStakingBrowserPreferencesPersistence,
multidelegationFirstVisit,
triggerMultidelegationFirstVisit: () => setMultidelegationFirstVisit(false),
multidelegationDAppCompatibility,
triggerMultidelegationDAppCompatibility: () => setMultidelegationDAppCompatibility(false),
multidelegationFirstVisitSincePortfolioPersistence,
triggerMultidelegationFirstVisitSincePortfolioPersistence: () => {
setMultidelegationFirstVisit(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,7 @@ describe('Testing useWalletManager hook', () => {
'hideBalance',
'isForgotPasswordFlow',
'multidelegationFirstVisit',
'isMultiDelegationDAppCompatibilityModalVisible',
'multidelegationFirstVisitSincePortfolioPersistence'
]
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,7 @@ export const useWalletManager = (): UseWalletManager => {
'hideBalance',
'isForgotPasswordFlow',
'multidelegationFirstVisit',
'isMultiDelegationDAppCompatibilityModalVisible',
'multidelegationFirstVisitSincePortfolioPersistence'
];

Expand Down
1 change: 1 addition & 0 deletions apps/browser-extension-wallet/src/types/local-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export interface ILocalStorage {
analyticsStatus?: EnhancedAnalyticsOptInStatus;
isForgotPasswordFlow?: boolean;
multidelegationFirstVisit?: boolean;
isMultiDelegationDAppCompatibilityModalVisible?: boolean;
multidelegationFirstVisitSincePortfolioPersistence?: boolean;
unconfirmedTransactions: UnconfirmedTransaction[];
stakingBrowserPreferences: StakingBrowserPreferences;
Expand Down
1 change: 1 addition & 0 deletions apps/browser-extension-wallet/src/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,5 @@ export const COINGECKO_URL = 'https://www.coingecko.com';
export const MULTIDELEGATION_FIRST_VISIT_SINCE_PORTFOLIO_PERSISTENCE_LS_KEY =
'multidelegationFirstVisitSincePortfolioPersistence';
export const MULTIDELEGATION_FIRST_VISIT_LS_KEY = 'multidelegationFirstVisit';
export const MULTIDELEGATION_DAPP_COMPATIBILITY_LS_KEY = 'isMultiDelegationDAppCompatibilityModalVisible';
export const STAKING_BROWSER_PREFERENCES_LS_KEY = 'stakingBrowserPreferences';
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { useAnalyticsContext, useCurrencyStore, useExternalLinkOpener } from '@p
import { DEFAULT_STAKING_BROWSER_PREFERENCES, OutsideHandlesProvider } from '@lace/staking';
import { useBalances, useCustomSubmitApi, useFetchCoinPrice, useLocalStorage } from '@hooks';
import {
MULTIDELEGATION_DAPP_COMPATIBILITY_LS_KEY,
MULTIDELEGATION_FIRST_VISIT_LS_KEY,
MULTIDELEGATION_FIRST_VISIT_SINCE_PORTFOLIO_PERSISTENCE_LS_KEY,
STAKING_BROWSER_PREFERENCES_LS_KEY
Expand All @@ -32,6 +33,8 @@ export const StakingContainer = (): React.ReactElement => {
MULTIDELEGATION_FIRST_VISIT_LS_KEY,
true
);
const [multidelegationDAppCompatibility, { updateLocalStorage: setMultidelegationDAppCompatibility }] =
useLocalStorage(MULTIDELEGATION_DAPP_COMPATIBILITY_LS_KEY, true);
const [
multidelegationFirstVisitSincePortfolioPersistence,
{ updateLocalStorage: setMultidelegationFirstVisitSincePortfolioPersistence }
Expand Down Expand Up @@ -125,6 +128,8 @@ export const StakingContainer = (): React.ReactElement => {
compactNumber: compactNumberWithUnit,
multidelegationFirstVisit,
triggerMultidelegationFirstVisit: () => setMultidelegationFirstVisit(false),
multidelegationDAppCompatibility,
triggerMultidelegationDAppCompatibility: () => setMultidelegationDAppCompatibility(false),
multidelegationFirstVisitSincePortfolioPersistence,
triggerMultidelegationFirstVisitSincePortfolioPersistence: () => {
setMultidelegationFirstVisit(false);
Expand Down
2 changes: 2 additions & 0 deletions packages/staking/.storybook/StakingStorybookProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ const outsideHandlesMocks: OutsideHandlesContextValue = {
compactNumber: undefined,
multidelegationFirstVisit: undefined,
triggerMultidelegationFirstVisit: undefined,
multidelegationDAppCompatibility: undefined,
triggerMultidelegationDAppCompatibility: undefined,
multidelegationFirstVisitSincePortfolioPersistence: undefined,
triggerMultidelegationFirstVisitSincePortfolioPersistence: undefined,
walletAddress: undefined,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { PostHogAction } from '@lace/common';
import { Table } from '@lace/ui';
import React from 'react';
import { MultidelegationDAppCompatibilityModal } from 'features/modals/MultidelegationDAppCompatibilityModal';
import React, { useCallback, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { useOutsideHandles } from '../../outside-handles-provider';
import { MAX_POOLS_COUNT, StakePoolDetails, isPoolSelectedSelector, useDelegationPortfolioStore } from '../../store';

import { config } from './config';

export const StakePoolsListRow = ({ stakePool, hexId, id, ...data }: StakePoolDetails): React.ReactElement => {
Expand All @@ -16,33 +18,57 @@ export const StakePoolsListRow = ({ stakePool, hexId, id, ...data }: StakePoolDe
selectionsFull: store.selectedPortfolio.length === MAX_POOLS_COUNT,
}));

const { multidelegationDAppCompatibility, triggerMultidelegationDAppCompatibility } = useOutsideHandles();
const [showDAppCompatibilityModal, setShowDAppCompatibilityModal] = useState(false);

const onClick = () => {
portfolioMutators.executeCommand({ data: stakePool, type: 'ShowPoolDetailsFromList' });
analytics.sendEventToPostHog(PostHogAction.StakingBrowsePoolsStakePoolDetailClick);
};

const onSelect = () => {
const onPoolSelect = useCallback(() => {
if (poolAlreadySelected) {
portfolioMutators.executeCommand({ data: hexId, type: 'UnselectPoolFromList' });
analytics.sendEventToPostHog(PostHogAction.StakingBrowsePoolsUnselectClick);
} else {
portfolioMutators.executeCommand({ data: [stakePool], type: 'SelectPoolFromList' });
analytics.sendEventToPostHog(PostHogAction.StakingBrowsePoolsStakeClick);
}
};
}, [analytics, hexId, poolAlreadySelected, portfolioMutators, stakePool]);

const onDAppCompatibilityConfirm = useCallback(() => {
triggerMultidelegationDAppCompatibility();
onPoolSelect();
}, [onPoolSelect, triggerMultidelegationDAppCompatibility]);

const onSelect = useCallback(() => {
if (multidelegationDAppCompatibility && !poolAlreadySelected) {
setShowDAppCompatibilityModal(true);
} else {
onPoolSelect();
}
}, [multidelegationDAppCompatibility, onPoolSelect, poolAlreadySelected]);

return (
<Table.Row<Omit<StakePoolDetails, 'stakePool' | 'hexId' | 'id'>>
columns={config.columns}
cellRenderers={config.renderer}
data={data}
selected={poolAlreadySelected}
onClick={onClick}
selectionDisabledMessage={t('browsePools.tooltips.maxNumberPoolsSelected')}
dataTestId="stake-pool"
withSelection
keyProp={id}
{...((!selectionsFull || poolAlreadySelected) && { onSelect })}
/>
<>
<Table.Row<Omit<StakePoolDetails, 'stakePool' | 'hexId' | 'id'>>
columns={config.columns}
cellRenderers={config.renderer}
data={data}
selected={poolAlreadySelected}
onClick={onClick}
selectionDisabledMessage={t('browsePools.tooltips.maxNumberPoolsSelected')}
dataTestId="stake-pool"
withSelection
keyProp={id}
{...((!selectionsFull || poolAlreadySelected) && { onSelect })}
/>
{showDAppCompatibilityModal && (
<MultidelegationDAppCompatibilityModal
visible={multidelegationDAppCompatibility}
onConfirm={onDAppCompatibilityConfirm}
/>
)}
</>
);
};
2 changes: 1 addition & 1 deletion packages/staking/src/features/Drawer/Drawer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export const Drawer = ({
const contentsMap = useMemo(
(): Record<DrawerStep, React.ReactElement> => ({
[DrawerDefaultStep.PoolDetails]: <StakePoolDetail popupView={popupView} />,
[DrawerManagementStep.Preferences]: <StepPreferencesContent />,
[DrawerManagementStep.Preferences]: <StepPreferencesContent popupView={popupView} />,
[DrawerManagementStep.Confirmation]: <StakePoolConfirmationContent />,
[DrawerManagementStep.Sign]: <SignConfirmation />,
[DrawerManagementStep.Success]: <TransactionSuccess />,
Expand Down
80 changes: 55 additions & 25 deletions packages/staking/src/features/Drawer/StakePoolDetail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import { Button, Flex } from '@lace/ui';
import cn from 'classnames';
import { StakePoolCardProgressBar } from 'features/BrowsePools';
import { isOversaturated } from 'features/BrowsePools/utils';
import { MultidelegationDAppCompatibilityModal } from 'features/modals/MultidelegationDAppCompatibilityModal';
import { TFunction } from 'i18next';
import React, { useCallback, useEffect, useMemo } from 'react';
import React, { useCallback, useEffect, useMemo, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { useOutsideHandles } from '../outside-handles-provider';
import {
Expand Down Expand Up @@ -254,6 +255,7 @@ const makeSelector =
poolInCurrentPortfolio,
poolSelected,
selectionsEmpty: selectedPortfolio.length === 0,
userAlreadyMultidelegated: currentPortfolio.length > 1,
};
};

Expand Down Expand Up @@ -312,15 +314,22 @@ const makeActionButtons = (

export const StakePoolDetailFooter = ({ popupView }: StakePoolDetailFooterProps): React.ReactElement => {
const { t } = useTranslation();
const { analytics } = useOutsideHandles();
const { analytics, multidelegationDAppCompatibility, triggerMultidelegationDAppCompatibility } = useOutsideHandles();
const { walletStoreWalletType } = useOutsideHandles();
const [showDAppCompatibilityModal, setShowDAppCompatibilityModal] = useState(false);
const { openPoolDetails, portfolioMutators, viewedStakePool } = useDelegationPortfolioStore((store) => ({
openPoolDetails: stakePoolDetailsSelector(store),
portfolioMutators: store.mutators,
viewedStakePool: store.viewedStakePool,
}));
const { ableToSelect, ableToStakeOnlyOnThisPool, selectionsEmpty, poolInCurrentPortfolio, poolSelected } =
useDelegationPortfolioStore(makeSelector(openPoolDetails));
const {
ableToSelect,
ableToStakeOnlyOnThisPool,
selectionsEmpty,
poolInCurrentPortfolio,
poolSelected,
userAlreadyMultidelegated,
} = useDelegationPortfolioStore(makeSelector(openPoolDetails));

const isInMemory = walletStoreWalletType === WalletType.InMemory;

Expand All @@ -329,16 +338,7 @@ export const StakePoolDetailFooter = ({ popupView }: StakePoolDetailFooterProps)
portfolioMutators.executeCommand({ type: 'BeginSingleStaking' });
}, [analytics, portfolioMutators]);

useEffect(() => {
if (isInMemory) return;
if (popupView) return;
const hasPersistedHwStakepool = !!localStorage.getItem('TEMP_POOLID');
if (!hasPersistedHwStakepool) return;
onStakeOnThisPool();
localStorage.removeItem('TEMP_POOLID');
}, [isInMemory, onStakeOnThisPool, popupView]);

const onSelectClick = useCallback(() => {
const selectPoolFromDetails = useCallback(() => {
if (!viewedStakePool) return;
analytics.sendEventToPostHog(PostHogAction.StakingBrowsePoolsStakePoolDetailAddStakingPoolClick);
portfolioMutators.executeCommand({
Expand All @@ -347,6 +347,27 @@ export const StakePoolDetailFooter = ({ popupView }: StakePoolDetailFooterProps)
});
}, [viewedStakePool, portfolioMutators, analytics]);

const onSelectClick = useCallback(() => {
if (!userAlreadyMultidelegated && multidelegationDAppCompatibility) {
setShowDAppCompatibilityModal(true);
} else {
selectPoolFromDetails();
}
}, [multidelegationDAppCompatibility, selectPoolFromDetails, userAlreadyMultidelegated]);

const onDAppCompatibilityConfirm = useCallback(() => {
triggerMultidelegationDAppCompatibility();
selectPoolFromDetails();
}, [selectPoolFromDetails, triggerMultidelegationDAppCompatibility]);

useEffect(() => {
if (isInMemory || popupView) return;
const hasPersistedHwStakepool = !!localStorage.getItem('TEMP_POOLID');
if (!hasPersistedHwStakepool) return;
onStakeOnThisPool();
localStorage.removeItem('TEMP_POOLID');
}, [isInMemory, onStakeOnThisPool, popupView]);

const onUnselectClick = useCallback(() => {
if (!viewedStakePool) return;
analytics.sendEventToPostHog(PostHogAction.StakingBrowsePoolsStakePoolDetailUnselectPoolClick);
Expand Down Expand Up @@ -392,18 +413,27 @@ export const StakePoolDetailFooter = ({ popupView }: StakePoolDetailFooterProps)
const [callToActionButton, ...secondaryButtons] = actionButtons;

return (
<Flex flexDirection="column" alignItems="stretch" gap="$16">
{callToActionButton && (
<Button.CallToAction
label={callToActionButton.label}
data-testid={callToActionButton.dataTestId}
onClick={callToActionButton.callback}
w="$fill"
<>
<Flex flexDirection="column" alignItems="stretch" gap="$16">
{callToActionButton && (
<Button.CallToAction
label={callToActionButton.label}
data-testid={callToActionButton.dataTestId}
onClick={callToActionButton.callback}
w="$fill"
/>
)}
{secondaryButtons.map(({ callback, dataTestId, label }) => (
<Button.Secondary key={dataTestId} onClick={callback} data-testid={dataTestId} label={label} w="$fill" />
))}
</Flex>
{showDAppCompatibilityModal && (
<MultidelegationDAppCompatibilityModal
visible={multidelegationDAppCompatibility}
onConfirm={onDAppCompatibilityConfirm}
popupView={popupView}
/>
)}
{secondaryButtons.map(({ callback, dataTestId, label }) => (
<Button.Secondary key={dataTestId} onClick={callback} data-testid={dataTestId} label={label} w="$fill" />
))}
</Flex>
</>
);
};
Loading
Loading