Skip to content

Commit

Permalink
fix get account and network functions and move them into utils
Browse files Browse the repository at this point in the history
  • Loading branch information
Reza Erami authored and rezaerami committed Aug 15, 2023
1 parent 015f68f commit 689fcbe
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 33 deletions.
20 changes: 5 additions & 15 deletions packages/apps/tools/src/components/Common/Layout/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { kadenaConstants, Network } from '@/constants/kadena';
import routes from '@/constants/routes';
import { useLayoutContext } from '@/context';
import { useWalletConnectClient } from '@/context/connect-wallet-context';
import { getNetworks } from '@/utils/wallet';
import classNames from 'classnames';
import useTranslation from 'next-translate/useTranslation';
import React, { type ReactNode, FC } from 'react';
Expand Down Expand Up @@ -44,20 +45,9 @@ export const Layout: FC<IProps> = ({ children }: IProps) => {
},
];

const getNetworks = (): Network[] => {
if (!session) return ['mainnet01', 'testnet04'];

const result = new Set(
accounts
?.map((account) => {
const [, network] = account.split(':');
return network;
})
?.filter((network) => network !== 'development'),
);

return Array.from(result ?? []) as Network[];
};
const networks: Network[] = session
? getNetworks(accounts)
: ['mainnet01', 'testnet04'];

return (
<div>
Expand All @@ -76,7 +66,7 @@ export const Layout: FC<IProps> = ({ children }: IProps) => {
}
icon={SystemIcon.Link}
>
{getNetworks().map((network) => (
{networks.map((network) => (
<Option key={network} value={network}>
{kadenaConstants?.[network].label}
</Option>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { accountInputWrapperStyle } from './styles.css';

import { useWalletConnectClient } from '@/context/connect-wallet-context';
import { useDidUpdateEffect } from '@/hooks';
import { getAccounts } from '@/utils/wallet';
import useTranslation from 'next-translate/useTranslation';
import React, { ChangeEvent, FC, useState } from 'react';
import { FieldError } from 'react-hook-form';
Expand Down Expand Up @@ -57,23 +58,6 @@ export const AccountNameField: FC<IAccountNameFieldProps> = ({
else setMode('input');
}, [session, accounts]);

const getAccounts = (): string[] => {
// eslint-disable-next-line @rushstack/security/no-unsafe-regexp
const regex = new RegExp(`kadena\:${selectedNetwork}.*`);
const result = new Set(
accounts
?.filter((account) => {
if (account.match(regex)) return account;
})
?.map((account) => {
const [, , accountName] = account.split(':');
return accountName;
}),
);

return Array.from(result) ?? [];
};

const lookup = {
select: (
<Select
Expand All @@ -88,7 +72,7 @@ export const AccountNameField: FC<IAccountNameFieldProps> = ({
id={elementId}
>
<Option value={''}>{t('Select Account')}</Option>
{getAccounts().map((account) => (
{getAccounts(accounts, selectedNetwork).map((account) => (
<Option key={account} value={account}>
{account.slice(0, 4)}****{account.slice(-4)}
</Option>
Expand Down
24 changes: 24 additions & 0 deletions packages/apps/tools/src/utils/wallet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Network } from '@/constants/kadena';

export const getNetworks = (accounts: string[] = []): Network[] =>
Array.from(
new Set(
accounts
?.map((account) => account.split(':')[1])
?.filter((network) => network !== 'development'),
),
) as Network[];

export const getAccounts = (
accounts: string[] = [],
selectedNetwork?: string,
): string[] =>
Array.from(
new Set(
accounts
?.filter((account: string): boolean =>
account.includes(`kadena:${selectedNetwork}`),
)
?.map((account) => account.split(':')[2]),
),
);

1 comment on commit 689fcbe

@vercel
Copy link

@vercel vercel bot commented on 689fcbe Aug 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

tools – ./packages/apps/tools

tools-kadena-js.vercel.app
kadena-js-transfer.vercel.app
tools.kadena.io
tools-git-main-kadena-js.vercel.app

Please sign in to comment.