1- import { ChevronRight } from 'lucide-react'
1+ import { ChevronFirst , ChevronLeft , ChevronRight } from 'lucide-react'
22import { useEffect , useState } from 'react'
33import { useTranslation } from 'react-i18next'
44import { Link , useParams , useSearchParams } from 'react-router'
55import { Spinner } from '@/components/Spinner'
66import { Button } from '@/components/ui/button'
77import { FriendIdDisplay } from '@/components/ui/friend-id-display'
8+ import { Switch } from '@/components/ui/switch'
89import { getCardByInternalId , tradeableExpansions } from '@/lib/CardsDB.ts'
910import { getExtraCards , getNeededCards } from '@/lib/utils'
1011import { CardList } from '@/pages/trade/components/CardList.tsx'
1112import { TradeOffer } from '@/pages/trade/components/TradeOffer.tsx'
1213import { useAccount , usePublicAccount } from '@/services/account/useAccount'
1314import { useCollection , usePublicCollection } from '@/services/collection/useCollection'
15+ import { useAllTrades } from '@/services/trade/useTrade'
1416import { type Card , type CollectionRow , type Rarity , type TradableRarity , tradableRarities } from '@/types'
17+ import TradeList from './components/TradeList'
1518
1619function getTradeCards ( extraCards : number [ ] , neededCards : number [ ] ) {
1720 const neededCardsSet = new Set ( neededCards )
@@ -33,6 +36,10 @@ function TradeWith() {
3336 const { data : account } = useAccount ( )
3437 const { data : ownedCards = new Map < number , CollectionRow > ( ) } = useCollection ( )
3538
39+ const [ viewHistory , setViewHistory ] = useState ( false )
40+ const [ pageHistory , setPageHistory ] = useState ( 0 )
41+ const allTrades = useAllTrades ( friendAccount ?. friend_id , viewHistory , pageHistory , true )
42+
3643 const [ yourCard , setYourCard ] = useState < Card | null > ( null )
3744 const [ friendCard , setFriendCard ] = useState < Card | null > ( ( ) => {
3845 const id = searchParams . get ( 'friend_card' )
@@ -91,23 +98,60 @@ function TradeWith() {
9198 const hasPossibleTrades = tradableRarities . some ( ( r ) => ( userTrades [ r ] ?? [ ] ) . length > 0 && ( friendTrades [ r ] ?? [ ] ) . length > 0 )
9299
93100 return (
94- < div className = "kap-4 justify-center w-full m-auto px-1 sm:px-2" >
101+ < div className = "flex flex-col gap-4 w-full px-1 sm:px-2" >
95102 < title > { `Trade with ${ friendAccount . username } – TCG Pocket Collection Tracker` } </ title >
96- < div className = "mb-4 mx-1 flex justify-between" >
103+ < div className = "mx-1 flex justify-between" >
97104 < h1 >
98105 < span className = "text-2xl font-light" > { t ( 'tradingWith' ) } </ span >
99106 < span className = "text-2xl font-bold" > { friendAccount . username } </ span >
100107 < span className = "block sm:inline text-sm" >
101108 < FriendIdDisplay friendId = { friendAccount . friend_id } />
102109 </ span >
103110 </ h1 >
104- < Link to = { `/collection/${ friendId } ` } >
105- < Button >
106- Collection
107- < ChevronRight />
108- </ Button >
109- </ Link >
111+ < div className = "flex flex-col-reverse sm:flex-row gap-2" >
112+ < label htmlFor = { `history-${ friendId } ` } className = "my-auto flex items-center" >
113+ { t ( 'viewHistory' ) }
114+ < Switch
115+ id = { `history-${ friendId } ` }
116+ className = "ml-2 my-auto"
117+ checked = { viewHistory }
118+ onCheckedChange = { ( x ) => {
119+ setViewHistory ( x )
120+ setPageHistory ( 0 )
121+ } }
122+ />
123+ </ label >
124+ < Link to = { `/collection/${ friendId } ` } >
125+ < Button >
126+ Collection
127+ < ChevronRight />
128+ </ Button >
129+ </ Link >
130+ </ div >
110131 </ div >
132+ { allTrades . isLoading ? (
133+ < Spinner size = "md" />
134+ ) : (
135+ allTrades . data && (
136+ < TradeList trades = { allTrades . data . trades } >
137+ < div className = "flex justify-between mt-2" >
138+ < span className = "text-neutral-400" > { allTrades . data . count } total offers</ span >
139+ < div className = "flex items-center gap-2" >
140+ < span > Page { pageHistory + 1 } </ span >
141+ < Button variant = "outline" onClick = { ( ) => setPageHistory ( ( ) => 0 ) } disabled = { pageHistory <= 0 } >
142+ < ChevronFirst />
143+ </ Button >
144+ < Button variant = "outline" onClick = { ( ) => setPageHistory ( ( prev ) => prev - 1 ) } disabled = { pageHistory <= 0 } >
145+ < ChevronLeft />
146+ </ Button >
147+ < Button variant = "outline" onClick = { ( ) => setPageHistory ( ( prev ) => prev + 1 ) } disabled = { ! allTrades . data . hasNext } >
148+ < ChevronRight />
149+ </ Button >
150+ </ div >
151+ </ div >
152+ </ TradeList >
153+ )
154+ ) }
111155
112156 < TradeOffer
113157 yourId = { account . friend_id }
@@ -126,21 +170,21 @@ function TradeWith() {
126170 ) }
127171
128172 { tradableRarities . map ( ( rarity ) => (
129- < div key = { rarity } className = "mt-4" >
173+ < div key = { rarity } >
130174 < h3 id = { rarity } className = "text-xl font-semibold mb-2 text-center" >
131175 [ { rarity } ]
132176 </ h3 >
133- < div className = "flex flex-col sm:flex-row gap-2 sm:gap-4" >
177+ < div className = "flex flex-col-reverse sm:flex-row gap-2 sm:gap-4" >
134178 < div className = "w-full sm:w-1/2" >
135- < h4 className = "text-md font-medium mb-1 ml-2" > { t ( 'youHave' ) } </ h4 >
179+ < h4 className = "text-md font-medium mb-1 ml-2 text-neutral-400 " > { t ( 'youHave' ) } </ h4 >
136180 { userTrades [ rarity ] ? (
137181 < CardList cards = { userTrades [ rarity ] } selected = { yourCard } setSelected = { setYourCard } />
138182 ) : (
139183 < div className = "text-center text-neutral-500 rounded-lg border-1 border-neutral-700 border-solid p-2" > No cards to trade</ div >
140184 ) }
141185 </ div >
142186 < div className = "w-full sm:w-1/2" >
143- < h4 className = "text-md font-medium mb-1 ml-2" > { t ( 'friendHas' ) } </ h4 >
187+ < h4 className = "text-md font-medium mb-1 ml-2 text-neutral-400 " > { t ( 'friendHas' ) } </ h4 >
144188 { friendTrades [ rarity ] ? (
145189 < CardList cards = { friendTrades [ rarity ] } selected = { friendCard } setSelected = { setFriendCard } />
146190 ) : (
0 commit comments