2023-09-14 19:10:33 +00:00
|
|
|
import { create } from 'zustand';
|
|
|
|
import { persist } from 'zustand/middleware';
|
2023-09-12 15:07:37 +00:00
|
|
|
import { useVegaWallet } from '@vegaprotocol/wallet';
|
2023-08-24 08:03:56 +00:00
|
|
|
import { depositsProvider } from '@vegaprotocol/deposits';
|
|
|
|
import { useDataProvider } from '@vegaprotocol/data-provider';
|
|
|
|
import { ordersWithMarketProvider } from '@vegaprotocol/orders';
|
|
|
|
import * as Types from '@vegaprotocol/types';
|
|
|
|
import { aggregatedAccountsDataProvider } from '@vegaprotocol/accounts';
|
|
|
|
import { positionsDataProvider } from '@vegaprotocol/positions';
|
|
|
|
import { useGlobalStore } from '../../stores';
|
|
|
|
|
2023-09-14 19:10:33 +00:00
|
|
|
const ONBOARDING_STORAGE_KEY = 'vega_onboarding';
|
|
|
|
export const useOnboardingStore = create<{
|
|
|
|
dialogOpen: boolean;
|
2023-09-22 09:04:02 +00:00
|
|
|
walletDialogOpen: boolean;
|
2023-09-14 19:10:33 +00:00
|
|
|
dismissed: boolean;
|
|
|
|
dismiss: () => void;
|
|
|
|
setDialogOpen: (isOpen: boolean) => void;
|
2023-09-22 09:04:02 +00:00
|
|
|
setWalletDialogOpen: (isOpen: boolean) => void;
|
2023-09-14 19:10:33 +00:00
|
|
|
}>()(
|
|
|
|
persist(
|
|
|
|
(set) => ({
|
|
|
|
dialogOpen: true,
|
2023-09-22 09:04:02 +00:00
|
|
|
walletDialogOpen: false,
|
2023-09-14 19:10:33 +00:00
|
|
|
dismissed: false,
|
|
|
|
dismiss: () => set({ dismissed: true }),
|
|
|
|
setDialogOpen: (isOpen) => set({ dialogOpen: isOpen }),
|
2023-09-22 09:04:02 +00:00
|
|
|
setWalletDialogOpen: (isOpen) => set({ walletDialogOpen: isOpen }),
|
2023-09-14 19:10:33 +00:00
|
|
|
}),
|
|
|
|
{
|
|
|
|
name: ONBOARDING_STORAGE_KEY,
|
|
|
|
partialize: (state) => ({
|
|
|
|
dismissed: state.dismissed,
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2023-08-24 08:03:56 +00:00
|
|
|
export enum OnboardingStep {
|
|
|
|
ONBOARDING_UNKNOWN_STEP,
|
|
|
|
ONBOARDING_WALLET_STEP,
|
|
|
|
ONBOARDING_CONNECT_STEP,
|
|
|
|
ONBOARDING_DEPOSIT_STEP,
|
|
|
|
ONBOARDING_ORDER_STEP,
|
|
|
|
ONBOARDING_COMPLETE_STEP,
|
|
|
|
}
|
|
|
|
|
|
|
|
export const useGetOnboardingStep = () => {
|
|
|
|
const connecting = useGlobalStore((store) => store.eagerConnecting);
|
|
|
|
const { pubKey = '', pubKeys } = useVegaWallet();
|
|
|
|
const { data: depositsData } = useDataProvider({
|
|
|
|
dataProvider: depositsProvider,
|
|
|
|
variables: { partyId: pubKey || '' },
|
|
|
|
skip: !pubKey,
|
|
|
|
});
|
|
|
|
const { data: collateralData } = useDataProvider({
|
|
|
|
dataProvider: aggregatedAccountsDataProvider,
|
|
|
|
variables: { partyId: pubKey || '' },
|
|
|
|
skip: !pubKey,
|
|
|
|
});
|
|
|
|
const collaterals = Boolean(collateralData?.length);
|
|
|
|
const deposits =
|
|
|
|
depositsData?.some(
|
|
|
|
(item) => item.status === Types.DepositStatus.STATUS_FINALIZED
|
|
|
|
) || false;
|
|
|
|
const { data: ordersData } = useDataProvider({
|
|
|
|
dataProvider: ordersWithMarketProvider,
|
|
|
|
variables: {
|
|
|
|
partyId: pubKey || '',
|
|
|
|
pagination: {
|
|
|
|
first: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
skip: !pubKey,
|
|
|
|
});
|
|
|
|
const orders = Boolean(ordersData?.length);
|
|
|
|
|
|
|
|
const partyIds = pubKeys?.map((item) => item.publicKey) || [];
|
|
|
|
const { data: positionsData } = useDataProvider({
|
|
|
|
dataProvider: positionsDataProvider,
|
|
|
|
variables: {
|
|
|
|
partyIds,
|
|
|
|
},
|
|
|
|
skip: !partyIds?.length,
|
|
|
|
});
|
|
|
|
const positions = Boolean(positionsData?.length);
|
|
|
|
|
|
|
|
const isLoading = Boolean(
|
|
|
|
(connecting || pubKey) &&
|
|
|
|
(depositsData === null ||
|
|
|
|
ordersData === null ||
|
|
|
|
collateralData === null ||
|
|
|
|
positionsData === null)
|
|
|
|
);
|
2023-09-12 15:07:37 +00:00
|
|
|
let step = OnboardingStep.ONBOARDING_UNKNOWN_STEP;
|
2023-08-24 08:03:56 +00:00
|
|
|
if (isLoading) {
|
2023-09-12 15:07:37 +00:00
|
|
|
return step;
|
2023-08-24 08:03:56 +00:00
|
|
|
}
|
|
|
|
if (!pubKey) {
|
2023-09-12 15:07:37 +00:00
|
|
|
step = OnboardingStep.ONBOARDING_CONNECT_STEP;
|
|
|
|
}
|
|
|
|
if (pubKey) {
|
|
|
|
step = OnboardingStep.ONBOARDING_DEPOSIT_STEP;
|
2023-08-24 08:03:56 +00:00
|
|
|
}
|
2023-09-12 15:07:37 +00:00
|
|
|
if (pubKey && (deposits || collaterals)) {
|
|
|
|
step = OnboardingStep.ONBOARDING_ORDER_STEP;
|
2023-08-24 08:03:56 +00:00
|
|
|
}
|
2023-09-12 15:07:37 +00:00
|
|
|
if (pubKey && (orders || positions)) {
|
|
|
|
step = OnboardingStep.ONBOARDING_COMPLETE_STEP;
|
2023-08-24 08:03:56 +00:00
|
|
|
}
|
2023-09-12 15:07:37 +00:00
|
|
|
return step;
|
2023-08-24 08:03:56 +00:00
|
|
|
};
|