11"use client" ;
22
3+ import { CUSTOMER_PAGE_EVENTS_LIMIT } from "@/lib/partners/constants" ;
34import useCustomer from "@/lib/swr/use-customer" ;
45import useWorkspace from "@/lib/swr/use-workspace" ;
56import {
@@ -165,7 +166,7 @@ const SalesTable = memo(({ customerId }: { customerId: string }) => {
165166 const { id : workspaceId , slug } = useWorkspace ( ) ;
166167
167168 const { data : salesData , isLoading : isSalesLoading } = useSWR < SaleEvent [ ] > (
168- `/api/events?event=sales&interval=all&limit=8 &customerId=${ customerId } &workspaceId=${ workspaceId } ` ,
169+ `/api/events?event=sales&interval=all&limit=${ CUSTOMER_PAGE_EVENTS_LIMIT } &customerId=${ customerId } &workspaceId=${ workspaceId } ` ,
169170 fetcher ,
170171 {
171172 keepPreviousData : true ,
@@ -175,7 +176,9 @@ const SalesTable = memo(({ customerId }: { customerId: string }) => {
175176 const { data : totalSales , isLoading : isTotalSalesLoading } = useSWR < {
176177 sales : number ;
177178 } > (
178- `/api/analytics?event=sales&interval=all&groupBy=count&customerId=${ customerId } &workspaceId=${ workspaceId } ` ,
179+ // Only fetch total sales count if the sales data is equal to the limit
180+ salesData ?. length === CUSTOMER_PAGE_EVENTS_LIMIT &&
181+ `/api/analytics?event=sales&interval=all&groupBy=count&customerId=${ customerId } &workspaceId=${ workspaceId } ` ,
179182 fetcher ,
180183 {
181184 keepPreviousData : true ,
@@ -185,9 +188,11 @@ const SalesTable = memo(({ customerId }: { customerId: string }) => {
185188 return (
186189 < CustomerSalesTable
187190 sales = { salesData }
188- totalSales = { totalSales ?. sales }
191+ totalSales = {
192+ isTotalSalesLoading ? undefined : totalSales ?. sales ?? salesData ?. length
193+ }
189194 viewAllHref = { `/${ slug } /events?event=sales&interval=all&customerId=${ customerId } ` }
190- isLoading = { isSalesLoading || isTotalSalesLoading }
195+ isLoading = { isSalesLoading }
191196 />
192197 ) ;
193198} ) ;
@@ -199,22 +204,28 @@ const PartnerEarningsTable = memo(
199204 const { data : commissions , isLoading : isComissionsLoading } = useSWR <
200205 CommissionResponse [ ]
201206 > (
202- `/api/programs/${ programId } /commissions?customerId=${ customerId } &workspaceId=${ workspaceId } &pageSize=8 ` ,
207+ `/api/programs/${ programId } /commissions?customerId=${ customerId } &workspaceId=${ workspaceId } &pageSize=${ CUSTOMER_PAGE_EVENTS_LIMIT } ` ,
203208 fetcher ,
204209 ) ;
205210
206211 const { data : totalCommissions , isLoading : isTotalCommissionsLoading } =
207212 useSWR < { all : { count : number } } > (
208- `/api/programs/${ programId } /commissions/count?customerId=${ customerId } &workspaceId=${ workspaceId } ` ,
213+ // Only fetch total earnings count if the earnings data is equal to the limit
214+ commissions ?. length === CUSTOMER_PAGE_EVENTS_LIMIT &&
215+ `/api/programs/${ programId } /commissions/count?customerId=${ customerId } &workspaceId=${ workspaceId } ` ,
209216 fetcher ,
210217 ) ;
211218
212219 return (
213220 < CustomerPartnerEarningsTable
214221 commissions = { commissions }
215- totalCommissions = { totalCommissions ?. all ?. count }
222+ totalCommissions = {
223+ isTotalCommissionsLoading
224+ ? undefined
225+ : totalCommissions ?. all ?. count ?? commissions ?. length
226+ }
216227 viewAllHref = { `/${ slug } /programs/${ programId } /commissions?customerId=${ customerId } ` }
217- isLoading = { isComissionsLoading || isTotalCommissionsLoading }
228+ isLoading = { isComissionsLoading }
218229 />
219230 ) ;
220231 } ,
0 commit comments