Skip to content

Commit

Permalink
feat(SPV-1139): add user balance
Browse files Browse the repository at this point in the history
  • Loading branch information
dzolt-4chain committed Nov 4, 2024
1 parent a130712 commit 081da3a
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/components/UserBalance/UserBalance.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { useSpvWalletClient } from '@/contexts';
import { userBalanceQueryOptions } from '@/utils/userBalanceQueryOptions';
import { useSuspenseQuery } from '@tanstack/react-query';
import { Badge } from '../ui';
import { WalletIcon } from 'lucide-react';

export const UserBalance = () => {
const { spvWalletClient } = useSpvWalletClient();

const { data: balance } = useSuspenseQuery(
userBalanceQueryOptions({
spvWalletClient: spvWalletClient!,
}),
);

return (
<>
{balance ? (
<Badge variant="outline" className="p-2 pl-3 pr-3 flex justify-center items-center gap-2">
<WalletIcon size="16px" />
{balance.toLocaleString()} SATS
</Badge>
) : null}
</>
);
};
1 change: 1 addition & 0 deletions src/components/UserBalance/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './UserBalance.tsx';
2 changes: 2 additions & 0 deletions src/routes/user/_user.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { useEffect, useState } from 'react';

import { Logo, ModeToggle, Profile, Sheet, Tooltip, TooltipContent, TooltipTrigger } from '@/components';
import { PageRefreshButton } from '@/components/PageRefreshButton';
import { UserBalance } from '@/components/UserBalance';

export const Route = createFileRoute('/user/_user')({
beforeLoad: ({ context, location }) => {
Expand Down Expand Up @@ -73,6 +74,7 @@ function LayoutComponent() {
</Sheet>
<div className="ml-auto flex items-center gap-4">
<PageRefreshButton />
<UserBalance />
<ModeToggle />
<Profile />
</div>
Expand Down
16 changes: 16 additions & 0 deletions src/utils/userBalanceQueryOptions.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { SpvWalletClientExtended } from '@/contexts';
import { queryOptions } from '@tanstack/react-query';

export interface UserBalanceQueryOptions {
spvWalletClient: SpvWalletClientExtended;
}

export const userBalanceQueryOptions = (opts: UserBalanceQueryOptions) => {
return queryOptions({
queryKey: ['balance', opts],
queryFn: async () => {
const user = await opts.spvWalletClient.GetUserInfo();
return user.currentBalance;
},
});
};

0 comments on commit 081da3a

Please sign in to comment.