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

fix: transaction empty state #43

Merged
merged 4 commits into from
Aug 26, 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
6 changes: 5 additions & 1 deletion hooks/useBalance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,9 @@ const fetcher = (...args: FetchArgs) => {

export function useBalance() {
const nwcClient = useAppStore((store) => store.nwcClient);
return useSWR(nwcClient && "getBalance", fetcher);
const selectedWalletId = useAppStore((store) => store.selectedWalletId);
return useSWR(
nwcClient && `getBalance?selectedWalletId=${selectedWalletId}`,
fetcher,
);
}
13 changes: 7 additions & 6 deletions hooks/useTransactions.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import {
Nip47GetBalanceResponse,
Nip47ListTransactionsResponse,
} from "@getalby/sdk/dist/NWCClient";
import { useAppStore } from "lib/state/appStore";
import useSWR, { BareFetcher } from "swr";
import useSWR from "swr";
import { TRANSACTIONS_PAGE_SIZE } from "~/lib/constants";

type FetchArgs = Parameters<typeof fetch>;
Expand All @@ -25,5 +21,10 @@ const fetcher = (...args: FetchArgs) => {

export function useTransactions(page = 1) {
const nwcClient = useAppStore((store) => store.nwcClient);
return useSWR(nwcClient && `listTransactions?page=${page}`, fetcher);
const selectedWalletId = useAppStore((store) => store.selectedWalletId);
return useSWR(
nwcClient &&
`listTransactions?page=${page}&selectedWalletId=${selectedWalletId}`,
fetcher,
);
}
37 changes: 23 additions & 14 deletions pages/Transactions.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
import { Nip47Transaction } from "@getalby/sdk/dist/NWCClient";
import dayjs from "dayjs";
import {
Link,
router,
Stack,
useFocusEffect,
useLocalSearchParams,
} from "expo-router";
import { Link, router, Stack } from "expo-router";
import React from "react";
import {
FlatList,
Expand All @@ -16,9 +10,9 @@ import {
View,
} from "react-native";
import { MoveDownLeft, MoveUpRight, X } from "~/components/Icons";
import { Button } from "~/components/ui/button";
import { Skeleton } from "~/components/ui/skeleton";
import { Text } from "~/components/ui/text";
import { useBalance } from "~/hooks/useBalance";
import { useGetFiatAmount } from "~/hooks/useGetFiatAmount";
import { useTransactions } from "~/hooks/useTransactions";
import { TRANSACTIONS_PAGE_SIZE } from "~/lib/constants";
Expand All @@ -29,6 +23,7 @@ export function Transactions() {
const { data: transactions, mutate: reloadTransactions } =
useTransactions(page);
const [loadingNextPage, setLoadingNextPage] = React.useState(false);
const [transactionsLoaded, setTransactionsLoaded] = React.useState(false);
const [allTransactions, setAllTransactions] = React.useState<
Nip47Transaction[]
>([]);
Expand All @@ -49,6 +44,10 @@ export function Transactions() {
setAllTransactions([...allTransactions, ...transactions.transactions]);
setLoadingNextPage(false);
}

if (transactions) {
setTransactionsLoaded(true);
}
}, [allTransactions, transactions, refreshingTransactions]);

const onRefresh = React.useCallback(() => {
Expand Down Expand Up @@ -99,7 +98,7 @@ export function Transactions() {
) : undefined
}
data={allTransactions}
onEndReachedThreshold={0.9}
onEndReachedThreshold={0.8}
onEndReached={() => {
if (
!refreshingTransactions &&
Expand Down Expand Up @@ -165,7 +164,7 @@ export function Transactions() {
</Pressable>
)}
/>
) : !allTransactions ? (
) : !transactionsLoaded ? (
<ScrollView className="mt-3">
{[...Array(20)].map((e, i) => (
<View
Expand All @@ -183,11 +182,21 @@ export function Transactions() {
</View>
))}
</ScrollView>
) :
<View className="p-6">
<Text>No transactions yet.</Text>
) : (
<View className="p-6 flex-1 items-center justify-center gap-5">
<View className="flex flex-col items-center">
<Text className="text-2xl font-semibold2">No transactions yet</Text>
<Text className="text-muted-foreground text-xl text-center">
Top up your wallet to get started
</Text>
</View>
<Link href="/receive" asChild>
<Button size="lg">
<Text>Receive</Text>
</Button>
</Link>
</View>
}
)}
</View>
);
}