forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseAllPayouts.ts
More file actions
71 lines (57 loc) · 1.86 KB
/
Copy pathuseAllPayouts.ts
File metadata and controls
71 lines (57 loc) · 1.86 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
66
67
68
69
70
71
'use client';
import { useState, useEffect } from 'react';
import { PayoutWithAppInfo } from '@/lib/services/omi-api/types';
import { useAuthFetch } from '@/hooks/useAuthToken';
export function useAllPayouts() {
const { fetchWithAuth, token } = useAuthFetch();
const [payouts, setPayouts] = useState<PayoutWithAppInfo[]>([]);
const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
const [hasMore, setHasMore] = useState(false);
const [totalCount, setTotalCount] = useState(0);
useEffect(() => {
if (!token) return;
const loadPayouts = async () => {
try {
setLoading(true);
setError(null);
const response = await fetchWithAuth('/api/omi/all-payouts');
if (!response.ok) {
throw new Error('Failed to fetch payouts');
}
const data = await response.json();
setPayouts(data.payouts);
setHasMore(data.hasMore);
setTotalCount(data.totalCount);
} catch (err) {
setError(err instanceof Error ? err.message : 'Failed to load payouts');
} finally {
setLoading(false);
}
};
loadPayouts();
}, [token, fetchWithAuth]);
const loadMorePayouts = async () => {
if (!token || !hasMore) return;
try {
const lastPayout = payouts[payouts.length - 1];
const response = await fetchWithAuth(`/api/omi/all-payouts?starting_after=${lastPayout.payout.id}`);
if (!response.ok) {
throw new Error('Failed to load more payouts');
}
const data = await response.json();
setPayouts((prev) => [...prev, ...data.payouts]);
setHasMore(data.hasMore);
} catch (err) {
setError(err instanceof Error ? err.message : 'Failed to load more payouts');
}
};
return {
payouts,
loading,
error,
hasMore,
totalCount,
loadMorePayouts,
};
}