Skip to content

Commit

Permalink
feat(staking): save user preferences for staking feature
Browse files Browse the repository at this point in the history
  • Loading branch information
vetalcore committed Feb 4, 2024
1 parent 789c550 commit 7f19131
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ import { BrowserViewSections } from '@lib/scripts/types';
import { useWalletActivities } from '@hooks/useWalletActivities';
import {
MULTIDELEGATION_FIRST_VISIT_LS_KEY,
MULTIDELEGATION_FIRST_VISIT_SINCE_PORTFOLIO_PERSISTENCE_LS_KEY
MULTIDELEGATION_FIRST_VISIT_SINCE_PORTFOLIO_PERSISTENCE_LS_KEY,
DELEGATION_PREFERENCES_LS_KEY
} from '@utils/constants';

export const MultiDelegationStakingPopup = (): JSX.Element => {
Expand Down Expand Up @@ -85,6 +86,10 @@ export const MultiDelegationStakingPopup = (): JSX.Element => {
multidelegationFirstVisitSincePortfolioPersistence,
{ updateLocalStorage: setMultidelegationFirstVisitSincePortfolioPersistence }
] = useLocalStorage(MULTIDELEGATION_FIRST_VISIT_SINCE_PORTFOLIO_PERSISTENCE_LS_KEY, true);

const [delegationPreferencePersistence, { updateLocalStorage: setDelegationPreferencePersistence }] =
useLocalStorage(DELEGATION_PREFERENCES_LS_KEY);

const walletAddress = walletInfo.addresses?.[0].address?.toString();
const analytics = useAnalyticsContext();

Expand All @@ -96,6 +101,8 @@ export const MultiDelegationStakingPopup = (): JSX.Element => {
<OutsideHandlesProvider
{...{
analytics,
delegationPreferencePersistence,
setDelegationPreferencePersistence,
multidelegationFirstVisit,
triggerMultidelegationFirstVisit: () => setMultidelegationFirstVisit(false),
multidelegationFirstVisitSincePortfolioPersistence,
Expand Down
2 changes: 2 additions & 0 deletions apps/browser-extension-wallet/src/types/local-storage.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Wallet } from '@lace/cardano';
import { EnhancedAnalyticsOptInStatus, TxCreationType } from '../providers/AnalyticsProvider/analyticsTracker/types';
import { DelegationPreferences } from '@lace/staking';

export interface WalletStorage {
name: string;
Expand Down Expand Up @@ -52,4 +53,5 @@ export interface ILocalStorage {
multidelegationFirstVisit?: boolean;
multidelegationFirstVisitSincePortfolioPersistence?: boolean;
unconfirmedTransactions: UnconfirmedTransaction[];
delegationPreferences: DelegationPreferences;
}
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,3 +107,4 @@ 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 DELEGATION_PREFERENCES_LS_KEY = 'delegationPreferences';
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { useWalletStore } from '@stores';
import { compactNumberWithUnit } from '@utils/format-number';
import { useWalletActivities } from '@hooks/useWalletActivities';
import {
DELEGATION_PREFERENCES_LS_KEY,
MULTIDELEGATION_FIRST_VISIT_LS_KEY,
MULTIDELEGATION_FIRST_VISIT_SINCE_PORTFOLIO_PERSISTENCE_LS_KEY
} from '@utils/constants';
Expand Down Expand Up @@ -85,6 +86,10 @@ export const MultiDelegationStaking = (): JSX.Element => {
multidelegationFirstVisitSincePortfolioPersistence,
{ updateLocalStorage: setMultidelegationFirstVisitSincePortfolioPersistence }
] = useLocalStorage(MULTIDELEGATION_FIRST_VISIT_SINCE_PORTFOLIO_PERSISTENCE_LS_KEY, true);

const [delegationPreferencePersistence, { updateLocalStorage: setDelegationPreferencePersistence }] =
useLocalStorage(DELEGATION_PREFERENCES_LS_KEY);

const walletAddress = walletInfo.addresses?.[0].address?.toString();
const analytics = useAnalyticsContext();

Expand All @@ -97,6 +102,8 @@ export const MultiDelegationStaking = (): JSX.Element => {
<OutsideHandlesProvider
{...{
analytics,
delegationPreferencePersistence,
setDelegationPreferencePersistence,
backgroundServiceAPIContextSetWalletPassword: setWalletPassword,
balancesBalance: balance,
delegationStoreSetDelegationTxBuilder: setDelegationTxBuilder,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,6 @@ const searchDebounce = 300;
const defaultFetchLimit = 100;

export const StakePoolsTable = ({ scrollableTargetId }: StakePoolsTableProps) => {
const { t } = useTranslation();
const [searchValue, setSearchValue] = useState<string>('');
const [isSearching, setIsSearching] = useState<boolean>(true);
const [isLoadingList, setIsLoadingList] = useState<boolean>(true);
const [sort, setSort] = useState<StakePoolSortOptions>(DEFAULT_SORT_OPTIONS);
const [stakePools, setStakePools] = useState<Wallet.StakePoolSearchResults['pageResults']>([]);
const [skip, setSkip] = useState<number>(0);
const selectedPortfolioStakePools = useDelegationPortfolioStore((store) =>
store.selectedPortfolio.map(({ stakePool }) => stakePool)
);
const {
currentChain,
walletStoreStakePoolSearchResults: {
Expand All @@ -47,7 +37,21 @@ export const StakePoolsTable = ({ scrollableTargetId }: StakePoolsTableProps) =>
walletStoreStakePoolSearchResultsStatus,
walletStoreFetchStakePools: fetchStakePools,
analytics,
delegationPreferencePersistence,
setDelegationPreferencePersistence,
} = useOutsideHandles();
const { t } = useTranslation();
const [searchValue, setSearchValue] = useState<string>(delegationPreferencePersistence?.searchQuery || '');
const [isSearching, setIsSearching] = useState<boolean>(true);
const [isLoadingList, setIsLoadingList] = useState<boolean>(true);
const [sort, setSort] = useState<StakePoolSortOptions>(
delegationPreferencePersistence?.sortOptions || DEFAULT_SORT_OPTIONS
);
const [stakePools, setStakePools] = useState<Wallet.StakePoolSearchResults['pageResults']>([]);
const [skip, setSkip] = useState<number>(0);
const selectedPortfolioStakePools = useDelegationPortfolioStore((store) =>
store.selectedPortfolio.map(({ stakePool }) => stakePool)
);

const fetchingPools = walletStoreStakePoolSearchResultsStatus === StateStatus.LOADING;

Expand All @@ -68,7 +72,19 @@ export const StakePoolsTable = ({ scrollableTargetId }: StakePoolsTableProps) =>
// Fetch pools on mount, network switching, searchValue change and sort change
setIsLoadingList(true);
debouncedSearch({ searchString: searchValue, sort });
}, [currentChain, searchValue, sort, debouncedSearch]);
setDelegationPreferencePersistence({
...delegationPreferencePersistence,
searchQuery: searchValue,
sortOptions: sort,
});
}, [
currentChain,
searchValue,
sort,
debouncedSearch,
setDelegationPreferencePersistence,
delegationPreferencePersistence,
]);

const loadMoreData = () => fetchStakePools({ searchString: searchValue, skip: skip + defaultFetchLimit, sort });

Expand Down Expand Up @@ -141,6 +157,7 @@ export const StakePoolsTable = ({ scrollableTargetId }: StakePoolsTableProps) =>
withSearchIcon
inputPlaceholder={t('browsePools.stakePoolTableBrowser.searchInputPlaceholder')}
onChange={onSearch}
value={searchValue}
data-testid="search-input"
loading={fetchingPools}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ export enum StateStatus {
ERROR = 'error',
}

export interface DelegationPreferences {
sortOptions: StakePoolSortOptions;
searchQuery?: string;
}

export interface IBlockchainProvider {
stakePoolProvider: Wallet.StakePoolProvider;
assetProvider: Wallet.AssetProvider;
Expand All @@ -52,6 +57,8 @@ export interface IBlockchainProvider {

export type OutsideHandlesContextValue = {
analytics: IAnalyticsTracker;
delegationPreferencePersistence: DelegationPreferences;
setDelegationPreferencePersistence: (preferences: DelegationPreferences) => void;
backgroundServiceAPIContextSetWalletPassword: (password?: Uint8Array) => void;
expandStakingView?: () => void;
balancesBalance?: Balance;
Expand Down
1 change: 1 addition & 0 deletions packages/staking/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ export { config as stakePooltableConfig } from './features/BrowsePools/StakePool
export type { TableRowProps } from './features/BrowsePools/StakePoolsTable/Table';
export * from './features/BrowsePools/StakePoolsTable/types';
export { OutsideHandlesProvider } from './features/outside-handles-provider';
export type { DelegationPreferences } from './features/outside-handles-provider';
export { MAX_POOLS_COUNT } from './features/store';

0 comments on commit 7f19131

Please sign in to comment.