Skip to content

Commit db57e9b

Browse files
Ni-2alsakhaev
authored andcommitted
feat: Display total reward amount in the app (issue#233)
1 parent 2c91238 commit db57e9b

File tree

2 files changed

+89
-10
lines changed

2 files changed

+89
-10
lines changed

apps/xen-tg-app/src/components/Wallet.tsx

Lines changed: 84 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
import { API_URL } from '@/env'
2+
import { formatNearAmount } from '@/lib/utils'
23
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
4+
import { useMemo } from 'react'
35
import LogOutIcon from '../assets/log-out'
46
import NEAR_ICON from '../assets/near-gray.svg'
5-
import { Balance, TXenUser } from '../types'
7+
import { Balance, RewardAmount, TXenUser } from '../types'
68
import HistoryMainPageModule from './HistoryMainPageModule'
79
import Spinner from './Spinner'
810

11+
const WALLET_URL = 'https://app.mynearwallet.com/' // ToDo: hardcode
12+
913
const queryFn = (name: string, isLoggedIn?: boolean) => async () => {
1014
if (isLoggedIn === false) return
1115
if (!window.Telegram.WebApp.initData) {
@@ -69,6 +73,11 @@ const Wallet = () => {
6973
queryFn: queryFn('getBalance', isLoggedIn),
7074
})
7175

76+
const { data: rewardAmount } = useQuery<RewardAmount>({
77+
queryKey: ['reward-amount'],
78+
queryFn: queryFn('getRewardAmount'),
79+
})
80+
7281
const handleLogout = useMutation({
7382
mutationFn: mutationFn('logout'),
7483
onSuccess: () => {
@@ -85,28 +94,60 @@ const Wallet = () => {
8594
},
8695
})
8796

97+
const amountInNearRounded = useMemo(() => {
98+
if (!rewardAmount?.total) return null
99+
const amountInNear = formatNearAmount(rewardAmount.total)
100+
return amountInNear.slice(0, amountInNear.indexOf('.') + 4)
101+
}, [rewardAmount?.total])
102+
103+
const availableToClaimRounded = useMemo(() => {
104+
if (!rewardAmount?.availableToClaim) return null
105+
const amountInNear = formatNearAmount(rewardAmount.availableToClaim)
106+
return amountInNear.slice(0, amountInNear.indexOf('.') + 4)
107+
}, [rewardAmount?.availableToClaim])
108+
88109
return user && isLoggedIn ? (
89-
<div className="z-1 flex w-full flex-wrap items-center justify-between gap-2.5 overflow-hidden rounded-xl border border-(--color-opposite-text) p-2.5 backdrop-blur-3xl backdrop-opacity-80 dark:border-(--color-main-text)/30">
90-
<div className="flex shrink-0 justify-between gap-3 text-[22px]/[150%] font-semibold">
91-
<img src={NEAR_ICON} alt="near" />
92-
{balance?.formatted.available ?? '-'}
93-
</div>
94-
<div className="me-1 flex items-center justify-between gap-3 text-[22px]/[150%] font-normal wrap-anywhere">
110+
<div className="z-1 flex w-full flex-col gap-1 overflow-hidden rounded-xl border border-(--color-opposite-text) p-2.5 backdrop-blur-3xl backdrop-opacity-80 dark:border-(--color-main-text)/30">
111+
<div className="ms-5 me-1 flex items-center justify-between gap-3 py-0.5 text-[22px]/7 font-normal wrap-anywhere">
95112
<a
96-
href="https://app.mynearwallet.com/"
113+
href={WALLET_URL}
97114
target="_blank"
98115
rel="noreferrer"
99116
className="cursor-pointer underline decoration-(--color-current-mix-50) underline-offset-[3px] transition hover:decoration-(--color-current-mix-20) focus:decoration-(--color-current-mix-20)"
100117
>
101118
{user.nearAccountId}
102119
</a>
103120
<button
104-
className="flex cursor-pointer p-1.5 text-(--color-gray-text) transition hover:text-(--color-main-text)"
121+
className="flex cursor-pointer p-1 text-(--color-gray-text) transition hover:text-(--color-main-text)"
105122
onClick={() => handleLogout.mutate()}
106123
>
107124
<LogOutIcon />
108125
</button>
109126
</div>
127+
<div className="flex shrink-0 flex-wrap items-center justify-between gap-x-2.5 gap-y-0">
128+
<div className="flex shrink-0 gap-2 text-[22px]/normal font-semibold">
129+
<img src={NEAR_ICON} alt="near" />
130+
{balance?.formatted.available ?? '-'}
131+
</div>
132+
<div className="flex shrink-0 flex-col text-xs/tight font-semibold text-(--color-gray-text)">
133+
<div className="flex gap-1">
134+
Total revenue:{' '}
135+
<span className="text-(--color-my-primary)">
136+
{amountInNearRounded && amountInNearRounded !== '0' ? `+${amountInNearRounded}` : '0'}
137+
</span>{' '}
138+
<img className="w-2.5" src={NEAR_ICON} alt="near" />
139+
</div>
140+
<div className="flex gap-1">
141+
Available to claim:{' '}
142+
<span className="text-(--color-my-primary)">
143+
{availableToClaimRounded && availableToClaimRounded !== '0'
144+
? `+${availableToClaimRounded}`
145+
: '0'}
146+
</span>{' '}
147+
<img className="w-2.5" src={NEAR_ICON} alt="near" />
148+
</div>
149+
</div>
150+
</div>
110151
<HistoryMainPageModule />
111152
</div>
112153
) : (
@@ -115,7 +156,40 @@ const Wallet = () => {
115156
<Spinner />
116157
) : (
117158
<div className="flex w-full items-center justify-between">
118-
<div className="text-[18px]/[150%] font-semibold">No wallet connected</div>
159+
<div className="flex shrink-0 flex-col gap-1 text-xs/tight font-semibold text-(--color-gray-text)">
160+
{availableToClaimRounded && availableToClaimRounded !== '0' ? (
161+
<>
162+
<div className="flex flex-col text-sm text-(--color-main-text)">
163+
<p>Available to claim:</p>
164+
<div className="flex gap-1 text-2xl/tight">
165+
<p className="text-(--color-my-primary)">{`+${availableToClaimRounded}`}</p>
166+
<img className="w-4" src={NEAR_ICON} alt="near" />
167+
</div>
168+
</div>
169+
<div className="flex gap-1">
170+
Total revenue:{' '}
171+
<span className="text-(--color-my-primary)">
172+
{amountInNearRounded && amountInNearRounded !== '0'
173+
? `+${amountInNearRounded}`
174+
: '0'}
175+
</span>{' '}
176+
<img className="w-2.5" src={NEAR_ICON} alt="near" />
177+
</div>
178+
</>
179+
) : (
180+
<div className="flex flex-col text-sm text-(--color-main-text)">
181+
<p>Total revenue:</p>
182+
<div className="flex gap-1 text-2xl/tight">
183+
<p className="text-(--color-my-primary)">
184+
{amountInNearRounded || amountInNearRounded !== '0'
185+
? `+${amountInNearRounded}`
186+
: '0'}
187+
</p>
188+
<img className="w-4" src={NEAR_ICON} alt="near" />
189+
</div>
190+
</div>
191+
)}
192+
</div>
119193
<button
120194
className="flex w-[118px] cursor-pointer flex-nowrap items-center justify-center rounded-xl bg-(--color-my-primary) py-2 text-(--color-opposite-text) dark:bg-[#f8f9ff] dark:text-(--color-opposite-text)"
121195
onClick={() => handleLogin.mutate()}

apps/xen-tg-app/src/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,8 @@ export type THistoryNote = {
6666
createdAt: string
6767
isFree?: boolean
6868
}
69+
70+
export type RewardAmount = {
71+
total: string
72+
availableToClaim: string
73+
}

0 commit comments

Comments
 (0)