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

fixed: user address not being displayed when clicked button before fu… #460

Merged
Show file tree
Hide file tree
Changes from all 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
41 changes: 27 additions & 14 deletions src/ui/hooks/__tests__/useProfileHook.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ vi.mock('@/ui/stores/profileStore', () => ({
setMainAddress: vi.fn(),
setEvmAddress: vi.fn(),
setEvmWallet: vi.fn(),
setUserWallet: vi.fn(),
setParentWallet: vi.fn(),
setCurrent: vi.fn(),
setChildAccount: vi.fn(),
setUserInfo: vi.fn(),
Expand Down Expand Up @@ -110,6 +110,12 @@ vi.mock('@/ui/utils/WalletContext', () => ({
loggedInAccounts: ['account1'],
}),
},
returnMainWallet: vi.fn().mockResolvedValue({
name: 'Test Wallet',
address: '0x138c20de202897fb',
type: 'flow',
blockchain: 'flow',
}),
}),
}));

Expand All @@ -119,7 +125,7 @@ describe('useProfileHook', () => {
setMainAddress: mocks.setMainAddress,
setEvmAddress: mocks.setEvmAddress,
setEvmWallet: mocks.setEvmWallet,
setUserWallet: vi.fn(),
setParentWallet: vi.fn(),
setCurrent: vi.fn(),
setChildAccount: vi.fn(),
setUserInfo: vi.fn(),
Expand Down Expand Up @@ -158,32 +164,40 @@ describe('useProfileHook', () => {
describe('freshUserWallet', () => {
it('should update user wallet data', async () => {
const mockSetWalletList = vi.fn();
const mockSetUserWallet = vi.fn();
const mockSetParentWallet = vi.fn();
const mockSetCurrent = vi.fn();
const mockSetMainLoading = vi.fn();

vi.mocked(useProfileStore).mockReturnValueOnce({
vi.mocked(useProfileStore).mockReturnValue({
setMainAddress: vi.fn(),
setEvmAddress: vi.fn(),
setEvmWallet: vi.fn(),
setUserWallet: mockSetUserWallet,
setCurrent: vi.fn(),
setParentWallet: mockSetParentWallet,
setCurrent: mockSetCurrent,
setChildAccount: vi.fn(),
setUserInfo: vi.fn(),
setOtherAccounts: vi.fn(),
setLoggedInAccounts: vi.fn(),
setMainLoading: vi.fn(),
setMainLoading: mockSetMainLoading,
setEvmLoading: vi.fn(),
setInitial: vi.fn(),
setWalletList: mockSetWalletList,
initialStart: true,
});

const { freshUserWallet } = useProfileHook();
const { fetchUserWallet } = useProfileHook();
await act(async () => {
await freshUserWallet();
await fetchUserWallet();
});

expect(mockSetWalletList).toHaveBeenCalled();
expect(mockSetUserWallet).toHaveBeenCalled();
expect(mockSetParentWallet).toHaveBeenCalledWith({
name: 'Test Wallet',
address: '0x138c20de202897fb',
type: 'flow',
blockchain: 'flow',
});
expect(mockSetCurrent).toHaveBeenCalled();
expect(mockSetMainLoading).toHaveBeenCalledWith(false);
});
});

Expand All @@ -193,12 +207,11 @@ describe('useProfileHook', () => {
const mockSetOtherAccounts = vi.fn();
const mockSetLoggedInAccounts = vi.fn();

// Mock the profile store
vi.mocked(useProfileStore).mockReturnValueOnce({
vi.mocked(useProfileStore).mockReturnValue({
setMainAddress: vi.fn(),
setEvmAddress: vi.fn(),
setEvmWallet: vi.fn(),
setUserWallet: vi.fn(),
setParentWallet: vi.fn(),
setCurrent: vi.fn(),
setChildAccount: vi.fn(),
setUserInfo: mockSetUserInfo,
Expand Down
19 changes: 6 additions & 13 deletions src/ui/hooks/useProfileHook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ export const useProfileHook = () => {
const {
setMainAddress,
setEvmAddress,
setUserWallet,
setInitial,
setChildAccount,
setCurrent,
Expand All @@ -28,6 +27,7 @@ export const useProfileHook = () => {
setOtherAccounts,
setLoggedInAccounts,
setWalletList,
setParentWallet,
initialStart,
} = useProfileStore();

Expand Down Expand Up @@ -93,6 +93,7 @@ export const useProfileHook = () => {
const freshUserInfo = useCallback(async () => {
if (!usewallet || !walletLoaded) return;
try {
//TODO: should rethink the wording of the wallet functions, have it be parent evm and child or something similar. State name and should be the same frontend and background.
const [currentWallet, isChild, mainAddress] = await Promise.all([
usewallet.getCurrentWallet(),
usewallet.getActiveWallet(),
Expand All @@ -102,14 +103,14 @@ export const useProfileHook = () => {
if (!currentWallet) {
throw new Error('Current wallet is undefined');
}

const mainwallet = await usewallet.returnMainWallet();
setParentWallet(mainwallet!);
if (isChild === 'evm') {
const evmWalletData = await setupEvmWallet(mainAddress!);
await setCurrent(evmWalletData);
} else if (isChild) {
await setCurrent(currentWallet);
} else {
const mainwallet = await usewallet.returnMainWallet();
await setCurrent(mainwallet);
}

Expand Down Expand Up @@ -155,6 +156,7 @@ export const useProfileHook = () => {
setCurrent,
setupEvmWallet,
setMainLoading,
setParentWallet,
]);

// 1. First called in index.ts, get the user info(name and avatar) and the main address
Expand All @@ -177,22 +179,13 @@ export const useProfileHook = () => {
const wallet: WalletResponse[] = await usewallet.getUserWallets();
const fData: WalletResponse[] = wallet.filter((item) => item.blockchain !== null);

setUserWallet(fData);
if (initialStart) {
await usewallet.openapi.putDeviceInfo(fData);
setInitial(false);
}
const formattedWallets = formatWallets(fData);
setWalletList(formattedWallets);
}, [
usewallet,
initialStart,
setUserWallet,
setInitial,
formatWallets,
setWalletList,
walletLoaded,
]);
}, [usewallet, initialStart, setInitial, formatWallets, setWalletList, walletLoaded]);

// 3. Third called in index.ts check the child account and set the child account
const fetchUserWallet = useCallback(async () => {
Expand Down
48 changes: 21 additions & 27 deletions src/ui/stores/profileStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import type { ChildAccount, WalletType, UserInfoResponse } from '../../shared/ty
interface ProfileState {
mainAddress: string;
evmAddress: string;
userWallet: any | null;
currentWalletIndex: number;
parentWallet: WalletType;
evmWallet: WalletType;
walletList: any[];
walletList: WalletType[];
initialStart: boolean;
currentWallet: any;
currentWallet: WalletType;
mainAddressLoading: boolean;
childAccounts: ChildAccount;
evmLoading: boolean;
Expand All @@ -22,8 +22,8 @@ interface ProfileState {
loggedInAccounts: LoggedInAccount[];
setMainAddress: (address: string) => void;
setEvmAddress: (address: string) => void;
setUserWallet: (wallet: any) => void;
setCurrentWalletIndex: (index: number) => void;
setParentWallet: (wallet: WalletType) => void;
setEvmWallet: (wallet: WalletType) => void;
setWalletList: (list: any[]) => void;
setInitial: (initial: boolean) => void;
Expand All @@ -38,21 +38,23 @@ interface ProfileState {
clearProfileData: () => void;
}

const INITIAL_WALLET = {
name: '',
icon: '',
address: '',
chain_id: 'flow',
id: 1,
coins: ['flow'],
color: '',
};

export const useProfileStore = create<ProfileState>((set) => ({
mainAddress: '',
evmAddress: '',
userWallet: null,
currentWalletIndex: 0,
evmWallet: {
name: '',
icon: '',
address: '',
chain_id: 'evm',
id: 1,
coins: ['flow'],
color: '',
},
currentWallet: {},
parentWallet: { ...INITIAL_WALLET },
evmWallet: { ...INITIAL_WALLET, chain_id: 'evm' },
currentWallet: { ...INITIAL_WALLET },
walletList: [],
initialStart: true,
mainAddressLoading: true,
Expand All @@ -64,8 +66,8 @@ export const useProfileStore = create<ProfileState>((set) => ({
listLoading: true,
setMainAddress: (address) => set({ mainAddress: address }),
setEvmAddress: (address) => set({ evmAddress: address }),
setUserWallet: (wallet) => set({ userWallet: wallet }),
setCurrentWalletIndex: (index) => set({ currentWalletIndex: index }),
setParentWallet: (wallet) => set({ parentWallet: wallet }),
setEvmWallet: (wallet) => set({ evmWallet: wallet }),
setWalletList: (list) => set({ walletList: list }),
setInitial: (initial) => set({ initialStart: initial }),
Expand All @@ -81,20 +83,12 @@ export const useProfileStore = create<ProfileState>((set) => ({
set({
mainAddress: '',
evmAddress: '',
userWallet: null,
currentWalletIndex: 0,
evmWallet: {
name: '',
icon: '',
address: '',
chain_id: 'evm',
id: 1,
coins: ['flow'],
color: '',
},
parentWallet: { ...INITIAL_WALLET },
evmWallet: { ...INITIAL_WALLET, chain_id: 'evm' },
walletList: [],
initialStart: true,
currentWallet: {},
currentWallet: { ...INITIAL_WALLET },
mainAddressLoading: true,
childAccounts: {},
evmLoading: true,
Expand Down
Loading
Loading