feat(trading): referral preview update (#5251)

This commit is contained in:
m.ray 2023-11-14 16:31:00 +02:00 committed by GitHub
parent b3014bb98a
commit 06769f25cf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 86 additions and 22 deletions

View File

@ -17,7 +17,7 @@ import { useReferral } from './hooks/use-referral';
import { Routes } from '../../lib/links'; import { Routes } from '../../lib/links';
import { useTransactionEventSubscription } from '@vegaprotocol/web3'; import { useTransactionEventSubscription } from '@vegaprotocol/web3';
import { t } from '@vegaprotocol/i18n'; import { t } from '@vegaprotocol/i18n';
import { Statistics } from './referral-statistics'; import { Statistics, useStats } from './referral-statistics';
import { useReferralProgram } from './hooks/use-referral-program'; import { useReferralProgram } from './hooks/use-referral-program';
const RELOAD_DELAY = 3000; const RELOAD_DELAY = 3000;
@ -132,6 +132,8 @@ export const ApplyCodeForm = () => {
}), }),
}); });
const { epochsValue, nextBenefitTierValue } = useStats({ program });
// go to main page when successfully applied // go to main page when successfully applied
useEffect(() => { useEffect(() => {
if (status === 'successful') { if (status === 'successful') {
@ -196,6 +198,10 @@ export const ApplyCodeForm = () => {
}; };
}; };
const nextBenefitTierEpochsValue = nextBenefitTierValue
? nextBenefitTierValue.epochs - epochsValue
: 0;
return ( return (
<> <>
<div className="w-2/3 max-w-md mx-auto bg-vega-clight-800 dark:bg-vega-cdark-800 p-8 rounded-lg"> <div className="w-2/3 max-w-md mx-auto bg-vega-clight-800 dark:bg-vega-cdark-800 p-8 rounded-lg">
@ -238,7 +244,12 @@ export const ApplyCodeForm = () => {
) : null} ) : null}
{previewData ? ( {previewData ? (
<div className="mt-10"> <div className="mt-10">
<h2 className="text-2xl mb-5">{t('You are joining')}</h2> <h2 className="text-2xl mb-5">
{t(
'You are joining the group shown, but will not have access to benefits until you have completed at least %s epochs.',
[nextBenefitTierEpochsValue.toString()]
)}
</h2>
<Statistics data={previewData} program={program} as="referee" /> <Statistics data={previewData} program={program} as="referee" />
</div> </div>
) : null} ) : null}

View File

@ -25,7 +25,7 @@ import compact from 'lodash/compact';
import { useReferralProgram } from './hooks/use-referral-program'; import { useReferralProgram } from './hooks/use-referral-program';
import { useStakeAvailable } from './hooks/use-stake-available'; import { useStakeAvailable } from './hooks/use-stake-available';
import sortBy from 'lodash/sortBy'; import sortBy from 'lodash/sortBy';
import { useLayoutEffect, useRef, useState } from 'react'; import { useLayoutEffect, useMemo, useRef, useState } from 'react';
import { useCurrentEpochInfoQuery } from './hooks/__generated__/Epoch'; import { useCurrentEpochInfoQuery } from './hooks/__generated__/Epoch';
import BigNumber from 'bignumber.js'; import BigNumber from 'bignumber.js';
import { t } from '@vegaprotocol/i18n'; import { t } from '@vegaprotocol/i18n';
@ -59,21 +59,20 @@ export const ReferralStatistics = () => {
return <CreateCodeContainer />; return <CreateCodeContainer />;
}; };
export const Statistics = ({ export const useStats = ({
data, data,
program, program,
as, as,
}: { }: {
data: NonNullable<ReturnType<typeof useReferral>['data']>; data?: NonNullable<ReturnType<typeof useReferral>['data']>;
program: ReturnType<typeof useReferralProgram>; program: ReturnType<typeof useReferralProgram>;
as: 'referrer' | 'referee'; as?: 'referrer' | 'referee';
}) => { }) => {
const { benefitTiers, details } = program; const { benefitTiers } = program;
const { data: epochData } = useCurrentEpochInfoQuery(); const { data: epochData } = useCurrentEpochInfoQuery();
const { stakeAvailable } = useStakeAvailable();
const { data: statsData } = useReferralSetStatsQuery({ const { data: statsData } = useReferralSetStatsQuery({
variables: { variables: {
code: data.code, code: data?.code || '',
}, },
skip: !data?.code, skip: !data?.code,
fetchPolicy: 'cache-and-network', fetchPolicy: 'cache-and-network',
@ -81,19 +80,12 @@ export const Statistics = ({
const currentEpoch = Number(epochData?.epoch.id); const currentEpoch = Number(epochData?.epoch.id);
const compactNumFormat = new Intl.NumberFormat(getUserLocale(), {
minimumFractionDigits: 0,
maximumFractionDigits: 2,
notation: 'compact',
compactDisplay: 'short',
});
const stats = const stats =
statsData?.referralSetStats.edges && statsData?.referralSetStats.edges &&
compact(removePaginationWrapper(statsData.referralSetStats.edges)); compact(removePaginationWrapper(statsData.referralSetStats.edges));
const refereeInfo = data.referee; const refereeInfo = data?.referee;
const refereeStats = stats?.find( const refereeStats = stats?.find(
(r) => r.partyId === data.referee?.refereeId (r) => r.partyId === data?.referee?.refereeId
); );
const statsAvailable = stats && stats.length > 0 && stats[0]; const statsAvailable = stats && stats.length > 0 && stats[0];
@ -136,6 +128,60 @@ export const Statistics = ({
? nextBenefitTierValue.epochs - epochsValue ? nextBenefitTierValue.epochs - epochsValue
: 0; : 0;
return {
baseCommissionValue,
runningVolumeValue,
referrerVolumeValue,
multiplier,
finalCommissionValue,
discountFactorValue,
currentBenefitTierValue,
nextBenefitTierValue,
epochsValue,
nextBenefitTierVolumeValue,
nextBenefitTierEpochsValue,
};
};
export const Statistics = ({
data,
program,
as,
}: {
data: NonNullable<ReturnType<typeof useReferral>['data']>;
program: ReturnType<typeof useReferralProgram>;
as: 'referrer' | 'referee';
}) => {
const {
baseCommissionValue,
runningVolumeValue,
referrerVolumeValue,
multiplier,
finalCommissionValue,
discountFactorValue,
currentBenefitTierValue,
epochsValue,
nextBenefitTierVolumeValue,
nextBenefitTierEpochsValue,
} = useStats({ data, program, as });
const isApplyCodePreview = useMemo(
() => data.referee === null,
[data.referee]
);
const { benefitTiers } = useReferralProgram();
const { stakeAvailable } = useStakeAvailable();
const { details } = program;
const compactNumFormat = new Intl.NumberFormat(getUserLocale(), {
minimumFractionDigits: 0,
maximumFractionDigits: 2,
notation: 'compact',
compactDisplay: 'short',
});
const baseCommissionTile = ( const baseCommissionTile = (
<StatTile <StatTile
title={t('Base commission rate')} title={t('Base commission rate')}
@ -229,11 +275,18 @@ export const Statistics = ({
const currentBenefitTierTile = ( const currentBenefitTierTile = (
<StatTile title={t('Current tier')}> <StatTile title={t('Current tier')}>
{currentBenefitTierValue?.tier || 'None'} {isApplyCodePreview
? currentBenefitTierValue?.tier || benefitTiers[0]?.tier || 'None'
: currentBenefitTierValue?.tier || 'None'}
</StatTile> </StatTile>
); );
const discountFactorTile = ( const discountFactorTile = (
<StatTile title={t('Discount')}>{discountFactorValue * 100}%</StatTile> <StatTile title={t('Discount')}>
{isApplyCodePreview
? benefitTiers[0].discountFactor * 100
: discountFactorValue * 100}
%
</StatTile>
); );
const runningVolumeTile = ( const runningVolumeTile = (
<StatTile <StatTile
@ -265,11 +318,11 @@ export const Statistics = ({
<> <>
<div className="grid grid-rows-1 gap-5 grid-cols-1 md:grid-cols-3"> <div className="grid grid-rows-1 gap-5 grid-cols-1 md:grid-cols-3">
{currentBenefitTierTile} {currentBenefitTierTile}
{discountFactorTile} {runningVolumeTile}
{codeTile} {codeTile}
</div> </div>
<div className="grid grid-rows-1 gap-5 grid-cols-1 sm:grid-cols-2 xl:grid-cols-4"> <div className="grid grid-rows-1 gap-5 grid-cols-1 sm:grid-cols-2 xl:grid-cols-4">
{runningVolumeTile} {discountFactorTile}
{nextTierVolumeTile} {nextTierVolumeTile}
{epochsTile} {epochsTile}
{nextTierEpochsTile} {nextTierEpochsTile}