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

Add account address argument to voucher hooks #1464

Merged
merged 3 commits into from
Dec 12, 2023
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { HexString } from '@gear-js/api';
import { useBalanceFormat, useVoucher } from '@gear-js/react-hooks';
import { useAccountVoucher, useBalanceFormat } from '@gear-js/react-hooks';

import { LabeledCheckbox } from '@/shared/ui';

Expand All @@ -10,7 +10,7 @@ type Props = {
};

const UseVoucherCheckbox = ({ programId }: Props) => {
const { isVoucherExists, voucherBalance } = useVoucher(programId);
const { isVoucherExists, voucherBalance } = useAccountVoucher(programId);
const { getFormattedBalance } = useBalanceFormat();

const formattedBalance = voucherBalance ? getFormattedBalance(voucherBalance) : undefined;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { HexString } from '@gear-js/api';
import { useIsVoucherExists } from '@gear-js/react-hooks';
import { useIsAccountVoucherExists } from '@gear-js/react-hooks';

import { withAccount } from '@/shared/ui';

Expand All @@ -11,7 +11,7 @@ type Props = {
};

const VoucherBadge = withAccount(({ programId }: Props) => {
const { isVoucherExists } = useIsVoucherExists(programId);
const { isVoucherExists } = useIsAccountVoucherExists(programId);

return isVoucherExists ? <BadgeSVG className={styles.badge} /> : null;
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { HexString } from '@gear-js/api';
import { useBalanceFormat, useVoucher } from '@gear-js/react-hooks';
import { useAccountVoucher, useBalanceFormat } from '@gear-js/react-hooks';

import VoucherPlaceholderSVG from '@/features/voucher/assets/voucher-placeholder.svg?react';
import { ContentLoader } from '@/shared/ui/contentLoader';
Expand All @@ -14,7 +14,7 @@ type Props = {
};

const VoucherTable = withAccount(({ programId }: Props) => {
const { isVoucherReady, isVoucherExists, voucherBalance } = useVoucher(programId);
const { isVoucherReady, isVoucherExists, voucherBalance } = useAccountVoucher(programId);
const { getFormattedBalance } = useBalanceFormat();

const status = isVoucherExists ? BulbStatus.Success : BulbStatus.Error;
Expand Down
2 changes: 1 addition & 1 deletion utils/gear-hooks/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@gear-js/react-hooks",
"version": "0.9.3",
"version": "0.9.4",
"description": "React hooks used across Gear applications",
"author": "Gear Technologies",
"license": "GPL-3.0",
Expand Down
13 changes: 12 additions & 1 deletion utils/gear-hooks/src/hooks/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,15 @@ import {
useReplyCalculateGas,
} from './useCalculateGas';

import { useIsVoucherExists, useVoucherBalance, useVoucher } from './voucher';
import {
useIsVoucherExists,
useVoucherBalance,
useVoucher,
useIsAccountVoucherExists,
useAccountVoucherBalance,
useAccountVoucher,
} from './voucher';

import { useBalance, useBalanceFormat } from './balance';

export {
Expand All @@ -26,6 +34,9 @@ export {
useIsVoucherExists,
useVoucherBalance,
useVoucher,
useIsAccountVoucherExists,
useAccountVoucherBalance,
useAccountVoucher,
useBalance,
useBalanceFormat,
SendMessageOptions,
Expand Down
13 changes: 12 additions & 1 deletion utils/gear-hooks/src/hooks/api/voucher/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,15 @@ import { useIsVoucherExists } from './use-is-voucher-exists';
import { useVoucherBalance } from './use-voucher-balance';
import { useVoucher } from './use-voucher';

export { useIsVoucherExists, useVoucherBalance, useVoucher };
import { useIsAccountVoucherExists } from './use-is-account-voucher-exists';
import { useAccountVoucherBalance } from './use-account-voucher-balance';
import { useAccountVoucher } from './use-account-voucher';

export {
useIsVoucherExists,
useVoucherBalance,
useVoucher,
useIsAccountVoucherExists,
useAccountVoucherBalance,
useAccountVoucher,
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { AccountContext } from 'context';
import { useContext } from 'react';
import { useVoucherBalance } from './use-voucher-balance';
import { HexString } from '@gear-js/api';

function useAccountVoucherBalance(programId: HexString | undefined) {
const { account } = useContext(AccountContext);

return useVoucherBalance(programId, account?.decodedAddress);
}

export { useAccountVoucherBalance };
12 changes: 12 additions & 0 deletions utils/gear-hooks/src/hooks/api/voucher/use-account-voucher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { useContext } from 'react';
import { useVoucher } from './use-voucher';
import { AccountContext } from 'context';
import { HexString } from '@gear-js/api';

function useAccountVoucher(programId: HexString | undefined) {
const { account } = useContext(AccountContext);

return useVoucher(programId, account?.decodedAddress);
}

export { useAccountVoucher };
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { HexString } from '@gear-js/api';
import { useIsVoucherExists } from './use-is-voucher-exists';
import { useContext } from 'react';
import { AccountContext } from 'context';

function useIsAccountVoucherExists(programId: HexString | undefined) {
const { account } = useContext(AccountContext);

return useIsVoucherExists(programId, account?.decodedAddress);
}

export { useIsAccountVoucherExists };
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import { HexString } from '@gear-js/api';
import { AccountContext, AlertContext, ApiContext } from 'context';
import { AlertContext, ApiContext } from 'context';
import { useState, useEffect, useContext } from 'react';

function useIsVoucherExists(programId: HexString | undefined) {
function useIsVoucherExists(programId: HexString | undefined, accountAddress: HexString | undefined) {
const { api, isApiReady } = useContext(ApiContext); // сircular dependency fix
const alert = useContext(AlertContext);

const { account } = useContext(AccountContext);
const accountAddress = account?.decodedAddress;

const [isVoucherExists, setIsVoucherExists] = useState<boolean>();
const isVoucherExistsReady = isVoucherExists !== undefined;

Expand Down
8 changes: 2 additions & 6 deletions utils/gear-hooks/src/hooks/api/voucher/use-voucher-balance.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
import { HexString, generateVoucherId } from '@gear-js/api';
import { AccountContext } from 'context';
import { useContext } from 'react';
import { useBalance } from '../balance/use-balance';

function useVoucherBalance(programId: HexString | undefined) {
const { account } = useContext(AccountContext);
const accountAddress = account?.decodedAddress;
import { useBalance } from '../balance/use-balance';

function useVoucherBalance(programId: HexString | undefined, accountAddress: HexString | undefined) {
const { balance, isBalanceReady } = useBalance(
programId && accountAddress ? generateVoucherId(accountAddress, programId) : undefined,
);
Expand Down
6 changes: 3 additions & 3 deletions utils/gear-hooks/src/hooks/api/voucher/use-voucher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { HexString } from '@polkadot/util/types';
import { useIsVoucherExists } from './use-is-voucher-exists';
import { useVoucherBalance } from './use-voucher-balance';

function useVoucher(programId: HexString | undefined) {
const { isVoucherExists, isVoucherExistsReady } = useIsVoucherExists(programId);
const { voucherBalance, isVoucherBalanceReady } = useVoucherBalance(programId);
function useVoucher(programId: HexString | undefined, accountAddress: HexString | undefined) {
const { isVoucherExists, isVoucherExistsReady } = useIsVoucherExists(programId, accountAddress);
const { voucherBalance, isVoucherBalanceReady } = useVoucherBalance(programId, accountAddress);

const isVoucherReady = isVoucherExistsReady && isVoucherBalanceReady;

Expand Down
6 changes: 6 additions & 0 deletions utils/gear-hooks/src/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import {
useIsVoucherExists,
useVoucherBalance,
useVoucher,
useIsAccountVoucherExists,
useAccountVoucherBalance,
useAccountVoucher,
useBalance,
useBalanceFormat,
SendMessageOptions,
Expand All @@ -32,6 +35,9 @@ export {
useIsVoucherExists,
useVoucherBalance,
useVoucher,
useIsAccountVoucherExists,
useAccountVoucherBalance,
useAccountVoucher,
useBalance,
useBalanceFormat,
useAccount,
Expand Down
6 changes: 6 additions & 0 deletions utils/gear-hooks/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import {
useIsVoucherExists,
useVoucherBalance,
useVoucher,
useIsAccountVoucherExists,
useAccountVoucherBalance,
useAccountVoucher,
useBalance,
useBalanceFormat,
useAccount,
Expand Down Expand Up @@ -60,6 +63,9 @@ export {
useIsVoucherExists,
useVoucherBalance,
useVoucher,
useIsAccountVoucherExists,
useAccountVoucherBalance,
useAccountVoucher,
useBalance,
useBalanceFormat,
useAccount,
Expand Down
Loading