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

feat: Refetch token price if online status is true #2337

Merged
merged 1 commit into from
Nov 23, 2024
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
32 changes: 25 additions & 7 deletions packages/app/src/library/NetworkBar/TokenPrice.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,36 @@

import { useEffectIgnoreInitial } from '@w3ux/hooks';
import { useNetwork } from 'contexts/Network';
import { isCustomEvent } from 'controllers/utils';
import {
ApolloProvider,
client,
useTokenPrice,
formatResult,
formatTokenPrice,
} from 'plugin-staking-api';
import { useRef } from 'react';
import { useEventListener } from 'usehooks-ts';

export const TokenPriceInner = () => {
const {
networkData: {
api: { unit },
},
} = useNetwork();

const { loading, error, data, refetch } = useTokenPrice({
ticker: `${unit}USDT`,
});
const { price, change } = formatResult(loading, error, data);
const { price, change } = formatTokenPrice(loading, error, data);

const usdFormatter = new Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'USD',
});
// Refetch token price if online status changes to online.
const handleOnlineStatus = (e: Event): void => {
if (isCustomEvent(e)) {
if (e.detail.online) {
refetch();
}
}
};

// Initiate interval to refetch token price every 30 seconds.
useEffectIgnoreInitial(() => {
Expand All @@ -34,6 +42,12 @@ export const TokenPriceInner = () => {
return () => clearInterval(interval);
}, [refetch]);

useEventListener(
'online-status',
handleOnlineStatus,
useRef<Document>(document)
);

return (
<>
<div className="stat">
Expand All @@ -45,7 +59,11 @@ export const TokenPriceInner = () => {
</span>
</div>
<div className="stat">
1 {unit} / {usdFormatter.format(price)}
1 {unit} /{' '}
{new Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'USD',
}).format(price)}
</div>
</>
);
Expand Down
7 changes: 5 additions & 2 deletions packages/app/src/library/Offline/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@ export const Offline = () => {
OnlineStatusController.initOnlineEvents();
}, []);

const documentRef = useRef<Document>(document);
useEventListener('online-status', handleOnlineStatus, documentRef);
useEventListener(
'online-status',
handleOnlineStatus,
useRef<Document>(document)
);

if (!offline) {
return null;
Expand Down
4 changes: 2 additions & 2 deletions packages/app/src/pages/Overview/AccountBalance/FiatValue.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { useNetwork } from 'contexts/Network';
import {
ApolloProvider,
client,
formatResult,
formatTokenPrice,
useTokenPrice,
} from 'plugin-staking-api';

Expand All @@ -24,7 +24,7 @@ export const FiatValueInner = ({ totalBalance }: FiatValueProps) => {
const { loading, error, data, refetch } = useTokenPrice({
ticker: `${unit}USDT`,
});
const { price } = formatResult(loading, error, data);
const { price } = formatTokenPrice(loading, error, data);

// Convert balance to fiat value.
const freeFiat = totalBalance.multipliedBy(
Expand Down
1 change: 0 additions & 1 deletion packages/plugin-staking-api/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@ import { ApolloProvider } from '@apollo/client';

export * from './Client';
export * from './useTokenPrice';
export * from './util';

export { ApolloProvider };
10 changes: 6 additions & 4 deletions packages/plugin-staking-api/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ import type {
OperationVariables,
} from '@apollo/client';

export interface TokenPrice {
price: number;
change: number;
}

export type TokenPriceResult = {
tokenPrice: {
price: number;
change: number;
};
tokenPrice: TokenPrice;
} | null;

export interface UseTokenPriceResult {
Expand Down
16 changes: 15 additions & 1 deletion packages/plugin-staking-api/src/useTokenPrice.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// Copyright 2024 @polkadot-cloud/polkadot-staking-dashboard authors & contributors
// SPDX-License-Identifier: GPL-3.0-only

import type { ApolloError } from '@apollo/client';
import { gql, useQuery } from '@apollo/client';
import type { UseTokenPriceResult } from './types';
import type { TokenPriceResult, UseTokenPriceResult } from './types';

const TOKEN_PRICE_QUERY = gql`
query TokenPrice($ticker: String!) {
Expand All @@ -23,3 +24,16 @@ export const useTokenPrice = ({
});
return { loading, error, data, refetch };
};

export const formatTokenPrice = (
loading: boolean,
error: ApolloError | undefined,
data: TokenPriceResult
) => {
const price =
loading || error ? 0 : Number(data?.tokenPrice?.price.toFixed(2)) || 0;
const change =
loading || error ? 0 : Number(data?.tokenPrice?.change.toFixed(2)) || 0;

return { price, change };
};
18 changes: 0 additions & 18 deletions packages/plugin-staking-api/src/util.ts

This file was deleted.

Loading