-
Notifications
You must be signed in to change notification settings - Fork 134
Expand file tree
/
Copy pathuseSales.ts
More file actions
110 lines (98 loc) · 3.33 KB
/
Copy pathuseSales.ts
File metadata and controls
110 lines (98 loc) · 3.33 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import { useCallback, useMemo } from 'react'
import { full, Id } from '@audius/sdk'
import { InfiniteData, useInfiniteQuery } from '@tanstack/react-query'
import { purchaseFromSDK } from '~/adapters/purchase'
import { useQueryContext, makeLoadNextPage } from '~/api/tan-query/utils'
import { ID } from '~/models'
import {
USDCContentPurchaseType,
USDCPurchaseDetails
} from '~/models/USDCTransactions'
import { useCollections } from '../collection/useCollections'
import { QUERY_KEYS } from '../queryKeys'
import { useTracks } from '../tracks/useTracks'
import { QueryKey, QueryOptions } from '../types'
import { useUsers } from '../users/useUsers'
import { combineQueryStatuses } from '../utils/combineQueryResults'
const PAGE_SIZE = 10
export type GetSalesListArgs = {
userId: ID | null | undefined
sortMethod?: full.GetPurchasesSortMethodEnum
sortDirection?: full.GetPurchasesSortDirectionEnum
pageSize?: number
}
export const getSalesQueryKey = ({
userId,
sortMethod,
sortDirection,
pageSize = PAGE_SIZE
}: GetSalesListArgs) =>
[
QUERY_KEYS.sales,
userId,
{ sortMethod, sortDirection, pageSize }
] as unknown as QueryKey<InfiniteData<USDCPurchaseDetails[]>>
export const useSales = (args: GetSalesListArgs, options?: QueryOptions) => {
const { userId, sortMethod, sortDirection, pageSize = PAGE_SIZE } = args
const context = useQueryContext()
const { audiusSdk } = context
const queryResult = useInfiniteQuery({
queryKey: getSalesQueryKey({ userId, sortMethod, sortDirection, pageSize }),
initialPageParam: 0,
getNextPageParam: (
lastPage: USDCPurchaseDetails[],
allPages: USDCPurchaseDetails[][]
) => {
if (lastPage.length < pageSize) return undefined
return allPages.length * pageSize
},
queryFn: async ({ pageParam }) => {
const sdk = await audiusSdk()
const { data = [] } = await sdk.users.getSales({
id: Id.parse(userId!),
userId: Id.parse(userId!),
limit: pageSize,
offset: pageParam,
sortDirection,
sortMethod
})
return data.map(purchaseFromSDK)
},
select: (data) => data.pages.flat(),
...options,
enabled: options?.enabled !== false && !!args.userId
})
const { userIdsToFetch, trackIdsToFetch, collectionIdsToFetch } = useMemo(
() => ({
userIdsToFetch: queryResult.data?.map(({ buyerUserId }) => buyerUserId),
trackIdsToFetch: queryResult.data
?.filter(
({ contentType }) => contentType === USDCContentPurchaseType.TRACK
)
.map(({ contentId }) => contentId),
collectionIdsToFetch: queryResult.data
?.filter(
({ contentType }) => contentType === USDCContentPurchaseType.ALBUM
)
.map(({ contentId }) => contentId)
}),
[queryResult.data]
)
// Call the hooks dropping results to pre-fetch the data
const usersQueryResult = useUsers(userIdsToFetch)
const tracksQueryResult = useTracks(trackIdsToFetch)
const collectionsQueryResult = useCollections(collectionIdsToFetch)
const loadNextPageCallback = useCallback(() => {
makeLoadNextPage(queryResult)
}, [queryResult])
return {
...combineQueryStatuses([
queryResult,
usersQueryResult,
tracksQueryResult,
collectionsQueryResult
]),
data: queryResult.data,
loadNextPage: loadNextPageCallback
}
}