Skip to content

Commit 81a36fc

Browse files
committed
feat: add recent activity items
1 parent c2f20df commit 81a36fc

File tree

4 files changed

+89
-1
lines changed

4 files changed

+89
-1
lines changed

packages/core/src/api/wallet.ts

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { faker } from "@faker-js/faker";
2+
import { Wallet } from "../types/wallet";
3+
4+
async function getWalletsRecentActivity(userId: string): Promise<Wallet[]> {
5+
await new Promise(resolve => setTimeout(resolve, 100));
6+
7+
return Array.from({ length: 4 }, () => {
8+
const status = getRandomStatus();
9+
return {
10+
id: faker.string.uuid(),
11+
name: faker.internet.username(),
12+
balance: getRandomBalance(status),
13+
createdAt: faker.date.past(),
14+
logo: faker.image.url(),
15+
status,
16+
};
17+
});
18+
}
19+
20+
const wallets = Array.from({ length: 10 }, () => {
21+
const status = getRandomStatus();
22+
return {
23+
id: faker.string.uuid(),
24+
name: faker.internet.username(),
25+
balance: getRandomBalance(status),
26+
createdAt: faker.date.past(),
27+
logo: faker.image.url(),
28+
status,
29+
};
30+
});
31+
32+
function getRandomStatus(): "Received" | "Pending" | "Sent" {
33+
const statuses: ("Received" | "Pending" | "Sent")[] = [
34+
"Received",
35+
"Pending",
36+
"Sent",
37+
];
38+
return statuses[Math.floor(Math.random() * statuses.length)];
39+
}
40+
41+
function getRandomBalance(status: "Received" | "Pending" | "Sent"): string {
42+
const balance = faker.number.int({ min: 100, max: 300 });
43+
44+
if (status === "Pending") {
45+
return `${balance}`;
46+
}
47+
48+
return status === "Sent" ? `-${balance}` : `+${balance}`;
49+
}
50+
51+
async function getWalletByKeyword(keyword: string): Promise<Wallet[]> {
52+
await new Promise(resolve => setTimeout(resolve, 100));
53+
54+
return wallets.filter(option =>
55+
option.name.toLowerCase().includes(keyword.toLowerCase()),
56+
);
57+
}
58+
59+
export { getWalletsRecentActivity, getWalletByKeyword };

packages/core/src/hooks/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from "./useSession";
2-
export * from "./logout";
2+
// export * from "./logout";
3+
export * from "./wallet";

packages/core/src/hooks/wallet.ts

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { useEffect, useState } from "react";
2+
import * as wallets from "../api/wallet";
3+
import { Wallet } from "../types/wallet";
4+
5+
export default function useWalletList(userId: string | undefined) {
6+
const [list, setList] = useState<Wallet[]>([]);
7+
const [isWalletLoading, setIsWalletLoading] = useState(true);
8+
9+
useEffect(() => {
10+
async function load() {
11+
setIsWalletLoading(true);
12+
const ws = await wallets.getWalletsRecentActivity(userId || "");
13+
setIsWalletLoading(false);
14+
setList(ws);
15+
}
16+
load();
17+
}, []);
18+
19+
return { list, isWalletLoading };
20+
}

packages/core/src/types/wallet.ts

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export type Wallet = {
2+
id: string;
3+
name: string;
4+
logo: string;
5+
balance: number;
6+
createdAt: Date;
7+
status: string;
8+
};

0 commit comments

Comments
 (0)