forked from Nova-reward/Nova-Rewards
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBalanceDisplay.withError.tsx
More file actions
65 lines (59 loc) · 2.01 KB
/
Copy pathBalanceDisplay.withError.tsx
File metadata and controls
65 lines (59 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import React from 'react';
import { useDataFetching } from '@/hooks/useDataFetching';
import { WithErrorHandling } from '@/components/ErrorStates/WithErrorHandling';
import { RefreshIndicator } from './RefreshIndicator';
import { formatTokenAmount } from '@/lib/utils';
import { apiClient } from '@/lib/api-client';
interface BalanceData {
novaBalance: string;
usdValue: string;
stakedBalance?: string;
pendingRewards?: string;
}
const fetchBalance = async (): Promise<BalanceData> => {
const response = await apiClient.get('/api/balance');
return {
novaBalance: response.data.novaBalance,
usdValue: response.data.usdValue,
stakedBalance: response.data.stakedBalance,
pendingRewards: response.data.pendingRewards,
};
};
export const BalanceDisplayWithErrorHandling: React.FC = () => {
const { data: balance, isLoading, error, isRefreshing, refetch } = useDataFetching({
fetchFn: fetchBalance,
});
if (!balance && isLoading) {
return (
<div className="animate-pulse">
<div className="h-8 w-32 rounded bg-gray-200 dark:bg-gray-700" />
</div>
);
}
return (
<WithErrorHandling
isLoading={isLoading && !balance}
error={error}
onRetry={refetch}
>
{balance && (
<div className="space-y-2">
<div className="flex items-baseline gap-2">
<span className="text-2xl font-bold text-gray-900 dark:text-white">
{formatTokenAmount(balance.novaBalance)} NOVA
</span>
<span className="text-sm text-gray-500">≈ ${balance.usdValue}</span>
</div>
<div className="flex items-center gap-4 text-sm">
{balance.stakedBalance && (
<div className="text-gray-600 dark:text-gray-400">
Staked: {formatTokenAmount(balance.stakedBalance)} NOVA
</div>
)}
<RefreshIndicator isRefreshing={isRefreshing} lastUpdated={new Date()} />
</div>
</div>
)}
</WithErrorHandling>
);
};