11import { useMutation , useQuery , useQueryClient } from '@tanstack/react-query'
2+ import { supabase } from '@/lib/supabase'
23import { useAccount } from '@/services/account/useAccount.ts'
3- import { getTrades , getTradingPartners , insertTrade , updateTrade } from '@/services/trade/tradeService.ts'
4+ import { getActiveTrades , getTradingPartners , insertTrade , updateTrade } from '@/services/trade/tradeService.ts'
45import type { TradeRow } from '@/types'
56
6- export function useTrades ( ) {
7+ export function useActiveTrades ( ) {
8+ const { data : account } = useAccount ( )
9+ const friendId = account ?. friend_id
710 return useQuery ( {
8- queryKey : [ 'trade' ] ,
9- queryFn : getTrades ,
11+ queryKey : [ 'trades' ] ,
12+ queryFn : ( ) => getActiveTrades ( friendId as string ) ,
13+ enabled : Boolean ( friendId ) ,
14+ throwOnError : true ,
1015 } )
1116}
1217
1318export function useTradingPartners ( enabled : boolean , cardId : number | undefined ) {
1419 const { data : account } = useAccount ( )
15-
1620 return useQuery ( {
1721 queryKey : [ 'trading-partners' , cardId ] ,
1822 queryFn : ( ) => getTradingPartners ( account ?. email as string , cardId ) ,
@@ -27,9 +31,7 @@ export function useInsertTrade() {
2731
2832 return useMutation ( {
2933 mutationFn : ( trade : TradeRow ) => insertTrade ( trade ) ,
30- onSuccess : async ( ) => {
31- await queryClient . invalidateQueries ( { queryKey : [ 'trade' ] } )
32- } ,
34+ onSuccess : ( ) => queryClient . invalidateQueries ( { queryKey : [ 'trades' ] } ) ,
3335 } )
3436}
3537
@@ -38,52 +40,30 @@ export function useUpdateTrade() {
3840
3941 return useMutation ( {
4042 mutationFn : ( { id, trade } : { id : number ; trade : Partial < TradeRow > } ) => updateTrade ( id , trade ) ,
41- onSuccess : async ( ) => {
42- await queryClient . invalidateQueries ( { queryKey : [ 'trade' ] } )
43- await queryClient . invalidateQueries ( { queryKey : [ 'actionableTradeCount' ] } )
44- } ,
43+ onSuccess : ( ) => queryClient . invalidateQueries ( { queryKey : [ 'trades' ] } ) ,
4544 } )
4645}
4746
4847export function useActionableTradeCount ( ) {
49- const { data : trades } = useTrades ( )
5048 const { data : account } = useAccount ( )
5149
5250 return useQuery ( {
53- queryKey : [ 'actionableTradeCount' , trades , account ?. friend_id ] ,
54- queryFn : ( ) => {
55- console . log ( 'openTrades' , trades , account )
56- if ( ! account || ! trades ) {
57- console . log ( 'no account or trades' )
58- return 0
51+ queryKey : [ 'trades' , 'new' ] ,
52+ queryFn : async ( ) => {
53+ if ( ! account ) {
54+ throw new Error ( 'Account is needed to fetch new trades' )
5955 }
60-
61- // trades where i'm the receiver but didn't accept/decline yet
62- const sentTrades = trades ?. filter ( ( t ) => t . receiving_friend_id === account ?. friend_id && t . status === 'offered' )
63- console . log ( 'sentTrades' , sentTrades )
64-
65- const nonCompletedTrades = trades ?. filter ( ( t ) => {
66- if ( t . status !== 'finished' ) {
67- return false //trade marked as completed
68- }
69-
70- if ( t . offering_friend_id === account ?. friend_id && t . offerer_ended ) {
71- return false //I initiated and also ended
72- }
73- if ( t . receiving_friend_id === account ?. friend_id && t . receiver_ended ) {
74- return false //I received and also ended
75- }
76-
77- if ( ! sentTrades ?. includes ( t ) ) {
78- return false //already counted in the sentTrades array, so don't count twice
79- }
80-
81- return true
82- } )
83-
84- console . log ( 'nonCompletedTrades' , nonCompletedTrades )
85-
86- return sentTrades ?. length + nonCompletedTrades ?. length
56+ const { count, error } = await supabase
57+ . from ( 'trades' )
58+ . select ( '*' , { count : 'exact' , head : true } )
59+ . eq ( 'status' , 'offered' )
60+ . eq ( 'receiving_friend_id' , account ?. friend_id )
61+ if ( error ) {
62+ console . log ( 'supabase error' , error )
63+ throw new Error ( 'Failed fetching new trades' )
64+ }
65+ return count as number
8766 } ,
67+ enabled : Boolean ( account ?. friend_id ) ,
8868 } )
8969}
0 commit comments