23 Commits

Author SHA1 Message Date
Matthieu
2acf50e46f add trade validation to prevent accepting invalid trades and update UI accordingly 2025-11-27 15:34:37 +01:00
Matthieu
1d77b6fb8e deduplicate cards in collection updates for Collection and Community views + async stuff 2025-11-27 15:07:43 +01:00
Matthieu
239a2c9591 add realtime updates for collection total value in Collection and Community views 2025-11-27 14:20:37 +01:00
Matthieu
64c48da05a refactor getCollectionTotalValue to use pre-calculated value from user profile 2025-11-27 14:15:47 +01:00
Matthieu
2d7641cc20 add total collection value calculation and loading state in Collection and Community views 2025-11-27 11:56:36 +01:00
Matthieu
24023570c7 add price tracking to collections and implement total value calculation 2025-11-27 11:56:31 +01:00
Matthieu
359cc61115 implement paginated user collection API and infinite scroll in collection views 2025-11-27 11:47:19 +01:00
Matthieu
71891a29be add card face toggling and hover preview functionality in community view 2025-11-27 11:38:04 +01:00
613db069b8 Merge pull request 'feature/trade-fix' (#13) from feature/trade-fix into master
Reviewed-on: #13
2025-11-27 11:28:02 +01:00
Matthieu
1183f0c7f6 add friend and request filtering in community view 2025-11-27 11:27:04 +01:00
d1728546b1 truc async 2025-11-26 22:12:34 +01:00
9f5dab94af fix trade service 2025-11-26 19:24:01 +01:00
89fc4a782c update trade system 2025-11-26 19:12:07 +01:00
Matthieu
abbe68888d add trade editing functionality and version history tracking 2025-11-26 15:34:41 +01:00
Matthieu
8f064d4336 add price display and message preview in trade creator 2025-11-26 14:37:16 +01:00
Matthieu
a005df9965 add trade detail view and counter offer functionality 2025-11-26 14:25:50 +01:00
Matthieu
7eb893ac63 add hover source tracking for card preview in deck manager 2025-11-26 14:00:56 +01:00
Matthieu
8d0ce534f8 add commander color identity validation and improve deck validation logic 2025-11-26 13:51:26 +01:00
8671745351 opti style 2025-11-25 19:14:39 +01:00
70e7db0bac optimization api calls, update models 2025-11-25 19:00:26 +01:00
4a28f5f1ec ui improvement + fix search card in deck manager that leaded to a crash when card was found 2025-11-25 18:38:09 +01:00
7e1cd5f9bd improve suggest mana and add rule color identity commander 2025-11-25 17:39:35 +01:00
b77cd48013 improve collection and deck manager 2025-11-25 17:23:04 +01:00
29 changed files with 3212 additions and 483 deletions

View File

@@ -1,6 +1,15 @@
{ {
"permissions": {
"allow": [
"mcp__supabase__apply_migration",
"mcp__supabase__list_tables",
"mcp__supabase__execute_sql",
"Bash(npm run build:*)",
"mcp__supabase__get_advisors"
]
},
"enableAllProjectMcpServers": true,
"enabledMcpjsonServers": [ "enabledMcpjsonServers": [
"supabase" "supabase"
], ]
"enableAllProjectMcpServers": true
} }

View File

@@ -82,7 +82,7 @@ define(['./workbox-ca84f546'], (function (workbox) { 'use strict';
"revision": "3ca0b8505b4bec776b69afdba2768812" "revision": "3ca0b8505b4bec776b69afdba2768812"
}, { }, {
"url": "index.html", "url": "index.html",
"revision": "0.obrcsn1e2cs" "revision": "0.vigoqq958cg"
}], {}); }], {});
workbox.cleanupOutdatedCaches(); workbox.cleanupOutdatedCaches();
workbox.registerRoute(new workbox.NavigationRoute(workbox.createHandlerBoundToURL("index.html"), { workbox.registerRoute(new workbox.NavigationRoute(workbox.createHandlerBoundToURL("index.html"), {

View File

@@ -40,7 +40,7 @@ function AppContent() {
switch (currentPage) { switch (currentPage) {
case 'home': case 'home':
return ( return (
<div className="bg-gray-900 text-white p-3 sm:p-6 animate-fade-in"> <div className="relative bg-gray-900 text-white p-3 sm:p-6 animate-fade-in md:min-h-screen">
<div className="max-w-7xl mx-auto"> <div className="max-w-7xl mx-auto">
<h1 className="text-2xl md:text-3xl font-bold mb-4 md:mb-6 animate-slide-in-left">My Decks</h1> <h1 className="text-2xl md:text-3xl font-bold mb-4 md:mb-6 animate-slide-in-left">My Decks</h1>
<DeckList <DeckList
@@ -78,10 +78,12 @@ function AppContent() {
}; };
return ( return (
<div className="min-h-screen bg-gray-900"> <div className="min-h-screen bg-gray-900 flex flex-col">
<Navigation currentPage={currentPage} setCurrentPage={setCurrentPage} /> <Navigation currentPage={currentPage} setCurrentPage={setCurrentPage} />
<main className="pt-0 pb-20 md:pt-16 md:pb-0"> <main className="relative flex-1 overflow-y-auto">
{renderPage()} <div className="relative min-h-full md:min-h-0 pt-0 md:pt-16 pb-20 md:pb-0">
{renderPage()}
</div>
</main> </main>
<PWAInstallPrompt /> <PWAInstallPrompt />
</div> </div>

View File

@@ -201,7 +201,7 @@ const CardSearch = () => {
}; };
return ( return (
<div className="bg-gray-900 text-white p-3 sm:p-6"> <div className="relative bg-gray-900 text-white p-3 sm:p-6 md:min-h-screen">
<div className="max-w-7xl mx-auto"> <div className="max-w-7xl mx-auto">
<h1 className="text-2xl md:text-3xl font-bold mb-4 md:mb-6">Card Search</h1> <h1 className="text-2xl md:text-3xl font-bold mb-4 md:mb-6">Card Search</h1>
<form onSubmit={handleSearch} className="mb-8 space-y-4"> <form onSubmit={handleSearch} className="mb-8 space-y-4">
@@ -774,7 +774,7 @@ const CardSearch = () => {
<div <div
className={`fixed bottom-4 right-4 p-4 rounded-lg shadow-lg transition-all duration-300 ${ className={`fixed bottom-4 right-4 p-4 rounded-lg shadow-lg transition-all duration-300 ${
snackbar.type === 'success' ? 'bg-green-500' : 'bg-red-500' snackbar.type === 'success' ? 'bg-green-500' : 'bg-red-500'
} text-white z-50`} } text-white z-[140]`}
> >
<div className="flex items-center justify-between"> <div className="flex items-center justify-between">
<div className="flex items-center"> <div className="flex items-center">

View File

@@ -1,17 +1,25 @@
import React, { useState, useEffect } from 'react'; import React, { useState, useEffect, useRef, useCallback } from 'react';
import { Search, Loader2, Trash2, CheckCircle, XCircle, RefreshCw, Plus, Minus, X } from 'lucide-react'; import { Search, Loader2, Trash2, CheckCircle, XCircle, RefreshCw, Plus, Minus, X } from 'lucide-react';
import { Card } from '../types'; import { Card } from '../types';
import { getUserCollection, getCardsByIds, addCardToCollection } from '../services/api'; import { getUserCollectionPaginated, getCardsByIds, addCardToCollection, getCollectionTotalValue } from '../services/api';
import { useAuth } from '../contexts/AuthContext'; import { useAuth } from '../contexts/AuthContext';
import { supabase } from '../lib/supabase'; import { supabase } from '../lib/supabase';
import ConfirmModal from './ConfirmModal'; import ConfirmModal from './ConfirmModal';
const PAGE_SIZE = 50;
export default function Collection() { export default function Collection() {
const { user } = useAuth(); const { user } = useAuth();
const [searchQuery, setSearchQuery] = useState(''); const [searchQuery, setSearchQuery] = useState('');
const [collection, setCollection] = useState<{ card: Card; quantity: number }[]>([]); const [collection, setCollection] = useState<{ card: Card; quantity: number }[]>([]);
const [filteredCollection, setFilteredCollection] = useState<{ card: Card; quantity: number }[]>([]); const [filteredCollection, setFilteredCollection] = useState<{ card: Card; quantity: number }[]>([]);
const [isLoadingCollection, setIsLoadingCollection] = useState(true); const [isLoadingCollection, setIsLoadingCollection] = useState(true);
const [isLoadingMore, setIsLoadingMore] = useState(false);
const [hasMore, setHasMore] = useState(false);
const [offset, setOffset] = useState(0);
const [totalCount, setTotalCount] = useState(0);
const [totalCollectionValue, setTotalCollectionValue] = useState<number>(0);
const [isLoadingTotalValue, setIsLoadingTotalValue] = useState(true);
const [hoveredCard, setHoveredCard] = useState<Card | null>(null); const [hoveredCard, setHoveredCard] = useState<Card | null>(null);
const [selectedCard, setSelectedCard] = useState<{ card: Card; quantity: number } | null>(null); const [selectedCard, setSelectedCard] = useState<{ card: Card; quantity: number } | null>(null);
const [cardFaceIndex, setCardFaceIndex] = useState<Map<string, number>>(new Map()); const [cardFaceIndex, setCardFaceIndex] = useState<Map<string, number>>(new Map());
@@ -22,6 +30,7 @@ export default function Collection() {
cardId: string; cardId: string;
cardName: string; cardName: string;
}>({ isOpen: false, cardId: '', cardName: '' }); }>({ isOpen: false, cardId: '', cardName: '' });
const observerTarget = useRef<HTMLDivElement>(null);
// Helper function to check if a card has an actual back face (not adventure/split/etc) // Helper function to check if a card has an actual back face (not adventure/split/etc)
const isDoubleFaced = (card: Card) => { const isDoubleFaced = (card: Card) => {
@@ -62,6 +71,58 @@ export default function Collection() {
}); });
}; };
// Calculate total collection value (lightweight query from database)
useEffect(() => {
const calculateTotalValue = async () => {
if (!user) {
setIsLoadingTotalValue(false);
return;
}
try {
setIsLoadingTotalValue(true);
// Get total value directly from database (no need to fetch all cards!)
const totalValue = await getCollectionTotalValue(user.id);
setTotalCollectionValue(totalValue);
} catch (error) {
console.error('Error calculating total collection value:', error);
setTotalCollectionValue(0);
} finally {
setIsLoadingTotalValue(false);
}
};
calculateTotalValue();
}, [user]);
// Subscribe to realtime updates for collection total value
useEffect(() => {
if (!user) return;
const profileChannel = supabase
.channel('profile-total-value-changes')
.on(
'postgres_changes',
{
event: 'UPDATE',
schema: 'public',
table: 'profiles',
filter: `id=eq.${user.id}`,
},
(payload: any) => {
if (payload.new?.collection_total_value !== undefined) {
console.log('Collection total value updated:', payload.new.collection_total_value);
setTotalCollectionValue(payload.new.collection_total_value);
}
}
)
.subscribe();
return () => {
supabase.removeChannel(profileChannel);
};
}, [user]);
// Load user's collection from Supabase on mount // Load user's collection from Supabase on mount
useEffect(() => { useEffect(() => {
const loadCollection = async () => { const loadCollection = async () => {
@@ -72,26 +133,33 @@ export default function Collection() {
try { try {
setIsLoadingCollection(true); setIsLoadingCollection(true);
// Get collection from Supabase (returns Map<card_id, quantity>) setOffset(0);
const collectionMap = await getUserCollection(user.id); setCollection([]);
if (collectionMap.size === 0) { // Get paginated collection from Supabase
const result = await getUserCollectionPaginated(user.id, PAGE_SIZE, 0);
setTotalCount(result.totalCount);
setHasMore(result.hasMore);
if (result.items.size === 0) {
setCollection([]); setCollection([]);
setFilteredCollection([]);
return; return;
} }
// Get the actual card data from Scryfall for all cards in collection // Get the actual card data from Scryfall for all cards in this page
const cardIds = Array.from(collectionMap.keys()); const cardIds = Array.from(result.items.keys());
const cards = await getCardsByIds(cardIds); const cards = await getCardsByIds(cardIds);
// Combine card data with quantities // Combine card data with quantities
const collectionWithCards = cards.map(card => ({ const collectionWithCards = cards.map(card => ({
card, card,
quantity: collectionMap.get(card.id) || 0, quantity: result.items.get(card.id) || 0,
})); }));
setCollection(collectionWithCards); setCollection(collectionWithCards);
setFilteredCollection(collectionWithCards); setFilteredCollection(collectionWithCards);
setOffset(PAGE_SIZE);
} catch (error) { } catch (error) {
console.error('Error loading collection:', error); console.error('Error loading collection:', error);
setSnackbar({ message: 'Failed to load collection', type: 'error' }); setSnackbar({ message: 'Failed to load collection', type: 'error' });
@@ -103,6 +171,70 @@ export default function Collection() {
loadCollection(); loadCollection();
}, [user]); }, [user]);
// Load more cards for infinite scroll
const loadMoreCards = useCallback(async () => {
if (!user || isLoadingMore || !hasMore) return;
try {
setIsLoadingMore(true);
// Get next page of collection
const result = await getUserCollectionPaginated(user.id, PAGE_SIZE, offset);
setHasMore(result.hasMore);
if (result.items.size === 0) {
return;
}
// Get card data from Scryfall
const cardIds = Array.from(result.items.keys());
const cards = await getCardsByIds(cardIds);
// Combine card data with quantities
const newCards = cards.map(card => ({
card,
quantity: result.items.get(card.id) || 0,
}));
// Deduplicate: only add cards that aren't already in the collection
setCollection(prev => {
const existingIds = new Set(prev.map(item => item.card.id));
const uniqueNewCards = newCards.filter(item => !existingIds.has(item.card.id));
return [...prev, ...uniqueNewCards];
});
setOffset(prev => prev + PAGE_SIZE);
} catch (error) {
console.error('Error loading more cards:', error);
setSnackbar({ message: 'Failed to load more cards', type: 'error' });
} finally {
setIsLoadingMore(false);
}
}, [user, offset, hasMore, isLoadingMore]);
// Intersection Observer for infinite scroll
useEffect(() => {
const observer = new IntersectionObserver(
(entries) => {
if (entries[0].isIntersecting && hasMore && !isLoadingMore) {
loadMoreCards();
}
},
{ threshold: 0.1 }
);
const currentTarget = observerTarget.current;
if (currentTarget) {
observer.observe(currentTarget);
}
return () => {
if (currentTarget) {
observer.unobserve(currentTarget);
}
};
}, [hasMore, isLoadingMore, loadMoreCards]);
// Filter collection based on search query // Filter collection based on search query
useEffect(() => { useEffect(() => {
if (!searchQuery.trim()) { if (!searchQuery.trim()) {
@@ -189,7 +321,7 @@ export default function Collection() {
}; };
return ( return (
<div className="bg-gray-900 text-white p-3 sm:p-6"> <div className="relative bg-gray-900 text-white p-3 sm:p-6 md:min-h-screen">
<div className="max-w-7xl mx-auto"> <div className="max-w-7xl mx-auto">
<h1 className="text-2xl md:text-3xl font-bold mb-4 md:mb-6">My Collection</h1> <h1 className="text-2xl md:text-3xl font-bold mb-4 md:mb-6">My Collection</h1>
@@ -209,9 +341,31 @@ export default function Collection() {
{/* Collection */} {/* Collection */}
<div> <div>
<h2 className="text-xl font-semibold mb-4"> <div className="flex flex-col sm:flex-row sm:items-center sm:justify-between gap-2 mb-4">
{searchQuery ? `Found ${filteredCollection.length} card(s)` : `My Cards (${collection.length} unique, ${collection.reduce((acc, c) => acc + c.quantity, 0)} total)`} <h2 className="text-xl font-semibold">
</h2> {searchQuery ? `Found ${filteredCollection.length} card(s)` : `My Cards (${collection.length} unique, ${collection.reduce((acc, c) => acc + c.quantity, 0)} total)`}
</h2>
{/* Collection Value Summary */}
<div className="bg-gray-800 border border-gray-700 rounded-lg px-4 py-2">
<div className="text-xs text-gray-400 mb-0.5">
{searchQuery ? 'Filtered Value' : 'Total Collection Value'}
</div>
<div className="text-lg font-bold text-green-400">
{isLoadingTotalValue ? (
<Loader2 className="animate-spin" size={20} />
) : searchQuery ? (
// For search results, calculate from filtered collection
`$${filteredCollection.reduce((total, { card, quantity }) => {
const price = card.prices?.usd ? parseFloat(card.prices.usd) : 0;
return total + (price * quantity);
}, 0).toFixed(2)}`
) : (
// For full collection, use pre-calculated total
`$${totalCollectionValue.toFixed(2)}`
)}
</div>
</div>
</div>
{isLoadingCollection ? ( {isLoadingCollection ? (
<div className="flex items-center justify-center py-12"> <div className="flex items-center justify-center py-12">
@@ -255,6 +409,12 @@ export default function Collection() {
<div className="absolute top-1 right-1 bg-blue-600 text-white text-xs sm:text-sm font-bold px-2 py-1 rounded-full shadow-lg"> <div className="absolute top-1 right-1 bg-blue-600 text-white text-xs sm:text-sm font-bold px-2 py-1 rounded-full shadow-lg">
x{quantity} x{quantity}
</div> </div>
{/* Price badge */}
{card.prices?.usd && (
<div className="absolute bottom-1 left-1 bg-green-600 text-white text-[10px] sm:text-xs font-bold px-1.5 py-0.5 rounded shadow-lg">
${card.prices.usd}
</div>
)}
{/* Flip button for double-faced cards */} {/* Flip button for double-faced cards */}
{isMultiFaced && ( {isMultiFaced && (
<button <button
@@ -279,6 +439,25 @@ export default function Collection() {
})} })}
</div> </div>
)} )}
{/* Infinite scroll loading indicator */}
{!searchQuery && isLoadingMore && (
<div className="flex justify-center py-8">
<Loader2 className="animate-spin text-blue-500" size={32} />
</div>
)}
{/* Observer target for infinite scroll */}
{!searchQuery && hasMore && !isLoadingMore && (
<div ref={observerTarget} className="h-20" />
)}
{/* End of collection indicator */}
{!searchQuery && !hasMore && collection.length > 0 && (
<div className="text-center py-8 text-gray-500 text-sm">
End of collection {totalCount} total cards
</div>
)}
</div> </div>
</div> </div>
@@ -295,7 +474,7 @@ export default function Collection() {
const displayOracleText = currentFace?.oracle_text || hoveredCard.oracle_text; const displayOracleText = currentFace?.oracle_text || hoveredCard.oracle_text;
return ( return (
<div className="hidden lg:block fixed top-1/2 right-8 transform -translate-y-1/2 z-40 pointer-events-none"> <div className="hidden lg:block fixed top-1/2 right-8 transform -translate-y-1/2 z-30 pointer-events-none">
<div className="bg-gray-800 rounded-lg shadow-2xl p-4 max-w-md"> <div className="bg-gray-800 rounded-lg shadow-2xl p-4 max-w-md">
<div className="relative"> <div className="relative">
<img <img
@@ -344,16 +523,16 @@ export default function Collection() {
<> <>
{/* Backdrop */} {/* Backdrop */}
<div <div
className="fixed inset-0 bg-black bg-opacity-50 z-40 transition-opacity duration-300" className="fixed inset-0 bg-black bg-opacity-50 z-[110] transition-opacity duration-300"
onClick={() => setSelectedCard(null)} onClick={() => setSelectedCard(null)}
/> />
{/* Sliding Panel */} {/* Sliding Panel */}
<div className="fixed top-0 right-0 h-full w-full md:w-96 bg-gray-800 shadow-2xl z-50 overflow-y-auto animate-slide-in-right"> <div className="fixed top-0 right-0 h-full w-full md:w-96 bg-gray-800 shadow-2xl z-[120] overflow-y-auto animate-slide-in-right">
{/* Close button - fixed position, stays visible when scrolling */} {/* Close button - fixed position, stays visible when scrolling */}
<button <button
onClick={() => setSelectedCard(null)} onClick={() => setSelectedCard(null)}
className="fixed top-4 right-4 bg-gray-700 hover:bg-gray-600 text-white p-2 md:p-1.5 rounded-full transition-colors z-[60] shadow-lg" className="fixed top-4 right-4 bg-gray-700 hover:bg-gray-600 text-white p-2 md:p-1.5 rounded-full transition-colors z-[130] shadow-lg"
aria-label="Close" aria-label="Close"
> >
<X size={24} className="md:w-5 md:h-5" /> <X size={24} className="md:w-5 md:h-5" />
@@ -362,7 +541,7 @@ export default function Collection() {
<div className="p-4 sm:p-6"> <div className="p-4 sm:p-6">
{/* Card Image */} {/* Card Image */}
<div className="relative mb-4"> <div className="relative mb-4 max-w-sm mx-auto">
<img <img
src={getCardLargeImageUri(selectedCard.card, currentFaceIndex)} src={getCardLargeImageUri(selectedCard.card, currentFaceIndex)}
alt={displayName} alt={displayName}
@@ -478,7 +657,7 @@ export default function Collection() {
<div <div
className={`fixed bottom-4 right-4 p-4 rounded-lg shadow-lg transition-all duration-300 ${ className={`fixed bottom-4 right-4 p-4 rounded-lg shadow-lg transition-all duration-300 ${
snackbar.type === 'success' ? 'bg-green-500' : 'bg-red-500' snackbar.type === 'success' ? 'bg-green-500' : 'bg-red-500'
} text-white z-50`} } text-white z-[140]`}
> >
<div className="flex items-center justify-between"> <div className="flex items-center justify-between">
<div className="flex items-center"> <div className="flex items-center">

File diff suppressed because it is too large Load Diff

View File

@@ -31,7 +31,7 @@ export default function DeckBuilder({
initial={{ x: "100%" }} initial={{ x: "100%" }}
animate={{ x: isOpen ? "0%" : "100%" }} animate={{ x: isOpen ? "0%" : "100%" }}
transition={{ type: "spring", stiffness: 300, damping: 30 }} transition={{ type: "spring", stiffness: 300, damping: 30 }}
className="fixed top-0 right-0 w-4/5 h-full bg-gray-800 p-6 shadow-lg md:static md:w-full md:h-auto md:p-6 md:shadow-none z-50" className="fixed top-0 right-0 w-4/5 h-full bg-gray-800 p-6 shadow-lg md:static md:w-full md:h-auto md:p-6 md:shadow-none z-[110]"
> >
{/* Bouton de fermeture */} {/* Bouton de fermeture */}
<button <button

View File

@@ -1,7 +1,6 @@
import React from 'react'; import React from 'react';
import { AlertTriangle, Check, Edit } from 'lucide-react'; import { AlertTriangle, Check, Edit } from 'lucide-react';
import { Deck } from '../types'; import { Deck } from '../types';
import { validateDeck } from '../utils/deckValidation';
interface DeckCardProps { interface DeckCardProps {
deck: Deck; deck: Deck;
@@ -9,16 +8,12 @@ interface DeckCardProps {
} }
export default function DeckCard({ deck, onEdit }: DeckCardProps) { export default function DeckCard({ deck, onEdit }: DeckCardProps) {
// Use pre-calculated validation data
const isValid = deck.isValid ?? true;
const validationErrors = deck.validationErrors || [];
if(deck.id === "410ed539-a8f4-4bc4-91f1-6c113b9b7e25"){ // Use cover card (already loaded)
console.log("deck", deck.name); const coverImage = deck.coverCard?.image_uris?.normal;
console.log("cardEntities", deck.cards);
}
const validation = validateDeck(deck);
const commander = deck.format === 'commander' ? deck.cards.find(card =>
card.is_commander
)?.card : null;
return ( return (
<div <div
@@ -27,11 +22,17 @@ export default function DeckCard({ deck, onEdit }: DeckCardProps) {
> >
{/* Full Card Art */} {/* Full Card Art */}
<div className="relative aspect-[5/7] overflow-hidden"> <div className="relative aspect-[5/7] overflow-hidden">
<img {coverImage ? (
src={commander?.image_uris?.normal || deck.cards[0]?.card.image_uris?.normal} <img
alt={commander?.name || deck.cards[0]?.card.name} src={coverImage}
className="w-full h-full object-cover group-hover:scale-105 transition-transform duration-300" alt={deck.coverCard?.name || deck.name}
/> className="w-full h-full object-cover group-hover:scale-105 transition-transform duration-300"
/>
) : (
<div className="w-full h-full bg-gray-700 flex items-center justify-center text-gray-500">
No Cover
</div>
)}
{/* Overlay for text readability */} {/* Overlay for text readability */}
<div className="absolute inset-0 bg-gradient-to-t from-gray-900 via-gray-900/60 to-transparent" /> <div className="absolute inset-0 bg-gradient-to-t from-gray-900 via-gray-900/60 to-transparent" />
@@ -39,21 +40,21 @@ export default function DeckCard({ deck, onEdit }: DeckCardProps) {
<div className="absolute bottom-0 left-0 right-0 p-3"> <div className="absolute bottom-0 left-0 right-0 p-3">
<div className="flex items-start justify-between mb-1"> <div className="flex items-start justify-between mb-1">
<h3 className="text-base sm:text-lg font-bold text-white line-clamp-2 flex-1">{deck.name}</h3> <h3 className="text-base sm:text-lg font-bold text-white line-clamp-2 flex-1">{deck.name}</h3>
{validation.isValid ? ( {isValid ? (
<Check size={16} className="text-green-400 ml-2 flex-shrink-0" /> <Check size={16} className="text-green-400 ml-2 flex-shrink-0" />
) : ( ) : (
<AlertTriangle size={16} className="text-yellow-400 ml-2 flex-shrink-0" title={validation.errors.join(', ')} /> <AlertTriangle size={16} className="text-yellow-400 ml-2 flex-shrink-0" title={validationErrors.join(', ')} />
)} )}
</div> </div>
<div className="flex items-center justify-between text-xs text-gray-300 mb-2"> <div className="flex items-center justify-between text-xs text-gray-300 mb-2">
<span className="capitalize">{deck.format}</span> <span className="capitalize">{deck.format}</span>
<span>{deck.cards.reduce((acc, curr) => acc + curr.quantity, 0)} cards</span> <span>{deck.cardCount || 0} cards</span>
</div> </div>
{commander && ( {deck.format === 'commander' && deck.coverCard && (
<div className="text-xs text-blue-300 mb-2 truncate"> <div className="text-xs text-blue-300 mb-2 truncate">
<span className="font-semibold">Commander:</span> {commander.name} <span className="font-semibold">Commander:</span> {deck.coverCard.name}
</div> </div>
)} )}

View File

@@ -42,6 +42,7 @@ export default function DeckEditor({ deckId, onClose }: DeckEditorProps) {
const cards = cardEntities.map(entity => ({ const cards = cardEntities.map(entity => ({
card: scryfallCards.find(c => c.id === entity.card_id) as Card, card: scryfallCards.find(c => c.id === entity.card_id) as Card,
quantity: entity.quantity, quantity: entity.quantity,
is_commander: entity.is_commander,
})); }));
setDeck({ setDeck({
@@ -62,7 +63,7 @@ export default function DeckEditor({ deckId, onClose }: DeckEditorProps) {
if (loading) { if (loading) {
return ( return (
<div className="min-h-screen bg-gray-900 text-white p-6 flex items-center justify-center"> <div className="relative md:min-h-screen bg-gray-900 text-white p-6 flex items-center justify-center">
<div className="animate-spin rounded-full h-32 w-32 border-t-2 border-b-2 border-blue-500"></div> <div className="animate-spin rounded-full h-32 w-32 border-t-2 border-b-2 border-blue-500"></div>
</div> </div>
); );
@@ -70,7 +71,7 @@ export default function DeckEditor({ deckId, onClose }: DeckEditorProps) {
if (!deck) { if (!deck) {
return ( return (
<div className="min-h-screen bg-gray-900 text-white p-6"> <div className="relative md:min-h-screen bg-gray-900 text-white p-6">
<div className="max-w-7xl mx-auto"> <div className="max-w-7xl mx-auto">
<div className="bg-red-500/10 border border-red-500 rounded-lg p-4"> <div className="bg-red-500/10 border border-red-500 rounded-lg p-4">
<h2 className="text-xl font-bold text-red-500">Error</h2> <h2 className="text-xl font-bold text-red-500">Error</h2>

View File

@@ -4,6 +4,7 @@ import { Deck } from '../types';
import { supabase } from "../lib/supabase"; import { supabase } from "../lib/supabase";
import DeckCard from "./DeckCard"; import DeckCard from "./DeckCard";
import { PlusCircle } from 'lucide-react'; import { PlusCircle } from 'lucide-react';
import MigrateDeckButton from "./MigrateDeckButton.tsx";
interface DeckListProps { interface DeckListProps {
onDeckEdit?: (deckId: string) => void; onDeckEdit?: (deckId: string) => void;
@@ -23,58 +24,36 @@ const DeckList = ({ onDeckEdit, onCreateDeck }: DeckListProps) => {
return; return;
} }
const decksWithCards = await Promise.all(decksData.map(async (deck) => { // Get all unique cover card IDs
const { data: cardEntities, error: cardsError } = await supabase const coverCardIds = decksData
.from('deck_cards') .map(deck => deck.cover_card_id)
.select('*') .filter(Boolean);
.eq('deck_id', deck.id);
// Fetch only cover cards (much lighter!)
const coverCards = coverCardIds.length > 0
? await getCardsByIds(coverCardIds)
: [];
// Map decks with their cover cards
const decksWithCoverCards = decksData.map(deck => {
const coverCard = deck.cover_card_id
? coverCards.find(c => c.id === deck.cover_card_id)
: null;
if (cardsError) { return {
console.error(`Error fetching cards for deck ${deck.id}:`, cardsError); ...deck,
return { ...deck, cards: [] }; cards: [], // Empty array, we don't load all cards here
} coverCard: coverCard || null,
createdAt: new Date(deck.created_at),
updatedAt: new Date(deck.updated_at),
validationErrors: deck.validation_errors || [],
isValid: deck.is_valid ?? true,
cardCount: deck.card_count || 0,
coverCardId: deck.cover_card_id,
};
});
const cardIds = cardEntities.map((entity) => entity.card_id); setDecks(decksWithCoverCards);
const uniqueCardIds = [...new Set(cardIds)];
if(deck.id === "410ed539-a8f4-4bc4-91f1-6c113b9b7e25"){
console.log("uniqueCardIds", uniqueCardIds);
}
try {
const scryfallCards = await getCardsByIds(uniqueCardIds);
if (!scryfallCards) {
console.error("scryfallCards is undefined after getCardsByIds");
return { ...deck, cards: [] };
}
const cards = cardEntities.map((entity) => {
const card = scryfallCards.find((c) => c.id === entity.card_id);
return {
card,
quantity: entity.quantity,
is_commander: entity.is_commander,
};
});
return {
...deck,
cards,
createdAt: new Date(deck.created_at),
updatedAt: new Date(deck.updated_at),
};
} catch (error) {
console.error("Error fetching cards from Scryfall:", error);
return { ...deck, cards: [] };
}
}));
setDecks(decksWithCards);
setLoading(false); setLoading(false);
}; };

View File

@@ -1,12 +1,12 @@
import React, { useState, useEffect } from 'react'; import React, { useState, useEffect } from 'react';
import { Plus, Minus, Search, Save, Trash2, Loader2, CheckCircle, XCircle, AlertCircle, PackagePlus, RefreshCw } from 'lucide-react'; import { Plus, Minus, Search, Save, Trash2, Loader2, CheckCircle, XCircle, AlertCircle, PackagePlus, RefreshCw, X } from 'lucide-react';
import { Card, Deck } from '../types'; import { Card, Deck } from '../types';
import { searchCards, getUserCollection, addCardToCollection, addMultipleCardsToCollection } from '../services/api'; import { searchCards, getUserCollection, addCardToCollection, addMultipleCardsToCollection } from '../services/api';
import { useAuth } from '../contexts/AuthContext'; import { useAuth } from '../contexts/AuthContext';
import { supabase } from '../lib/supabase'; import { supabase } from '../lib/supabase';
import { validateDeck } from '../utils/deckValidation'; import { validateDeck } from '../utils/deckValidation';
import MagicCard from './MagicCard'; import MagicCard from './MagicCard';
import { ManaCost } from './ManaCost'; import { ManaCost, ManaSymbol } from './ManaCost';
interface DeckManagerProps { interface DeckManagerProps {
initialDeck?: Deck; initialDeck?: Deck;
@@ -26,7 +26,8 @@ interface DeckManagerProps {
const suggestLandCountAndDistribution = ( const suggestLandCountAndDistribution = (
cards: { card; quantity: number }[], cards: { card; quantity: number }[],
format: string format: string,
commanderColors: string[] = []
) => { ) => {
const formatRules = { const formatRules = {
standard: { minCards: 60 }, standard: { minCards: 60 },
@@ -64,6 +65,16 @@ const suggestLandCountAndDistribution = (
} }
}); });
// For commander, filter out colors not in commander's color identity
if (format === 'commander' && commanderColors.length > 0) {
for (const color in colorCounts) {
if (!commanderColors.includes(color)) {
totalColorSymbols -= colorCounts[color as keyof typeof colorCounts];
colorCounts[color as keyof typeof colorCounts] = 0;
}
}
}
const landDistribution: { [key: string]: number } = {}; const landDistribution: { [key: string]: number } = {};
for (const color in colorCounts) { for (const color in colorCounts) {
const proportion = const proportion =
@@ -96,10 +107,25 @@ const suggestLandCountAndDistribution = (
return { landCount: landsToAdd, landDistribution }; return { landCount: landsToAdd, landDistribution };
}; };
// Get commander color identity
const getCommanderColors = (commander: Card | null): string[] => {
if (!commander) return [];
return commander.colors || [];
};
// Check if a card's colors are valid for the commander
const isCardValidForCommander = (card: Card, commanderColors: string[]): boolean => {
if (commanderColors.length === 0) return true; // No commander restriction
const cardColors = card.colors || [];
// Every color in the card must be in the commander's colors
return cardColors.every(color => commanderColors.includes(color));
};
export default function DeckManager({ initialDeck, onSave }: DeckManagerProps) { export default function DeckManager({ initialDeck, onSave }: DeckManagerProps) {
const [currentDeckId, setCurrentDeckId] = useState<string | null>(initialDeck?.id || null); const [currentDeckId, setCurrentDeckId] = useState<string | null>(initialDeck?.id || null);
const [searchQuery, setSearchQuery] = useState(''); const [searchQuery, setSearchQuery] = useState('');
const [searchResults, setSearchResults] = useState<Card[]>([]); const [searchResults, setSearchResults] = useState<Card[]>([]);
const [isSearching, setIsSearching] = useState(false);
const [selectedCards, setSelectedCards] = useState<{ const [selectedCards, setSelectedCards] = useState<{
card: Card; card: Card;
quantity: number; quantity: number;
@@ -123,6 +149,9 @@ export default function DeckManager({ initialDeck, onSave }: DeckManagerProps) {
const [addingCardId, setAddingCardId] = useState<string | null>(null); const [addingCardId, setAddingCardId] = useState<string | null>(null);
const [isAddingAll, setIsAddingAll] = useState(false); const [isAddingAll, setIsAddingAll] = useState(false);
const [cardFaceIndex, setCardFaceIndex] = useState<Map<string, number>>(new Map()); const [cardFaceIndex, setCardFaceIndex] = useState<Map<string, number>>(new Map());
const [hoveredCard, setHoveredCard] = useState<Card | null>(null);
const [hoverSource, setHoverSource] = useState<'search' | 'deck' | null>(null);
const [selectedCard, setSelectedCard] = useState<Card | null>(null);
// Load user collection on component mount // Load user collection on component mount
useEffect(() => { useEffect(() => {
@@ -171,6 +200,13 @@ export default function DeckManager({ initialDeck, onSave }: DeckManagerProps) {
return card.image_uris?.normal || card.image_uris?.small || card.card_faces?.[0]?.image_uris?.normal; return card.image_uris?.normal || card.image_uris?.small || card.card_faces?.[0]?.image_uris?.normal;
}; };
const getCardLargeImageUri = (card: Card, faceIndex: number = 0) => {
if (isDoubleFaced(card) && card.card_faces) {
return card.card_faces[faceIndex]?.image_uris?.large || card.card_faces[faceIndex]?.image_uris?.normal;
}
return card.image_uris?.large || card.image_uris?.normal;
};
// Helper function to check if a card is in the collection // Helper function to check if a card is in the collection
const isCardInCollection = (cardId: string, requiredQuantity: number = 1): boolean => { const isCardInCollection = (cardId: string, requiredQuantity: number = 1): boolean => {
const ownedQuantity = userCollection.get(cardId) || 0; const ownedQuantity = userCollection.get(cardId) || 0;
@@ -262,11 +298,16 @@ export default function DeckManager({ initialDeck, onSave }: DeckManagerProps) {
e.preventDefault(); e.preventDefault();
if (!searchQuery.trim()) return; if (!searchQuery.trim()) return;
setIsSearching(true);
try { try {
const cards = await searchCards(searchQuery); const cards = await searchCards(searchQuery);
setSearchResults(cards); setSearchResults(cards || []);
} catch (error) { } catch (error) {
console.error('Failed to search cards:', error); console.error('Failed to search cards:', error);
setSearchResults([]);
setSnackbar({ message: 'Failed to search cards', type: 'error' });
} finally {
setIsSearching(false);
} }
}; };
@@ -323,6 +364,17 @@ export default function DeckManager({ initialDeck, onSave }: DeckManagerProps) {
updatedAt: new Date(), updatedAt: new Date(),
}; };
// Calculate validation for storage
const validation = validateDeck(deckToSave);
// Determine cover card (commander or first card)
const commanderCard = deckFormat === 'commander' ? selectedCards.find(c => c.card.id === commander?.id) : null;
const coverCard = commanderCard?.card || selectedCards[0]?.card;
const coverCardId = coverCard?.id || null;
// Calculate total card count
const totalCardCount = selectedCards.reduce((acc, curr) => acc + curr.quantity, 0);
const deckData = { const deckData = {
id: deckToSave.id, id: deckToSave.id,
name: deckToSave.name, name: deckToSave.name,
@@ -330,6 +382,10 @@ export default function DeckManager({ initialDeck, onSave }: DeckManagerProps) {
user_id: deckToSave.userId, user_id: deckToSave.userId,
created_at: deckToSave.createdAt, created_at: deckToSave.createdAt,
updated_at: deckToSave.updatedAt, updated_at: deckToSave.updatedAt,
cover_card_id: coverCardId,
validation_errors: validation.errors,
is_valid: validation.isValid,
card_count: totalCardCount,
}; };
// Save or update the deck // Save or update the deck
@@ -387,11 +443,14 @@ export default function DeckManager({ initialDeck, onSave }: DeckManagerProps) {
const validation = validateDeck(currentDeck); const validation = validateDeck(currentDeck);
// Commander color identity validation (for land suggestions)
const commanderColors = deckFormat === 'commander' ? getCommanderColors(commander) : [];
const deckSize = selectedCards.reduce((acc, curr) => acc + curr.quantity, 0); const deckSize = selectedCards.reduce((acc, curr) => acc + curr.quantity, 0);
const { const {
landCount: suggestedLandCountValue, landCount: suggestedLandCountValue,
landDistribution: suggestedLands, landDistribution: suggestedLands,
} = suggestLandCountAndDistribution(selectedCards, deckFormat); } = suggestLandCountAndDistribution(selectedCards, deckFormat, commanderColors);
const totalPrice = selectedCards.reduce((acc, { card, quantity }) => { const totalPrice = selectedCards.reduce((acc, { card, quantity }) => {
const isBasicLand = const isBasicLand =
@@ -498,7 +557,7 @@ export default function DeckManager({ initialDeck, onSave }: DeckManagerProps) {
}; };
return ( return (
<div className="bg-gray-900 text-white p-3 sm:p-6 pt-6 pb-20 md:pt-20 md:pb-6"> <div className="relative bg-gray-900 text-white p-3 sm:p-6 pt-6 pb-44 md:pt-20 md:pb-6 md:min-h-screen">
<div className="max-w-7xl mx-auto"> <div className="max-w-7xl mx-auto">
<div className="grid grid-cols-1 lg:grid-cols-3 gap-4 sm:gap-6"> <div className="grid grid-cols-1 lg:grid-cols-3 gap-4 sm:gap-6">
{/* Card Search Section */} {/* Card Search Section */}
@@ -535,7 +594,17 @@ export default function DeckManager({ initialDeck, onSave }: DeckManagerProps) {
{/* Vertical Card List for Mobile */} {/* Vertical Card List for Mobile */}
<div className="space-y-2"> <div className="space-y-2">
{searchResults.map(card => { {isSearching ? (
<div className="flex items-center justify-center py-12">
<Loader2 className="animate-spin text-blue-500" size={48} />
</div>
) : searchResults.length === 0 && searchQuery ? (
<div className="text-center py-12 text-gray-400">
<p className="text-lg mb-2">No cards found</p>
<p className="text-sm">Try a different search term</p>
</div>
) : (
searchResults.map(card => {
const currentFaceIndex = getCurrentFaceIndex(card.id); const currentFaceIndex = getCurrentFaceIndex(card.id);
const isMultiFaced = isDoubleFaced(card); const isMultiFaced = isDoubleFaced(card);
const inCollection = userCollection.get(card.id) || 0; const inCollection = userCollection.get(card.id) || 0;
@@ -547,13 +616,27 @@ export default function DeckManager({ initialDeck, onSave }: DeckManagerProps) {
? card.card_faces[currentFaceIndex]?.name || card.name ? card.card_faces[currentFaceIndex]?.name || card.name
: card.name; : card.name;
const isValidForCommander = deckFormat !== 'commander' || !commander || isCardValidForCommander(card, commanderColors);
return ( return (
<div <div
key={card.id} key={card.id}
className="bg-gray-800 rounded-lg p-3 flex items-center gap-3 hover:bg-gray-750 transition-colors" className={`bg-gray-800 rounded-lg p-3 flex items-center gap-3 hover:bg-gray-750 transition-colors cursor-pointer ${
!isValidForCommander ? 'border border-yellow-500/50' : ''
}`}
onMouseEnter={() => {
setHoveredCard(card);
setHoverSource('search');
}}
onMouseLeave={() => {
setHoveredCard(null);
setHoverSource(null);
}}
onClick={() => setSelectedCard(card)}
> >
{/* Card Thumbnail */} {/* Card Thumbnail */}
<div className="relative flex-shrink-0 w-16 h-22 rounded overflow-hidden"> <div className="relative flex-shrink-0 w-16 h-22 rounded overflow-hidden"
onClick={(e) => e.stopPropagation()}>
{getCardImageUri(card, currentFaceIndex) ? ( {getCardImageUri(card, currentFaceIndex) ? (
<img <img
src={getCardImageUri(card, currentFaceIndex)} src={getCardImageUri(card, currentFaceIndex)}
@@ -593,11 +676,17 @@ export default function DeckManager({ initialDeck, onSave }: DeckManagerProps) {
x{inCollection} in collection x{inCollection} in collection
</div> </div>
)} )}
{!isValidForCommander && (
<div className="text-xs text-yellow-400 mt-1 flex items-center gap-1">
<AlertCircle size={12} />
Not in commander colors
</div>
)}
</div> </div>
{/* Add/Quantity Controls */} {/* Add/Quantity Controls */}
{quantityInDeck > 0 ? ( {quantityInDeck > 0 ? (
<div className="flex-shrink-0 flex items-center gap-1"> <div className="flex-shrink-0 flex items-center gap-1" onClick={(e) => e.stopPropagation()}>
<button <button
onClick={() => { onClick={() => {
if (quantityInDeck === 1) { if (quantityInDeck === 1) {
@@ -620,7 +709,10 @@ export default function DeckManager({ initialDeck, onSave }: DeckManagerProps) {
</div> </div>
) : ( ) : (
<button <button
onClick={() => addCardToDeck(card)} onClick={(e) => {
e.stopPropagation();
addCardToDeck(card);
}}
className="flex-shrink-0 w-10 h-10 bg-blue-600 hover:bg-blue-700 rounded-full flex items-center justify-center transition-colors" className="flex-shrink-0 w-10 h-10 bg-blue-600 hover:bg-blue-700 rounded-full flex items-center justify-center transition-colors"
> >
<Plus size={20} /> <Plus size={20} />
@@ -629,7 +721,10 @@ export default function DeckManager({ initialDeck, onSave }: DeckManagerProps) {
{/* Add to Collection Button (hidden on mobile by default) */} {/* Add to Collection Button (hidden on mobile by default) */}
<button <button
onClick={() => handleAddCardToCollection(card.id, 1)} onClick={(e) => {
e.stopPropagation();
handleAddCardToCollection(card.id, 1);
}}
disabled={isAddingThisCard} disabled={isAddingThisCard}
className="hidden sm:flex flex-shrink-0 w-10 h-10 bg-green-600 hover:bg-green-700 disabled:bg-gray-600 disabled:cursor-not-allowed rounded-full items-center justify-center transition-colors" className="hidden sm:flex flex-shrink-0 w-10 h-10 bg-green-600 hover:bg-green-700 disabled:bg-gray-600 disabled:cursor-not-allowed rounded-full items-center justify-center transition-colors"
title="Add to collection" title="Add to collection"
@@ -642,7 +737,8 @@ export default function DeckManager({ initialDeck, onSave }: DeckManagerProps) {
</button> </button>
</div> </div>
); );
})} })
)}
</div> </div>
</div> </div>
@@ -671,27 +767,39 @@ export default function DeckManager({ initialDeck, onSave }: DeckManagerProps) {
</select> </select>
{deckFormat === 'commander' && ( {deckFormat === 'commander' && (
<select <div className="space-y-2">
value={commander?.id || ''} <select
onChange={e => { value={commander?.id || ''}
const card = onChange={e => {
selectedCards.find(c => c.card.id === e.target.value)?.card || const card =
null; selectedCards.find(c => c.card.id === e.target.value)?.card ||
setCommander(card); null;
}} setCommander(card);
className="w-full px-4 py-2 bg-gray-700 border border-gray-600 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent text-white" }}
> className="w-full px-4 py-2 bg-gray-700 border border-gray-600 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent text-white"
<option value="">Select Commander</option> >
{selectedCards <option value="">Select Commander</option>
.filter(c => {selectedCards
c.card.type_line?.toLowerCase().includes('legendary') .filter(c =>
) c.card.type_line?.toLowerCase().includes('legendary')
.map(({ card }) => ( )
<option key={card.id} value={card.id}> .map(({ card }) => (
{card.name} <option key={card.id} value={card.id}>
</option> {card.name}
))} </option>
</select> ))}
</select>
{commander && commanderColors.length > 0 && (
<div className="bg-gray-700 rounded px-3 py-2 flex items-center gap-2">
<span className="text-xs text-gray-400">Commander Colors:</span>
<div className="flex items-center gap-1">
{commanderColors.map(color => (
<ManaSymbol key={color} symbol={color} size={18} />
))}
</div>
</div>
)}
</div>
)} )}
<div className="relative"> <div className="relative">
@@ -732,113 +840,326 @@ export default function DeckManager({ initialDeck, onSave }: DeckManagerProps) {
</h3> </h3>
</div> </div>
{selectedCards.map(({ card, quantity }) => ( {selectedCards.map(({ card, quantity }) => {
<div const isValidForCommander = deckFormat !== 'commander' || !commander || isCardValidForCommander(card, commanderColors);
key={card.id}
className="flex items-center gap-3 p-2 rounded-lg bg-gray-700" return (
> <div
<img key={card.id}
src={card.image_uris?.art_crop} className={`flex items-center gap-3 p-2 rounded-lg bg-gray-700 cursor-pointer hover:bg-gray-650 transition-colors ${
alt={card.name} !isValidForCommander ? 'border border-yellow-500/50' : ''
className="w-10 h-10 rounded" }`}
/> onMouseEnter={() => {
<div className="flex-1 min-w-0"> setHoveredCard(card);
<h4 className="font-medium text-sm truncate">{card.name}</h4> setHoverSource('deck');
{card.prices?.usd && ( }}
<div className="text-xs text-gray-400">${card.prices.usd}</div> onMouseLeave={() => {
)} setHoveredCard(null);
</div> setHoverSource(null);
<div className="flex items-center gap-2"> }}
<input onClick={() => setSelectedCard(card)}
type="number" >
value={quantity} <img
onChange={e => src={card.image_uris?.art_crop}
updateCardQuantity(card.id, parseInt(e.target.value)) alt={card.name}
} className="w-10 h-10 rounded"
min="1"
className="w-14 px-2 py-1 bg-gray-600 border border-gray-500 rounded text-center text-sm"
/> />
<button <div className="flex-1 min-w-0">
onClick={() => removeCardFromDeck(card.id)} <h4 className="font-medium text-sm truncate">{card.name}</h4>
className="text-red-500 hover:text-red-400" {card.prices?.usd && (
> <div className="text-xs text-gray-400">${card.prices.usd}</div>
<Trash2 size={18} /> )}
</button> {!isValidForCommander && (
<div className="text-xs text-yellow-400 flex items-center gap-1 mt-0.5">
<AlertCircle size={10} />
<span>Not in commander colors</span>
</div>
)}
</div>
<div className="flex items-center gap-2" onClick={(e) => e.stopPropagation()}>
<input
type="number"
value={quantity}
onChange={e =>
updateCardQuantity(card.id, parseInt(e.target.value))
}
min="1"
className="w-14 px-2 py-1 bg-gray-600 border border-gray-500 rounded text-center text-sm"
/>
<button
onClick={() => removeCardFromDeck(card.id)}
className="text-red-500 hover:text-red-400"
>
<Trash2 size={18} />
</button>
</div>
</div> </div>
);
})}
</div>
{deckSize > 0 && suggestedLandCountValue > 0 && (
<div className="bg-gray-700 rounded-lg p-3">
<div className="flex items-center justify-between mb-2">
<span className="text-sm font-semibold text-gray-300">Suggested Lands</span>
<span className="text-xs text-gray-400">{suggestedLandCountValue} total</span>
</div> </div>
))} <div className="flex items-center gap-3 flex-wrap">
</div> {Object.entries(suggestedLands).map(([landType, count]) =>
count > 0 ? (
<div className="font-bold text-xl"> <div key={landType} className="flex items-center gap-1.5 bg-gray-800 px-2 py-1 rounded">
Total Price: ${totalPrice.toFixed(2)} <ManaSymbol symbol={landType} size={20} />
</div> <span className="text-sm font-medium text-white">{count}</span>
</div>
{deckSize > 0 && ( ) : null
<div className="text-gray-400"> )}
Suggested Land Count: {suggestedLandCountValue} </div>
{Object.entries(suggestedLands).map(([landType, count]) => ( <button
<div key={landType}> onClick={addSuggestedLandsToDeck}
{landType}: {count} className="w-full mt-3 px-4 py-2 bg-blue-600 hover:bg-blue-700 rounded-lg flex items-center justify-center gap-2 transition-colors"
</div> >
))} <Plus size={20} />
Add Suggested Lands
</button>
</div> </div>
)} )}
{deckSize > 0 && (
<button
onClick={addSuggestedLandsToDeck}
className="w-full px-4 py-2 bg-blue-600 hover:bg-blue-700 rounded-lg flex items-center justify-center gap-2"
>
<Plus size={20} />
Add Suggested Lands
</button>
)}
<div className="flex gap-2">
{!isLoadingCollection && getMissingCards().length > 0 && (
<button
onClick={handleAddAllMissingCards}
disabled={isAddingAll}
className="flex-1 px-4 py-2 bg-yellow-600 hover:bg-yellow-700 disabled:bg-gray-600 disabled:cursor-not-allowed rounded-lg flex items-center justify-center gap-2"
title="Add missing cards to collection"
>
{isAddingAll ? (
<Loader2 className="animate-spin" size={20} />
) : (
<>
<PackagePlus size={20} />
<span className="hidden sm:inline">Add Missing</span>
</>
)}
</button>
)}
<button
onClick={saveDeck}
disabled={
!deckName.trim() || selectedCards.length === 0 || isSaving
}
className="flex-1 px-4 py-2 bg-green-600 hover:bg-green-700 disabled:bg-gray-600 disabled:cursor-not-allowed rounded-lg flex items-center justify-center gap-2 relative"
>
{isSaving ? (
<>
<Loader2 className="animate-spin text-white absolute left-2 top-1/2 -translate-y-1/2" size={20} />
<span className="opacity-0">Save Deck</span>
</>
) : (
<>
<Save size={20} />
<span>{initialDeck ? 'Update Deck' : 'Save Deck'}</span>
</>
)}
</button>
</div>
</div> </div>
</div> </div>
</div> </div>
</div> </div>
{/* Fixed Footer with Price and Actions - Mobile First */}
<div className="fixed bottom-16 left-0 right-0 md:left-auto md:right-4 md:bottom-4 md:w-80 z-20 bg-gray-800 border-t border-gray-700 md:border md:rounded-lg shadow-2xl">
<div className="p-3 space-y-3">
{/* Total Price */}
<div className="flex items-center justify-between">
<span className="text-sm font-semibold text-gray-300">Total Price</span>
<span className="text-xl font-bold text-green-400">${totalPrice.toFixed(2)}</span>
</div>
{/* Action Buttons */}
<div className="flex gap-2">
{!isLoadingCollection && getMissingCards().length > 0 && (
<button
onClick={handleAddAllMissingCards}
disabled={isAddingAll}
className="flex-1 px-3 py-2 bg-yellow-600 hover:bg-yellow-700 disabled:bg-gray-600 disabled:cursor-not-allowed rounded-lg flex items-center justify-center gap-2 text-sm font-medium transition-colors"
title="Add missing cards to collection"
>
{isAddingAll ? (
<Loader2 className="animate-spin" size={18} />
) : (
<>
<PackagePlus size={18} />
<span className="hidden sm:inline">Add Missing</span>
</>
)}
</button>
)}
<button
onClick={saveDeck}
disabled={
!deckName.trim() || selectedCards.length === 0 || isSaving
}
className="flex-1 px-3 py-2 bg-green-600 hover:bg-green-700 disabled:bg-gray-600 disabled:cursor-not-allowed rounded-lg flex items-center justify-center gap-2 text-sm font-medium relative transition-colors"
>
{isSaving ? (
<>
<Loader2 className="animate-spin text-white" size={18} />
<span>Saving...</span>
</>
) : (
<>
<Save size={18} />
<span>{initialDeck ? 'Update' : 'Save'}</span>
</>
)}
</button>
</div>
</div>
</div>
{/* Hover Card Preview - only show if no card is selected */}
{hoveredCard && !selectedCard && (() => {
const currentFaceIndex = getCurrentFaceIndex(hoveredCard.id);
const isMultiFaced = isDoubleFaced(hoveredCard);
const currentFace = isMultiFaced && hoveredCard.card_faces
? hoveredCard.card_faces[currentFaceIndex]
: null;
const displayName = currentFace?.name || hoveredCard.name;
const displayTypeLine = currentFace?.type_line || hoveredCard.type_line;
const displayOracleText = currentFace?.oracle_text || hoveredCard.oracle_text;
// Position preview based on hover source
const positionClass = hoverSource === 'deck' ? 'left-8' : 'right-8';
return (
<div className={`hidden lg:block fixed top-1/2 ${positionClass} transform -translate-y-1/2 z-30 pointer-events-none`}>
<div className="bg-gray-800 rounded-lg shadow-2xl p-4 max-w-md">
<div className="relative">
<img
src={getCardLargeImageUri(hoveredCard, currentFaceIndex)}
alt={displayName}
className="w-full h-auto rounded-lg shadow-lg"
/>
{isMultiFaced && (
<div className="absolute top-2 right-2 bg-purple-600 text-white text-xs font-bold px-2 py-1 rounded-full shadow-lg">
Face {currentFaceIndex + 1}/{hoveredCard.card_faces!.length}
</div>
)}
</div>
<div className="mt-3 space-y-2">
<h3 className="text-xl font-bold">{displayName}</h3>
<p className="text-sm text-gray-400">{displayTypeLine}</p>
{displayOracleText && (
<p className="text-sm text-gray-300 border-t border-gray-700 pt-2">
{displayOracleText}
</p>
)}
{hoveredCard.prices?.usd && (
<div className="text-sm text-green-400 font-semibold border-t border-gray-700 pt-2">
${hoveredCard.prices.usd}
</div>
)}
</div>
</div>
</div>
);
})()}
{/* Card Detail Panel - slides in from right */}
{selectedCard && (() => {
const currentFaceIndex = getCurrentFaceIndex(selectedCard.id);
const isMultiFaced = isDoubleFaced(selectedCard);
const currentFace = isMultiFaced && selectedCard.card_faces
? selectedCard.card_faces[currentFaceIndex]
: null;
const displayName = currentFace?.name || selectedCard.name;
const displayTypeLine = currentFace?.type_line || selectedCard.type_line;
const displayOracleText = currentFace?.oracle_text || selectedCard.oracle_text;
return (
<>
{/* Backdrop */}
<div
className="fixed inset-0 bg-black bg-opacity-50 z-[110] transition-opacity duration-300"
onClick={() => setSelectedCard(null)}
/>
{/* Sliding Panel */}
<div className="fixed top-0 right-0 h-full w-full md:w-96 bg-gray-800 shadow-2xl z-[120] overflow-y-auto animate-slide-in-right">
{/* Close button */}
<button
onClick={() => setSelectedCard(null)}
className="fixed top-4 right-4 bg-gray-700 hover:bg-gray-600 text-white p-2 md:p-1.5 rounded-full transition-colors z-[130] shadow-lg"
aria-label="Close"
>
<X size={24} className="md:w-5 md:h-5" />
</button>
<div className="p-4 sm:p-6">
{/* Card Image */}
<div className="relative mb-4 max-w-sm mx-auto">
<img
src={getCardLargeImageUri(selectedCard, currentFaceIndex)}
alt={displayName}
className="w-full h-auto rounded-lg shadow-lg"
/>
{isMultiFaced && (
<>
<div className="absolute top-2 right-2 bg-purple-600 text-white text-xs font-bold px-2 py-1 rounded-full shadow-lg">
Face {currentFaceIndex + 1}/{selectedCard.card_faces!.length}
</div>
<button
onClick={() => toggleCardFace(selectedCard.id, selectedCard.card_faces!.length)}
className="absolute bottom-2 right-2 bg-purple-600 hover:bg-purple-700 text-white p-2 rounded-full shadow-lg transition-all"
title="Flip card"
>
<RefreshCw size={20} />
</button>
</>
)}
</div>
{/* Card Info */}
<div className="space-y-4">
<div>
<h2 className="text-xl md:text-2xl font-bold text-white mb-2">{displayName}</h2>
<p className="text-xs sm:text-sm text-gray-400">{displayTypeLine}</p>
</div>
{displayOracleText && (
<div className="border-t border-gray-700 pt-3">
<p className="text-sm text-gray-300">{displayOracleText}</p>
</div>
)}
{selectedCard.prices?.usd && (
<div className="border-t border-gray-700 pt-3">
<div className="text-lg text-green-400 font-semibold">
${selectedCard.prices.usd} each
</div>
</div>
)}
{/* Collection Status */}
{userCollection.has(selectedCard.id) && (
<div className="border-t border-gray-700 pt-3">
<div className="text-sm text-green-400">
<CheckCircle size={16} className="inline mr-1" />
x{userCollection.get(selectedCard.id)} in your collection
</div>
</div>
)}
{/* Deck Quantity Management */}
<div className="border-t border-gray-700 pt-3">
<h3 className="text-lg font-semibold mb-3">Quantity in Deck</h3>
<div className="flex items-center justify-between bg-gray-900 rounded-lg p-4">
<button
onClick={() => {
const cardInDeck = selectedCards.find(c => c.card.id === selectedCard.id);
const currentQuantity = cardInDeck?.quantity || 0;
if (currentQuantity === 1) {
removeCardFromDeck(selectedCard.id);
} else if (currentQuantity > 1) {
updateCardQuantity(selectedCard.id, currentQuantity - 1);
}
}}
disabled={!selectedCards.find(c => c.card.id === selectedCard.id)}
className="bg-red-600 hover:bg-red-700 disabled:bg-gray-600 disabled:cursor-not-allowed text-white p-2 rounded-lg transition-colors"
>
<Minus size={20} />
</button>
<div className="text-center">
<div className="text-3xl font-bold">
{selectedCards.find(c => c.card.id === selectedCard.id)?.quantity || 0}
</div>
<div className="text-xs text-gray-400">copies</div>
</div>
<button
onClick={() => addCardToDeck(selectedCard)}
className="bg-green-600 hover:bg-green-700 text-white p-2 rounded-lg transition-colors"
>
<Plus size={20} />
</button>
</div>
</div>
</div>
</div>
</div>
</>
);
})()}
{snackbar && ( {snackbar && (
<div <div
className={`fixed bottom-4 right-4 bg-green-500 text-white p-4 rounded-lg shadow-lg transition-all duration-300 ${ className={`fixed bottom-4 right-4 text-white p-4 rounded-lg shadow-lg transition-all duration-300 z-[140] ${
snackbar.type === 'success' ? 'bg-green-500' : 'bg-red-500' snackbar.type === 'success' ? 'bg-green-500' : 'bg-red-500'
}`} }`}
> >

View File

@@ -157,7 +157,7 @@ import React, { useState, useEffect } from 'react';
); );
return ( return (
<div className="min-h-screen bg-gray-900 text-white p-6"> <div className="relative md:min-h-screen bg-gray-900 text-white p-6">
<div className="max-w-7xl mx-auto"> <div className="max-w-7xl mx-auto">
<h1 className="text-3xl font-bold mb-6">Life Counter</h1> <h1 className="text-3xl font-bold mb-6">Life Counter</h1>
{!setupComplete ? renderSetupForm() : renderLifeCounters()} {!setupComplete ? renderSetupForm() : renderLifeCounters()}

View File

@@ -0,0 +1,64 @@
import React, { useState } from 'react';
import { Database, Loader2 } from 'lucide-react';
import { migrateExistingDecks } from '../utils/migrateDeckData';
export default function MigrateDeckButton() {
const [isMigrating, setIsMigrating] = useState(false);
const [result, setResult] = useState<string | null>(null);
const handleMigrate = async () => {
if (!confirm('This will update all existing decks with optimization data. Continue?')) {
return;
}
setIsMigrating(true);
setResult(null);
try {
await migrateExistingDecks();
setResult('Migration completed successfully!');
} catch (error) {
console.error('Migration error:', error);
setResult('Migration failed. Check console for details.');
} finally {
setIsMigrating(false);
}
};
return (
<div className="bg-gray-800 rounded-lg p-4 border border-gray-700">
<h3 className="text-lg font-semibold mb-2 flex items-center gap-2">
<Database size={20} />
Deck Migration Tool
</h3>
<p className="text-sm text-gray-400 mb-4">
Update existing decks with optimization fields (cover image, validation cache, card count).
Run this once after the database migration.
</p>
<button
onClick={handleMigrate}
disabled={isMigrating}
className="px-4 py-2 bg-blue-600 hover:bg-blue-700 disabled:bg-gray-600 disabled:cursor-not-allowed text-white rounded-lg flex items-center gap-2 transition-colors"
>
{isMigrating ? (
<>
<Loader2 className="animate-spin" size={20} />
Migrating...
</>
) : (
<>
<Database size={20} />
Migrate Decks
</>
)}
</button>
{result && (
<p className={`mt-3 text-sm ${result.includes('success') ? 'text-green-400' : 'text-red-400'}`}>
{result}
</p>
)}
</div>
);
}

View File

@@ -53,12 +53,12 @@ export default function Modal({
<> <>
{/* Backdrop */} {/* Backdrop */}
<div <div
className="fixed inset-0 bg-black bg-opacity-50 z-50 transition-opacity duration-300 animate-fade-in" className="fixed inset-0 bg-black bg-opacity-50 z-[110] transition-opacity duration-300 animate-fade-in"
onClick={onClose} onClick={onClose}
/> />
{/* Modal */} {/* Modal */}
<div className="fixed inset-0 z-50 flex items-center justify-center p-4 pointer-events-none"> <div className="fixed inset-0 z-[120] flex items-center justify-center p-4 pointer-events-none">
<div <div
className={`${sizeClasses[size]} w-full bg-gray-800 rounded-lg shadow-2xl pointer-events-auto animate-scale-in`} className={`${sizeClasses[size]} w-full bg-gray-800 rounded-lg shadow-2xl pointer-events-auto animate-scale-in`}
onClick={(e) => e.stopPropagation()} onClick={(e) => e.stopPropagation()}

View File

@@ -69,7 +69,7 @@ export default function Navigation({ currentPage, setCurrentPage }: NavigationPr
return ( return (
<> <>
{/* Desktop Navigation - Top */} {/* Desktop Navigation - Top */}
<nav className="hidden md:block fixed top-0 left-0 right-0 bg-gray-800 border-b border-gray-700 z-50 animate-slide-in-left"> <nav className="hidden md:block fixed top-0 left-0 right-0 bg-gray-800 border-b border-gray-700 z-[100] animate-slide-in-left">
<div className="max-w-7xl mx-auto px-4"> <div className="max-w-7xl mx-auto px-4">
<div className="flex items-center justify-between h-16"> <div className="flex items-center justify-between h-16">
<div className="flex items-center space-x-8"> <div className="flex items-center space-x-8">
@@ -107,7 +107,7 @@ export default function Navigation({ currentPage, setCurrentPage }: NavigationPr
</button> </button>
{showDropdown && ( {showDropdown && (
<div className="absolute right-0 mt-2 w-48 bg-gray-800 rounded-md shadow-lg py-1 border border-gray-700 animate-scale-in glass-effect"> <div className="absolute right-0 mt-2 w-48 bg-gray-800 rounded-md shadow-lg py-1 border border-gray-700 animate-scale-in glass-effect z-[110]">
<button <button
onClick={handleSignOut} onClick={handleSignOut}
className="flex items-center space-x-2 w-full px-4 py-2 text-sm text-gray-300 hover:bg-gray-700 transition-smooth" className="flex items-center space-x-2 w-full px-4 py-2 text-sm text-gray-300 hover:bg-gray-700 transition-smooth"

View File

@@ -85,7 +85,7 @@ export default function PWAInstallPrompt() {
} }
return ( return (
<div className="fixed bottom-20 md:bottom-4 left-4 right-4 md:left-auto md:right-4 md:max-w-sm z-50 animate-slide-in-bottom"> <div className="fixed bottom-20 md:bottom-4 left-4 right-4 md:left-auto md:right-4 md:max-w-sm z-[105] animate-slide-in-bottom">
<div className="bg-gradient-to-r from-blue-600 to-purple-600 rounded-lg shadow-2xl p-4 text-white"> <div className="bg-gradient-to-r from-blue-600 to-purple-600 rounded-lg shadow-2xl p-4 text-white">
<button <button
onClick={handleDismiss} onClick={handleDismiss}

View File

@@ -3,7 +3,7 @@ import { X, ArrowLeftRight, ArrowRight, ArrowLeft, Minus, Send, Gift, Loader2, S
import { useAuth } from '../contexts/AuthContext'; import { useAuth } from '../contexts/AuthContext';
import { useToast } from '../contexts/ToastContext'; import { useToast } from '../contexts/ToastContext';
import { getUserCollection, getCardsByIds } from '../services/api'; import { getUserCollection, getCardsByIds } from '../services/api';
import { createTrade } from '../services/tradesService'; import { createTrade, updateTrade } from '../services/tradesService';
import { Card } from '../types'; import { Card } from '../types';
interface CollectionItem { interface CollectionItem {
@@ -95,6 +95,11 @@ function CollectionGrid({
<div className="absolute top-1 right-1 bg-gray-900/80 text-white text-[10px] px-1 py-0.5 rounded"> <div className="absolute top-1 right-1 bg-gray-900/80 text-white text-[10px] px-1 py-0.5 rounded">
{remainingQty}/{quantity} {remainingQty}/{quantity}
</div> </div>
{card.prices?.usd && (
<div className="absolute top-1 left-1 bg-gray-900/80 text-green-400 text-[10px] px-1 py-0.5 rounded font-semibold">
${card.prices.usd}
</div>
)}
{selected && ( {selected && (
<button <button
onClick={(e) => { onClick={(e) => {
@@ -128,9 +133,22 @@ function SelectedCardsSummary({ cards, onRemove, label, emptyLabel, color }: Sel
const bgColor = color === 'green' ? 'bg-green-900/50' : 'bg-blue-900/50'; const bgColor = color === 'green' ? 'bg-green-900/50' : 'bg-blue-900/50';
const textColor = color === 'green' ? 'text-green-400' : 'text-blue-400'; const textColor = color === 'green' ? 'text-green-400' : 'text-blue-400';
// Calculate total price
const totalPrice = Array.from(cards.values()).reduce((total, item) => {
const price = item.card.prices?.usd ? parseFloat(item.card.prices.usd) : 0;
return total + (price * item.quantity);
}, 0);
return ( return (
<div> <div>
<h4 className={`text-xs font-semibold ${textColor} mb-1`}>{label}:</h4> <div className="flex items-center justify-between mb-1">
<h4 className={`text-xs font-semibold ${textColor}`}>{label}:</h4>
{cards.size > 0 && (
<span className={`text-xs font-semibold ${textColor}`}>
${totalPrice.toFixed(2)}
</span>
)}
</div>
{cards.size === 0 ? ( {cards.size === 0 ? (
<p className="text-gray-500 text-xs">{emptyLabel}</p> <p className="text-gray-500 text-xs">{emptyLabel}</p>
) : ( ) : (
@@ -164,6 +182,11 @@ interface TradeCreatorProps {
receiverCollection: CollectionItem[]; receiverCollection: CollectionItem[];
onClose: () => void; onClose: () => void;
onTradeCreated: () => void; onTradeCreated: () => void;
editMode?: boolean;
existingTradeId?: string;
initialSenderCards?: Card[];
initialReceiverCards?: Card[];
initialMessage?: string;
} }
type MobileStep = 'want' | 'give' | 'review'; type MobileStep = 'want' | 'give' | 'review';
@@ -174,13 +197,18 @@ export default function TradeCreator({
receiverCollection, receiverCollection,
onClose, onClose,
onTradeCreated, onTradeCreated,
editMode = false,
existingTradeId,
initialSenderCards = [],
initialReceiverCards = [],
initialMessage = '',
}: TradeCreatorProps) { }: TradeCreatorProps) {
const { user } = useAuth(); const { user } = useAuth();
const toast = useToast(); const toast = useToast();
const [myCollection, setMyCollection] = useState<CollectionItem[]>([]); const [myCollection, setMyCollection] = useState<CollectionItem[]>([]);
const [loading, setLoading] = useState(true); const [loading, setLoading] = useState(true);
const [submitting, setSubmitting] = useState(false); const [submitting, setSubmitting] = useState(false);
const [message, setMessage] = useState(''); const [message, setMessage] = useState(initialMessage);
const [isGiftMode, setIsGiftMode] = useState(false); const [isGiftMode, setIsGiftMode] = useState(false);
const [mobileStep, setMobileStep] = useState<MobileStep>('want'); const [mobileStep, setMobileStep] = useState<MobileStep>('want');
@@ -204,6 +232,57 @@ export default function TradeCreator({
} }
}, [isGiftMode]); }, [isGiftMode]);
// Pre-populate cards in edit mode
useEffect(() => {
if (!editMode || !myCollection.length || !receiverCollection.length) return;
if (initialSenderCards.length === 0 && initialReceiverCards.length === 0) return;
console.log('Pre-populating cards', {
initialSenderCards: initialSenderCards.length,
initialReceiverCards: initialReceiverCards.length,
myCollection: myCollection.length,
receiverCollection: receiverCollection.length
});
// Pre-populate sender cards with their quantities
const senderMap = new Map<string, SelectedCard>();
initialSenderCards.forEach(card => {
const collectionItem = myCollection.find(c => c.card.id === card.id);
if (collectionItem) {
// Find the quantity from trade items if card has quantity property
const quantity = (card as any).quantity || 1;
console.log('Adding sender card:', card.name, 'qty:', quantity);
senderMap.set(card.id, {
card: card,
quantity: quantity,
maxQuantity: collectionItem.quantity,
});
} else {
console.log('Card not found in my collection:', card.name, card.id);
}
});
setMyOfferedCards(senderMap);
// Pre-populate receiver cards with their quantities
const receiverMap = new Map<string, SelectedCard>();
initialReceiverCards.forEach(card => {
const collectionItem = receiverCollection.find(c => c.card.id === card.id);
if (collectionItem) {
// Find the quantity from trade items if card has quantity property
const quantity = (card as any).quantity || 1;
console.log('Adding receiver card:', card.name, 'qty:', quantity);
receiverMap.set(card.id, {
card: card,
quantity: quantity,
maxQuantity: collectionItem.quantity,
});
} else {
console.log('Card not found in their collection:', card.name, card.id);
}
});
setWantedCards(receiverMap);
}, [editMode, myCollection, receiverCollection, initialSenderCards, initialReceiverCards]);
const loadMyCollection = async () => { const loadMyCollection = async () => {
if (!user) return; if (!user) return;
setLoading(true); setLoading(true);
@@ -296,28 +375,42 @@ export default function TradeCreator({
setSubmitting(true); setSubmitting(true);
try { try {
const senderCards = Array.from(myOfferedCards.values()).map((item) => ({ const myCards = Array.from(myOfferedCards.values()).map((item) => ({
cardId: item.card.id, cardId: item.card.id,
quantity: item.quantity, quantity: item.quantity,
})); }));
const receiverCards = Array.from(wantedCards.values()).map((item) => ({ const theirCards = Array.from(wantedCards.values()).map((item) => ({
cardId: item.card.id, cardId: item.card.id,
quantity: item.quantity, quantity: item.quantity,
})); }));
await createTrade({ if (editMode && existingTradeId) {
senderId: user.id, // Update existing trade
receiverId, await updateTrade({
message: message || undefined, tradeId: existingTradeId,
senderCards, editorId: user.id,
receiverCards, message: message || undefined,
}); myCards,
theirCards,
});
toast.success('Trade updated!');
} else {
// Create new trade
await createTrade({
user1Id: user.id,
user2Id: receiverId,
message: message || undefined,
user1Cards: myCards,
user2Cards: theirCards,
});
toast.success('Trade offer sent!');
}
onTradeCreated(); onTradeCreated();
} catch (error) { } catch (error) {
console.error('Error creating trade:', error); console.error('Error with trade:', error);
toast.error('Failed to create trade'); toast.error(editMode ? 'Failed to update trade' : 'Failed to create trade');
} finally { } finally {
setSubmitting(false); setSubmitting(false);
} }
@@ -340,14 +433,14 @@ export default function TradeCreator({
if (loading) { if (loading) {
return ( return (
<div className="fixed inset-0 bg-black/80 z-50 flex items-center justify-center"> <div className="fixed inset-0 bg-black/80 z-[110] flex items-center justify-center">
<Loader2 className="animate-spin text-blue-500" size={48} /> <Loader2 className="animate-spin text-blue-500" size={48} />
</div> </div>
); );
} }
return ( return (
<div className="fixed inset-0 bg-black/80 z-50 flex items-center justify-center p-0 md:p-4"> <div className="fixed inset-0 bg-black/80 z-[110] flex items-center justify-center p-0 md:p-4">
<div className="bg-gray-800 w-full h-full md:rounded-lg md:w-full md:max-w-6xl md:max-h-[90vh] overflow-hidden flex flex-col"> <div className="bg-gray-800 w-full h-full md:rounded-lg md:w-full md:max-w-6xl md:max-h-[90vh] overflow-hidden flex flex-col">
{/* ============ MOBILE VIEW ============ */} {/* ============ MOBILE VIEW ============ */}
@@ -535,7 +628,7 @@ export default function TradeCreator({
<div className="flex items-center justify-between p-4 border-b border-gray-700"> <div className="flex items-center justify-between p-4 border-b border-gray-700">
<div className="flex items-center gap-3"> <div className="flex items-center gap-3">
<ArrowLeftRight size={24} className="text-blue-400" /> <ArrowLeftRight size={24} className="text-blue-400" />
<h2 className="text-xl font-bold">Trade with {receiverUsername}</h2> <h2 className="text-xl font-bold">{editMode ? 'Edit Trade' : `Trade with ${receiverUsername}`}</h2>
<label className="flex items-center gap-2 ml-4 cursor-pointer"> <label className="flex items-center gap-2 ml-4 cursor-pointer">
<div <div
className={`relative w-10 h-5 rounded-full transition-colors ${ className={`relative w-10 h-5 rounded-full transition-colors ${
@@ -614,7 +707,7 @@ export default function TradeCreator({
)} )}
</div> </div>
<div className="flex items-center gap-4"> <div className="flex items-center gap-4 mb-4">
<input <input
type="text" type="text"
value={message} value={message}

View File

@@ -0,0 +1,486 @@
import React, { useState, useEffect } from 'react';
import { X, Check, ArrowLeftRight, DollarSign, Loader2, Edit, RefreshCcw, History, AlertTriangle } from 'lucide-react';
import { useAuth } from '../contexts/AuthContext';
import { useToast } from '../contexts/ToastContext';
import { Trade, TradeHistoryEntry, getTradeVersionHistory } from '../services/tradesService';
import { getUserCollection, getCardsByIds } from '../services/api';
import { Card } from '../types';
import TradeCreator from './TradeCreator';
interface TradeDetailProps {
trade: Trade;
onClose: () => void;
onAccept: (tradeId: string) => Promise<void>;
onDecline: (tradeId: string) => Promise<void>;
onTradeUpdated: () => void;
}
interface TradeCardItem {
card: Card;
quantity: number;
}
interface CollectionItem {
card: Card;
quantity: number;
}
function calculateTotalPrice(items: TradeCardItem[]): number {
return items.reduce((total, { card, quantity }) => {
const price = card.prices?.usd ? parseFloat(card.prices.usd) : 0;
return total + (price * quantity);
}, 0);
}
export default function TradeDetail({
trade,
onClose,
onAccept,
onDecline,
onTradeUpdated,
}: TradeDetailProps) {
const { user } = useAuth();
const toast = useToast();
const [loading, setLoading] = useState(true);
const [processing, setProcessing] = useState(false);
const [senderCards, setSenderCards] = useState<TradeCardItem[]>([]);
const [receiverCards, setReceiverCards] = useState<TradeCardItem[]>([]);
const [showHistory, setShowHistory] = useState(false);
const [history, setHistory] = useState<TradeHistoryEntry[]>([]);
const [showEditMode, setShowEditMode] = useState(false);
const [editReceiverCollection, setEditReceiverCollection] = useState<CollectionItem[]>([]);
const isUser1 = trade.user1_id === user?.id;
const isUser2 = trade.user2_id === user?.id;
const otherUser = isUser1 ? trade.user2 : trade.user1;
const myUserId = user?.id || '';
const otherUserId = isUser1 ? trade.user2_id : trade.user1_id;
useEffect(() => {
loadTradeCards();
loadTradeHistory();
}, [trade]);
const loadTradeCards = async () => {
setLoading(true);
try {
const allCardIds = trade.items?.map(item => item.card_id) || [];
if (allCardIds.length === 0) {
setSenderCards([]);
setReceiverCards([]);
return;
}
const cards = await getCardsByIds(allCardIds);
const cardMap = new Map<string, Card>();
cards.forEach(card => cardMap.set(card.id, card));
const myItems: TradeCardItem[] = [];
const theirItems: TradeCardItem[] = [];
trade.items?.forEach(item => {
const card = cardMap.get(item.card_id);
if (!card) return;
if (item.owner_id === myUserId) {
myItems.push({ card, quantity: item.quantity });
} else {
theirItems.push({ card, quantity: item.quantity });
}
});
setSenderCards(myItems);
setReceiverCards(theirItems);
} catch (error) {
console.error('Error loading trade cards:', error);
toast.error('Failed to load trade details');
} finally {
setLoading(false);
}
};
const loadTradeHistory = async () => {
try {
const historyData = await getTradeVersionHistory(trade.id);
setHistory(historyData);
} catch (error) {
console.error('Error loading trade history:', error);
}
};
const handleAccept = async () => {
setProcessing(true);
try {
await onAccept(trade.id);
onClose();
} catch (error) {
console.error('Error accepting trade:', error);
} finally {
setProcessing(false);
}
};
const handleDecline = async () => {
setProcessing(true);
try {
await onDecline(trade.id);
onClose();
} catch (error) {
console.error('Error declining trade:', error);
} finally {
setProcessing(false);
}
};
const handleEdit = async () => {
try {
// Load the other user's collection for editing
const collectionMap = await getUserCollection(otherUserId);
const cardIds = Array.from(collectionMap.keys());
const cards = await getCardsByIds(cardIds);
const collection = cards.map((card) => ({
card,
quantity: collectionMap.get(card.id) || 0,
}));
setEditReceiverCollection(collection);
setShowEditMode(true);
} catch (error) {
console.error('Error loading collection for edit:', error);
toast.error('Failed to load collection');
}
};
// In the symmetric model, counter-offer is the same as edit
const handleCounterOffer = handleEdit;
// senderCards = myCards, receiverCards = theirCards (already calculated correctly)
const yourCards = senderCards;
const theirCards = receiverCards;
const yourPrice = calculateTotalPrice(yourCards);
const theirPrice = calculateTotalPrice(theirCards);
// For edit mode, pre-populate with current cards
// In the symmetric model, both edit and counter-offer use the same perspective:
// - Your cards (what you're offering)
// - Their cards (what you want)
// Include quantity in the card object so TradeCreator can preserve it
const editInitialSenderCards = yourCards.map(c => ({ ...c.card, quantity: c.quantity }));
const editInitialReceiverCards = theirCards.map(c => ({ ...c.card, quantity: c.quantity }));
if (showEditMode) {
return (
<TradeCreator
receiverId={otherUserId}
receiverUsername={otherUser?.username || 'User'}
receiverCollection={editReceiverCollection}
onClose={() => {
setShowEditMode(false);
onClose();
}}
onTradeCreated={() => {
setShowEditMode(false);
onTradeUpdated();
onClose();
}}
editMode={true}
existingTradeId={trade.id}
initialSenderCards={editInitialSenderCards}
initialReceiverCards={editInitialReceiverCards}
initialMessage={trade.message || ''}
/>
);
}
return (
<div className="fixed inset-0 bg-black/80 backdrop-blur-sm z-50 flex items-end md:items-center justify-center p-0 md:p-4">
<div className="bg-gray-900 w-full md:max-w-4xl md:rounded-2xl flex flex-col max-h-screen md:max-h-[90vh]">
{/* Header */}
<div className="flex items-center justify-between p-4 border-b border-gray-800">
<div className="flex items-center gap-2">
<ArrowLeftRight size={20} className="text-blue-400" />
<div>
<h2 className="text-lg font-bold">Trade Details {trade.version > 1 && `(v${trade.version})`}</h2>
<p className="text-sm text-gray-400">
With: {otherUser?.username}
</p>
</div>
</div>
<button
onClick={onClose}
className="p-2 hover:bg-gray-800 rounded-lg transition"
>
<X size={20} />
</button>
</div>
{/* Content */}
<div className="flex-1 overflow-y-auto p-4">
{loading ? (
<div className="flex items-center justify-center py-12">
<Loader2 className="animate-spin text-blue-500" size={48} />
</div>
) : (
<div className="space-y-4">
{/* Invalid Trade Warning */}
{trade.status === 'pending' && !trade.is_valid && (
<div className="bg-red-900/30 border border-red-600 rounded-lg p-3 flex items-start gap-2">
<AlertTriangle size={20} className="text-red-400 flex-shrink-0 mt-0.5" />
<div>
<h4 className="font-semibold text-red-400 text-sm">Trade No Longer Valid</h4>
<p className="text-red-200 text-xs mt-1">
One or more cards in this trade are no longer available in the required quantities. This trade cannot be accepted until it is updated.
</p>
</div>
</div>
)}
<div className="grid grid-cols-1 lg:grid-cols-2 gap-4">
{/* Your Side */}
<div className="space-y-3">
<div className="flex items-center justify-between">
<h3 className="font-semibold text-green-400">
You Give
</h3>
<div className="flex items-center gap-1 text-green-400 text-sm">
<DollarSign size={14} />
{yourPrice.toFixed(2)}
</div>
</div>
{yourCards.length === 0 ? (
<p className="text-gray-500 text-center py-8">Gift (no cards)</p>
) : (
<div className="grid grid-cols-3 sm:grid-cols-4 gap-2">
{yourCards.map((item, idx) => (
<div key={idx} className="relative rounded-lg overflow-hidden">
<img
src={item.card.image_uris?.small || item.card.image_uris?.normal}
alt={item.card.name}
className="w-full h-auto"
/>
{item.quantity > 1 && (
<div className="absolute top-1 right-1 bg-green-600 text-white text-xs px-1.5 py-0.5 rounded font-semibold">
x{item.quantity}
</div>
)}
{item.card.prices?.usd && (
<div className="absolute bottom-1 left-1 bg-gray-900/90 text-white text-[10px] px-1 py-0.5 rounded">
${item.card.prices.usd}
</div>
)}
</div>
))}
</div>
)}
</div>
{/* Their Side */}
<div className="space-y-3">
<div className="flex items-center justify-between">
<h3 className="font-semibold text-blue-400">
You Receive
</h3>
<div className="flex items-center gap-1 text-blue-400 text-sm">
<DollarSign size={14} />
{theirPrice.toFixed(2)}
</div>
</div>
{theirCards.length === 0 ? (
<p className="text-gray-500 text-center py-8">Gift (no cards)</p>
) : (
<div className="grid grid-cols-3 sm:grid-cols-4 gap-2">
{theirCards.map((item, idx) => (
<div key={idx} className="relative rounded-lg overflow-hidden">
<img
src={item.card.image_uris?.small || item.card.image_uris?.normal}
alt={item.card.name}
className="w-full h-auto"
/>
{item.quantity > 1 && (
<div className="absolute top-1 right-1 bg-blue-600 text-white text-xs px-1.5 py-0.5 rounded font-semibold">
x{item.quantity}
</div>
)}
{item.card.prices?.usd && (
<div className="absolute bottom-1 left-1 bg-gray-900/90 text-white text-[10px] px-1 py-0.5 rounded">
${item.card.prices.usd}
</div>
)}
</div>
))}
</div>
)}
</div>
</div>
{/* Message */}
{trade.message && (
<div className="p-3 bg-gray-800 rounded-lg">
<p className="text-sm text-gray-400 mb-1">Message:</p>
<p className="text-sm">{trade.message}</p>
</div>
)}
{/* Price Difference */}
{!loading && (yourPrice > 0 || theirPrice > 0) && (
<div className="p-3 bg-gray-800 rounded-lg">
<div className="flex items-center justify-between text-sm">
<span className="text-gray-400">Value Difference:</span>
<span className={Math.abs(yourPrice - theirPrice) > 5 ? 'text-yellow-400' : 'text-gray-300'}>
${Math.abs(yourPrice - theirPrice).toFixed(2)}
{yourPrice > theirPrice ? ' in your favor' : yourPrice < theirPrice ? ' in their favor' : ' (balanced)'}
</span>
</div>
</div>
)}
{/* History */}
{history.length > 0 && (
<div>
<button
onClick={() => setShowHistory(!showHistory)}
className="flex items-center gap-2 text-sm text-blue-400 hover:text-blue-300"
>
<History size={16} />
{showHistory ? 'Hide' : 'Show'} History ({history.length} {history.length === 1 ? 'version' : 'versions'})
</button>
{showHistory && (
<div className="mt-3 space-y-2">
{history.map((entry) => (
<div key={entry.id} className="p-3 bg-gray-800 rounded-lg text-sm">
<div className="flex items-center justify-between mb-2">
<span className="font-semibold text-purple-400">Version {entry.version}</span>
<span className="text-gray-400 text-xs">
Edited by {entry.editor?.username} {new Date(entry.created_at).toLocaleDateString()}
</span>
</div>
{entry.message && (
<p className="text-gray-300 text-xs">{entry.message}</p>
)}
</div>
))}
</div>
)}
</div>
)}
</div>
)}
</div>
{/* Actions - Only for pending trades */}
{trade.status === 'pending' && !loading && (
<div className="border-t border-gray-800 p-4 space-y-2">
{/* Only the user who DIDN'T make the last edit can respond */}
{trade.editor_id && trade.editor_id !== user?.id ? (
/* User receives the last edit - can accept/decline/counter */
<>
<div className="flex gap-2">
<button
onClick={handleAccept}
disabled={processing || !trade.is_valid}
className="flex-1 flex items-center justify-center gap-2 px-4 py-3 bg-green-600 hover:bg-green-700 disabled:bg-gray-600 disabled:cursor-not-allowed rounded-lg font-medium transition"
title={!trade.is_valid ? 'This trade is no longer valid' : ''}
>
{processing ? (
<Loader2 className="animate-spin" size={18} />
) : (
<>
<Check size={18} />
Accept Trade
</>
)}
</button>
<button
onClick={handleDecline}
disabled={processing}
className="flex-1 flex items-center justify-center gap-2 px-4 py-3 bg-red-600 hover:bg-red-700 disabled:bg-gray-600 rounded-lg font-medium transition"
>
<X size={18} />
Decline
</button>
</div>
<button
onClick={handleCounterOffer}
disabled={processing}
className="w-full flex items-center justify-center gap-2 px-4 py-3 bg-purple-600 hover:bg-purple-700 disabled:bg-gray-600 rounded-lg font-medium transition"
>
<RefreshCcw size={18} />
Make Counter Offer
</button>
</>
) : trade.editor_id === user?.id ? (
/* User made the last edit - can still edit while waiting for response */
<>
<p className="text-center text-gray-400 text-sm py-2">
Waiting for {otherUser?.username} to respond...
</p>
<button
onClick={handleEdit}
disabled={processing}
className="w-full flex items-center justify-center gap-2 px-4 py-3 bg-blue-600 hover:bg-blue-700 disabled:bg-gray-600 rounded-lg font-medium transition"
>
<Edit size={18} />
Modify Your Offer
</button>
</>
) : (
/* No editor yet (initial trade) */
<>
{isUser1 ? (
/* User1 (initiator) can edit their initial offer */
<button
onClick={handleEdit}
disabled={processing}
className="w-full flex items-center justify-center gap-2 px-4 py-3 bg-blue-600 hover:bg-blue-700 disabled:bg-gray-600 rounded-lg font-medium transition"
>
<Edit size={18} />
Edit Trade Offer
</button>
) : (
/* User2 (partner) can accept/decline/counter */
<>
<div className="flex gap-2">
<button
onClick={handleAccept}
disabled={processing || !trade.is_valid}
className="flex-1 flex items-center justify-center gap-2 px-4 py-3 bg-green-600 hover:bg-green-700 disabled:bg-gray-600 disabled:cursor-not-allowed rounded-lg font-medium transition"
title={!trade.is_valid ? 'This trade is no longer valid' : ''}
>
{processing ? (
<Loader2 className="animate-spin" size={18} />
) : (
<>
<Check size={18} />
Accept Trade
</>
)}
</button>
<button
onClick={handleDecline}
disabled={processing}
className="flex-1 flex items-center justify-center gap-2 px-4 py-3 bg-red-600 hover:bg-red-700 disabled:bg-gray-600 rounded-lg font-medium transition"
>
<X size={18} />
Decline
</button>
</div>
<button
onClick={handleCounterOffer}
disabled={processing}
className="w-full flex items-center justify-center gap-2 px-4 py-3 bg-purple-600 hover:bg-purple-700 disabled:bg-gray-600 rounded-lg font-medium transition"
>
<RefreshCcw size={18} />
Make Counter Offer
</button>
</>
)}
</>
)}
</div>
)}
</div>
</div>
);
}

View File

@@ -205,42 +205,55 @@ export type Database = {
trades: { trades: {
Row: { Row: {
id: string id: string
sender_id: string user1_id: string
receiver_id: string user2_id: string
status: 'pending' | 'accepted' | 'declined' | 'cancelled' status: 'pending' | 'accepted' | 'declined' | 'cancelled'
message: string | null message: string | null
created_at: string | null created_at: string | null
updated_at: string | null updated_at: string | null
version: number
editor_id: string | null
} }
Insert: { Insert: {
id?: string id?: string
sender_id: string user1_id: string
receiver_id: string user2_id: string
status?: 'pending' | 'accepted' | 'declined' | 'cancelled' status?: 'pending' | 'accepted' | 'declined' | 'cancelled'
message?: string | null message?: string | null
created_at?: string | null created_at?: string | null
updated_at?: string | null updated_at?: string | null
version?: number
editor_id?: string | null
} }
Update: { Update: {
id?: string id?: string
sender_id?: string user1_id?: string
receiver_id?: string user2_id?: string
status?: 'pending' | 'accepted' | 'declined' | 'cancelled' status?: 'pending' | 'accepted' | 'declined' | 'cancelled'
message?: string | null message?: string | null
created_at?: string | null created_at?: string | null
updated_at?: string | null updated_at?: string | null
version?: number
editor_id?: string | null
} }
Relationships: [ Relationships: [
{ {
foreignKeyName: "trades_sender_id_fkey" foreignKeyName: "trades_user1_id_fkey"
columns: ["sender_id"] columns: ["user1_id"]
isOneToOne: false isOneToOne: false
referencedRelation: "profiles" referencedRelation: "profiles"
referencedColumns: ["id"] referencedColumns: ["id"]
}, },
{ {
foreignKeyName: "trades_receiver_id_fkey" foreignKeyName: "trades_user2_id_fkey"
columns: ["receiver_id"] columns: ["user2_id"]
isOneToOne: false
referencedRelation: "profiles"
referencedColumns: ["id"]
},
{
foreignKeyName: "trades_editor_id_fkey"
columns: ["editor_id"]
isOneToOne: false isOneToOne: false
referencedRelation: "profiles" referencedRelation: "profiles"
referencedColumns: ["id"] referencedColumns: ["id"]

View File

@@ -75,10 +75,76 @@ export const getUserCollection = async (userId: string): Promise<Map<string, num
return collectionMap; return collectionMap;
}; };
// Paginated collection API
export interface PaginatedCollectionResult {
items: Map<string, number>; // card_id -> quantity
totalCount: number;
hasMore: boolean;
}
// Get total collection value from user profile (pre-calculated by triggers)
export const getCollectionTotalValue = async (userId: string): Promise<number> => {
const { data, error } = await supabase
.from('profiles')
.select('collection_total_value')
.eq('id', userId)
.single();
if (error) {
console.error('Error fetching collection total value:', error);
return 0;
}
return data?.collection_total_value || 0;
};
export const getUserCollectionPaginated = async (
userId: string,
pageSize: number = 50,
offset: number = 0
): Promise<PaginatedCollectionResult> => {
// First, get the total count
const { count: totalCount, error: countError } = await supabase
.from('collections')
.select('*', { count: 'exact', head: true })
.eq('user_id', userId);
if (countError) {
console.error('Error counting user collection:', countError);
throw countError;
}
// Then get the paginated data
const { data, error } = await supabase
.from('collections')
.select('card_id, quantity')
.eq('user_id', userId)
.order('created_at', { ascending: false })
.range(offset, offset + pageSize - 1);
if (error) {
console.error('Error fetching user collection:', error);
throw error;
}
// Create a map of card_id to quantity for easy lookup
const collectionMap = new Map<string, number>();
data?.forEach((item) => {
collectionMap.set(item.card_id, item.quantity);
});
return {
items: collectionMap,
totalCount: totalCount || 0,
hasMore: offset + pageSize < (totalCount || 0),
};
};
export const addCardToCollection = async ( export const addCardToCollection = async (
userId: string, userId: string,
cardId: string, cardId: string,
quantity: number = 1 quantity: number = 1,
priceUsd: number = 0
): Promise<void> => { ): Promise<void> => {
// Check if card already exists in collection // Check if card already exists in collection
const { data: existing, error: fetchError } = await supabase const { data: existing, error: fetchError } = await supabase
@@ -94,11 +160,12 @@ export const addCardToCollection = async (
} }
if (existing) { if (existing) {
// Update existing card quantity // Update existing card quantity and price
const { error: updateError } = await supabase const { error: updateError } = await supabase
.from('collections') .from('collections')
.update({ .update({
quantity: existing.quantity + quantity, quantity: existing.quantity + quantity,
price_usd: priceUsd,
updated_at: new Date().toISOString() updated_at: new Date().toISOString()
}) })
.eq('id', existing.id); .eq('id', existing.id);
@@ -112,6 +179,7 @@ export const addCardToCollection = async (
user_id: userId, user_id: userId,
card_id: cardId, card_id: cardId,
quantity: quantity, quantity: quantity,
price_usd: priceUsd,
}); });
if (insertError) throw insertError; if (insertError) throw insertError;
@@ -120,7 +188,7 @@ export const addCardToCollection = async (
export const addMultipleCardsToCollection = async ( export const addMultipleCardsToCollection = async (
userId: string, userId: string,
cards: { cardId: string; quantity: number }[] cards: { cardId: string; quantity: number; priceUsd?: number }[]
): Promise<void> => { ): Promise<void> => {
// Fetch existing cards in collection // Fetch existing cards in collection
const cardIds = cards.map(c => c.cardId); const cardIds = cards.map(c => c.cardId);
@@ -146,6 +214,7 @@ export const addMultipleCardsToCollection = async (
toUpdate.push({ toUpdate.push({
id: existing.id, id: existing.id,
quantity: existing.quantity + card.quantity, quantity: existing.quantity + card.quantity,
price_usd: card.priceUsd || 0,
updated_at: new Date().toISOString(), updated_at: new Date().toISOString(),
}); });
} else { } else {
@@ -153,6 +222,7 @@ export const addMultipleCardsToCollection = async (
user_id: userId, user_id: userId,
card_id: card.cardId, card_id: card.cardId,
quantity: card.quantity, quantity: card.quantity,
price_usd: card.priceUsd || 0,
}); });
} }
} }
@@ -170,7 +240,11 @@ export const addMultipleCardsToCollection = async (
for (const update of toUpdate) { for (const update of toUpdate) {
const { error: updateError } = await supabase const { error: updateError } = await supabase
.from('collections') .from('collections')
.update({ quantity: update.quantity, updated_at: update.updated_at }) .update({
quantity: update.quantity,
price_usd: update.price_usd,
updated_at: update.updated_at
})
.eq('id', update.id); .eq('id', update.id);
if (updateError) throw updateError; if (updateError) throw updateError;

View File

@@ -10,23 +10,53 @@ export interface TradeItem {
export interface Trade { export interface Trade {
id: string; id: string;
sender_id: string; user1_id: string;
receiver_id: string; user2_id: string;
status: 'pending' | 'accepted' | 'declined' | 'cancelled'; status: 'pending' | 'accepted' | 'declined' | 'cancelled';
message: string | null; message: string | null;
created_at: string | null; created_at: string | null;
updated_at: string | null; updated_at: string | null;
sender?: { username: string | null }; version: number;
receiver?: { username: string | null }; editor_id: string | null;
is_valid: boolean;
user1?: { username: string | null };
user2?: { username: string | null };
items?: TradeItem[]; items?: TradeItem[];
} }
export interface TradeHistoryEntry {
id: string;
trade_id: string;
version: number;
editor_id: string;
message: string | null;
created_at: string;
editor?: { username: string | null };
items?: TradeHistoryItem[];
}
export interface TradeHistoryItem {
id: string;
history_id: string;
owner_id: string;
card_id: string;
quantity: number;
}
export interface CreateTradeParams { export interface CreateTradeParams {
senderId: string; user1Id: string;
receiverId: string; user2Id: string;
message?: string; message?: string;
senderCards: { cardId: string; quantity: number }[]; user1Cards: { cardId: string; quantity: number }[];
receiverCards: { cardId: string; quantity: number }[]; user2Cards: { cardId: string; quantity: number }[];
}
export interface UpdateTradeParams {
tradeId: string;
editorId: string;
message?: string;
myCards: { cardId: string; quantity: number }[];
theirCards: { cardId: string; quantity: number }[];
} }
// Get all trades for a user // Get all trades for a user
@@ -35,11 +65,11 @@ export async function getTrades(userId: string): Promise<Trade[]> {
.from('trades') .from('trades')
.select(` .select(`
*, *,
sender:profiles!trades_sender_id_fkey(username), user1:profiles!trades_user1_id_fkey(username),
receiver:profiles!trades_receiver_id_fkey(username), user2:profiles!trades_user2_id_fkey(username),
items:trade_items(*) items:trade_items(*)
`) `)
.or(`sender_id.eq.${userId},receiver_id.eq.${userId}`) .or(`user1_id.eq.${userId},user2_id.eq.${userId}`)
.order('created_at', { ascending: false }); .order('created_at', { ascending: false });
if (error) throw error; if (error) throw error;
@@ -52,12 +82,12 @@ export async function getPendingTrades(userId: string): Promise<Trade[]> {
.from('trades') .from('trades')
.select(` .select(`
*, *,
sender:profiles!trades_sender_id_fkey(username), user1:profiles!trades_user1_id_fkey(username),
receiver:profiles!trades_receiver_id_fkey(username), user2:profiles!trades_user2_id_fkey(username),
items:trade_items(*) items:trade_items(*)
`) `)
.eq('status', 'pending') .eq('status', 'pending')
.or(`sender_id.eq.${userId},receiver_id.eq.${userId}`) .or(`user1_id.eq.${userId},user2_id.eq.${userId}`)
.order('created_at', { ascending: false }); .order('created_at', { ascending: false });
if (error) throw error; if (error) throw error;
@@ -70,8 +100,8 @@ export async function getTradeById(tradeId: string): Promise<Trade | null> {
.from('trades') .from('trades')
.select(` .select(`
*, *,
sender:profiles!trades_sender_id_fkey(username), user1:profiles!trades_user1_id_fkey(username),
receiver:profiles!trades_receiver_id_fkey(username), user2:profiles!trades_user2_id_fkey(username),
items:trade_items(*) items:trade_items(*)
`) `)
.eq('id', tradeId) .eq('id', tradeId)
@@ -83,39 +113,40 @@ export async function getTradeById(tradeId: string): Promise<Trade | null> {
// Create a new trade with items // Create a new trade with items
export async function createTrade(params: CreateTradeParams): Promise<Trade> { export async function createTrade(params: CreateTradeParams): Promise<Trade> {
const { senderId, receiverId, message, senderCards, receiverCards } = params; const { user1Id, user2Id, message, user1Cards, user2Cards } = params;
// Create the trade // Create the trade
const { data: trade, error: tradeError } = await supabase const { data: trade, error: tradeError } = await supabase
.from('trades') .from('trades')
.insert({ .insert({
sender_id: senderId, user1_id: user1Id,
receiver_id: receiverId, user2_id: user2Id,
message, message,
status: 'pending', status: 'pending',
// editor_id starts as null - gets set when someone edits the trade
}) })
.select() .select()
.single(); .single();
if (tradeError) throw tradeError; if (tradeError) throw tradeError;
// Add sender's cards // Add user1's cards
const senderItems = senderCards.map((card) => ({ const user1Items = user1Cards.map((card) => ({
trade_id: trade.id, trade_id: trade.id,
owner_id: senderId, owner_id: user1Id,
card_id: card.cardId, card_id: card.cardId,
quantity: card.quantity, quantity: card.quantity,
})); }));
// Add receiver's cards (what sender wants) // Add user2's cards
const receiverItems = receiverCards.map((card) => ({ const user2Items = user2Cards.map((card) => ({
trade_id: trade.id, trade_id: trade.id,
owner_id: receiverId, owner_id: user2Id,
card_id: card.cardId, card_id: card.cardId,
quantity: card.quantity, quantity: card.quantity,
})); }));
const allItems = [...senderItems, ...receiverItems]; const allItems = [...user1Items, ...user2Items];
if (allItems.length > 0) { if (allItems.length > 0) {
const { error: itemsError } = await supabase const { error: itemsError } = await supabase
@@ -130,6 +161,25 @@ export async function createTrade(params: CreateTradeParams): Promise<Trade> {
// Accept a trade (executes the card transfer) // Accept a trade (executes the card transfer)
export async function acceptTrade(tradeId: string): Promise<boolean> { export async function acceptTrade(tradeId: string): Promise<boolean> {
// First check if the trade is valid
const { data: trade, error: tradeError } = await supabase
.from('trades')
.select('is_valid, status')
.eq('id', tradeId)
.single();
if (tradeError) throw tradeError;
// Prevent accepting invalid trades
if (!trade.is_valid) {
throw new Error('This trade is no longer valid. One or more cards are no longer available in the required quantities.');
}
// Prevent accepting non-pending trades
if (trade.status !== 'pending') {
throw new Error('This trade has already been processed.');
}
const { data, error } = await supabase.rpc('execute_trade', { const { data, error } = await supabase.rpc('execute_trade', {
trade_id: tradeId, trade_id: tradeId,
}); });
@@ -170,11 +220,11 @@ export async function getTradeHistory(userId: string): Promise<Trade[]> {
.from('trades') .from('trades')
.select(` .select(`
*, *,
sender:profiles!trades_sender_id_fkey(username), user1:profiles!trades_user1_id_fkey(username),
receiver:profiles!trades_receiver_id_fkey(username), user2:profiles!trades_user2_id_fkey(username),
items:trade_items(*) items:trade_items(*)
`) `)
.or(`sender_id.eq.${userId},receiver_id.eq.${userId}`) .or(`user1_id.eq.${userId},user2_id.eq.${userId}`)
.in('status', ['accepted', 'declined', 'cancelled']) .in('status', ['accepted', 'declined', 'cancelled'])
.order('updated_at', { ascending: false }) .order('updated_at', { ascending: false })
.limit(50); .limit(50);
@@ -182,3 +232,116 @@ export async function getTradeHistory(userId: string): Promise<Trade[]> {
if (error) throw error; if (error) throw error;
return data as Trade[]; return data as Trade[];
} }
// Update an existing trade (for edits and counter-offers)
export async function updateTrade(params: UpdateTradeParams): Promise<Trade> {
const { tradeId, editorId, message, myCards, theirCards } = params;
// Get current trade info
const { data: currentTrade, error: tradeError } = await supabase
.from('trades')
.select('version, user1_id, user2_id')
.eq('id', tradeId)
.single();
if (tradeError) throw tradeError;
const newVersion = (currentTrade.version || 1) + 1;
// Determine the other user's ID
const otherUserId = currentTrade.user1_id === editorId
? currentTrade.user2_id
: currentTrade.user1_id;
// Save current state to history before updating
const { data: historyEntry, error: historyError } = await supabase
.from('trade_history')
.insert({
trade_id: tradeId,
version: currentTrade.version || 1,
editor_id: editorId,
message: message || null,
})
.select()
.single();
if (historyError) throw historyError;
// Save current items to history
const { data: currentItems } = await supabase
.from('trade_items')
.select('*')
.eq('trade_id', tradeId);
if (currentItems && currentItems.length > 0) {
const historyItems = currentItems.map(item => ({
history_id: historyEntry.id,
owner_id: item.owner_id,
card_id: item.card_id,
quantity: item.quantity,
}));
await supabase.from('trade_history_items').insert(historyItems);
}
// Update the trade
const { data: updatedTrade, error: updateError } = await supabase
.from('trades')
.update({
message,
version: newVersion,
editor_id: editorId,
updated_at: new Date().toISOString(),
})
.eq('id', tradeId)
.select()
.single();
if (updateError) throw updateError;
// Delete existing items
await supabase.from('trade_items').delete().eq('trade_id', tradeId);
// Add new items (myCards belong to editor, theirCards belong to other user)
const myItems = myCards.map((card) => ({
trade_id: tradeId,
owner_id: editorId,
card_id: card.cardId,
quantity: card.quantity,
}));
const theirItems = theirCards.map((card) => ({
trade_id: tradeId,
owner_id: otherUserId,
card_id: card.cardId,
quantity: card.quantity,
}));
const allItems = [...myItems, ...theirItems];
if (allItems.length > 0) {
const { error: itemsError } = await supabase
.from('trade_items')
.insert(allItems);
if (itemsError) throw itemsError;
}
return updatedTrade;
}
// Get version history for a trade
export async function getTradeVersionHistory(tradeId: string): Promise<TradeHistoryEntry[]> {
const { data, error } = await supabase
.from('trade_history')
.select(`
*,
editor:profiles!trade_history_editor_id_fkey(username),
items:trade_history_items(*)
`)
.eq('trade_id', tradeId)
.order('version', { ascending: true });
if (error) throw error;
return data as TradeHistoryEntry[];
}

View File

@@ -55,6 +55,11 @@ export interface Deck {
userId: string; userId: string;
createdAt: Date; createdAt: Date;
updatedAt: Date; updatedAt: Date;
coverCardId?: string;
coverCard?: Card | null;
validationErrors?: string[];
isValid?: boolean;
cardCount?: number;
} }
export interface CardEntity { export interface CardEntity {

View File

@@ -1,10 +1,23 @@
import { Deck } from '../types'; import { Card, Deck } from '../types';
interface DeckValidation { interface DeckValidation {
isValid: boolean; isValid: boolean;
errors: string[]; errors: string[];
} }
// Helper function to get commander color identity
function getCommanderColors(commander: Card | null): string[] {
if (!commander) return [];
return commander.colors || [];
}
// Helper function to check if a card's colors are valid for the commander
function isCardValidForCommander(card: Card, commanderColors: string[]): boolean {
if (commanderColors.length === 0) return true;
const cardColors = card.colors || [];
return cardColors.every(color => commanderColors.includes(color));
}
const FORMAT_RULES = { const FORMAT_RULES = {
standard: { standard: {
minCards: 60, minCards: 60,
@@ -74,6 +87,25 @@ export function validateDeck(deck: Deck): DeckValidation {
} }
}); });
// Commander-specific validations
if (deck.format === 'commander') {
const commander = deck.cards.find(card => card.is_commander)?.card;
if (!commander) {
errors.push('Commander deck must have a commander');
} else {
// Check commander color identity
const commanderColors = getCommanderColors(commander);
const invalidCards = deck.cards.filter(({ card, is_commander }) =>
!is_commander && !isCardValidForCommander(card, commanderColors)
);
if (invalidCards.length > 0) {
errors.push(`Some cards don't match commander's color identity`);
}
}
}
return { return {
isValid: errors.length === 0, isValid: errors.length === 0,
errors, errors,

View File

@@ -0,0 +1,116 @@
import { supabase } from '../lib/supabase';
import { getCardsByIds } from '../services/api';
import { validateDeck } from './deckValidation';
import { Deck } from '../types';
/**
* Migrate existing decks to include optimization fields
* This should be run once to update all existing decks
*/
export async function migrateExistingDecks() {
console.log('Starting deck migration...');
// Get all decks
const { data: decksData, error: decksError } = await supabase
.from('decks')
.select('*');
if (decksError) {
console.error('Error fetching decks:', decksError);
return;
}
console.log(`Found ${decksData.length} decks to migrate`);
for (const deck of decksData) {
// Skip if already migrated
if (deck.cover_card_id && deck.card_count !== null) {
console.log(`Deck ${deck.name} already migrated, skipping`);
continue;
}
console.log(`Migrating deck: ${deck.name}`);
// Get deck cards
const { data: cardEntities, error: cardsError } = await supabase
.from('deck_cards')
.select('*')
.eq('deck_id', deck.id);
if (cardsError || !cardEntities || cardEntities.length === 0) {
console.error(`Error fetching cards for deck ${deck.id}:`, cardsError);
continue;
}
const cardIds = cardEntities.map(entity => entity.card_id);
const uniqueCardIds = [...new Set(cardIds)];
try {
// Fetch cards from Scryfall
const scryfallCards = await getCardsByIds(uniqueCardIds);
if (!scryfallCards) {
console.error(`Failed to fetch cards for deck ${deck.id}`);
continue;
}
const cards = cardEntities.map(entity => {
const card = scryfallCards.find(c => c.id === entity.card_id);
return {
card,
quantity: entity.quantity,
is_commander: entity.is_commander,
};
});
// Create deck object for validation
const deckToValidate: Deck = {
id: deck.id,
name: deck.name,
format: deck.format,
cards,
userId: deck.user_id,
createdAt: new Date(deck.created_at),
updatedAt: new Date(deck.updated_at),
};
// Calculate validation
const validation = validateDeck(deckToValidate);
// Determine cover card (commander or first card)
const commanderCard = deck.format === 'commander'
? cardEntities.find(c => c.is_commander)
: null;
const coverCardId = commanderCard
? commanderCard.card_id
: cardEntities[0]?.card_id || null;
// Calculate total card count
const totalCardCount = cardEntities.reduce((acc, curr) => acc + curr.quantity, 0);
// Update deck with optimization fields
const { error: updateError } = await supabase
.from('decks')
.update({
cover_card_id: coverCardId,
validation_errors: validation.errors,
is_valid: validation.isValid,
card_count: totalCardCount,
})
.eq('id', deck.id);
if (updateError) {
console.error(`Error updating deck ${deck.id}:`, updateError);
} else {
console.log(`✓ Migrated deck: ${deck.name}`);
}
// Small delay to avoid rate limiting
await new Promise(resolve => setTimeout(resolve, 100));
} catch (error) {
console.error(`Error processing deck ${deck.id}:`, error);
}
}
console.log('Migration complete!');
}

View File

@@ -0,0 +1,273 @@
/*
# Friends, Trades, and Collection Visibility
1. Changes to profiles
- Add `collection_visibility` column (public, friends, private)
2. New Tables
- `friendships` - Friend relationships between users
- `trades` - Trade offers between users
- `trade_items` - Cards included in trades
3. Security
- RLS policies for all new tables
- Updated collection policies for visibility
*/
-- Add collection visibility to profiles
ALTER TABLE public.profiles
ADD COLUMN collection_visibility text DEFAULT 'private'
CHECK (collection_visibility IN ('public', 'friends', 'private'));
-- =============================================
-- FRIENDSHIPS TABLE
-- =============================================
CREATE TABLE public.friendships (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
requester_id uuid REFERENCES public.profiles(id) ON DELETE CASCADE NOT NULL,
addressee_id uuid REFERENCES public.profiles(id) ON DELETE CASCADE NOT NULL,
status text DEFAULT 'pending' CHECK (status IN ('pending', 'accepted', 'declined')),
created_at timestamptz DEFAULT now(),
updated_at timestamptz DEFAULT now(),
UNIQUE(requester_id, addressee_id),
CHECK (requester_id != addressee_id)
);
ALTER TABLE public.friendships ENABLE ROW LEVEL SECURITY;
-- Users can see friendships they're involved in
CREATE POLICY "Users can view their friendships"
ON public.friendships
FOR SELECT
TO authenticated
USING (requester_id = auth.uid() OR addressee_id = auth.uid());
-- Users can create friend requests
CREATE POLICY "Users can send friend requests"
ON public.friendships
FOR INSERT
TO authenticated
WITH CHECK (requester_id = auth.uid());
-- Users can update friendships they received (accept/decline)
CREATE POLICY "Users can respond to friend requests"
ON public.friendships
FOR UPDATE
TO authenticated
USING (addressee_id = auth.uid());
-- Users can delete their own friendships
CREATE POLICY "Users can delete their friendships"
ON public.friendships
FOR DELETE
TO authenticated
USING (requester_id = auth.uid() OR addressee_id = auth.uid());
-- =============================================
-- TRADES TABLE
-- =============================================
CREATE TABLE public.trades (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
sender_id uuid REFERENCES public.profiles(id) ON DELETE CASCADE NOT NULL,
receiver_id uuid REFERENCES public.profiles(id) ON DELETE CASCADE NOT NULL,
status text DEFAULT 'pending' CHECK (status IN ('pending', 'accepted', 'declined', 'cancelled')),
message text,
created_at timestamptz DEFAULT now(),
updated_at timestamptz DEFAULT now(),
CHECK (sender_id != receiver_id)
);
ALTER TABLE public.trades ENABLE ROW LEVEL SECURITY;
-- Users can see trades they're involved in
CREATE POLICY "Users can view their trades"
ON public.trades
FOR SELECT
TO authenticated
USING (sender_id = auth.uid() OR receiver_id = auth.uid());
-- Users can create trades
CREATE POLICY "Users can create trades"
ON public.trades
FOR INSERT
TO authenticated
WITH CHECK (sender_id = auth.uid());
-- Sender can cancel, receiver can accept/decline
CREATE POLICY "Users can update their trades"
ON public.trades
FOR UPDATE
TO authenticated
USING (sender_id = auth.uid() OR receiver_id = auth.uid());
-- =============================================
-- TRADE ITEMS TABLE
-- =============================================
CREATE TABLE public.trade_items (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
trade_id uuid REFERENCES public.trades(id) ON DELETE CASCADE NOT NULL,
owner_id uuid REFERENCES public.profiles(id) ON DELETE CASCADE NOT NULL,
card_id text NOT NULL,
quantity integer DEFAULT 1 CHECK (quantity > 0),
created_at timestamptz DEFAULT now()
);
ALTER TABLE public.trade_items ENABLE ROW LEVEL SECURITY;
-- Users can see items in their trades
CREATE POLICY "Users can view trade items"
ON public.trade_items
FOR SELECT
TO authenticated
USING (
EXISTS (
SELECT 1 FROM public.trades
WHERE trades.id = trade_items.trade_id
AND (trades.sender_id = auth.uid() OR trades.receiver_id = auth.uid())
)
);
-- Users can add items to trades they created
CREATE POLICY "Users can add trade items"
ON public.trade_items
FOR INSERT
TO authenticated
WITH CHECK (
EXISTS (
SELECT 1 FROM public.trades
WHERE trades.id = trade_items.trade_id
AND trades.sender_id = auth.uid()
AND trades.status = 'pending'
)
);
-- =============================================
-- UPDATE COLLECTION POLICIES FOR VISIBILITY
-- =============================================
-- Drop old restrictive policy
DROP POLICY IF EXISTS "Users can view their own collection" ON public.collections;
-- New policy: view own collection OR public collections OR friend's collections (if friends visibility)
CREATE POLICY "Users can view collections based on visibility"
ON public.collections
FOR SELECT
TO authenticated
USING (
user_id = auth.uid()
OR EXISTS (
SELECT 1 FROM public.profiles
WHERE profiles.id = collections.user_id
AND profiles.collection_visibility = 'public'
)
OR EXISTS (
SELECT 1 FROM public.profiles p
JOIN public.friendships f ON (
(f.requester_id = p.id AND f.addressee_id = auth.uid())
OR (f.addressee_id = p.id AND f.requester_id = auth.uid())
)
WHERE p.id = collections.user_id
AND p.collection_visibility = 'friends'
AND f.status = 'accepted'
)
);
-- =============================================
-- UPDATE PROFILES POLICY FOR PUBLIC VIEWING
-- =============================================
-- Drop old restrictive policy
DROP POLICY IF EXISTS "Users can view their own profile" ON public.profiles;
-- New policy: users can view all profiles (needed for friend search and public collections)
CREATE POLICY "Users can view profiles"
ON public.profiles
FOR SELECT
TO authenticated
USING (true);
-- =============================================
-- FUNCTION: Execute trade (transfer cards)
-- =============================================
CREATE OR REPLACE FUNCTION public.execute_trade(trade_id uuid)
RETURNS boolean
LANGUAGE plpgsql
SECURITY DEFINER
AS $$
DECLARE
v_trade RECORD;
v_item RECORD;
BEGIN
-- Get the trade
SELECT * INTO v_trade FROM public.trades WHERE id = trade_id;
-- Check trade exists and is pending
IF v_trade IS NULL OR v_trade.status != 'pending' THEN
RETURN false;
END IF;
-- Check caller is the receiver
IF v_trade.receiver_id != auth.uid() THEN
RETURN false;
END IF;
-- Process each trade item
FOR v_item IN SELECT * FROM public.trade_items WHERE trade_items.trade_id = execute_trade.trade_id
LOOP
-- Determine new owner
DECLARE
v_new_owner uuid;
BEGIN
IF v_item.owner_id = v_trade.sender_id THEN
v_new_owner := v_trade.receiver_id;
ELSE
v_new_owner := v_trade.sender_id;
END IF;
-- Remove from old owner's collection
UPDATE public.collections
SET quantity = quantity - v_item.quantity,
updated_at = now()
WHERE user_id = v_item.owner_id
AND card_id = v_item.card_id;
-- Delete if quantity is 0 or less
DELETE FROM public.collections
WHERE user_id = v_item.owner_id
AND card_id = v_item.card_id
AND quantity <= 0;
-- Add to new owner's collection
INSERT INTO public.collections (user_id, card_id, quantity)
VALUES (v_new_owner, v_item.card_id, v_item.quantity)
ON CONFLICT (user_id, card_id)
DO UPDATE SET
quantity = collections.quantity + v_item.quantity,
updated_at = now();
END;
END LOOP;
-- Mark trade as accepted
UPDATE public.trades
SET status = 'accepted', updated_at = now()
WHERE id = trade_id;
RETURN true;
END;
$$;
-- Add unique constraint on collections for upsert
ALTER TABLE public.collections
ADD CONSTRAINT collections_user_card_unique UNIQUE (user_id, card_id);
-- =============================================
-- INDEXES FOR PERFORMANCE
-- =============================================
CREATE INDEX idx_friendships_requester ON public.friendships(requester_id);
CREATE INDEX idx_friendships_addressee ON public.friendships(addressee_id);
CREATE INDEX idx_friendships_status ON public.friendships(status);
CREATE INDEX idx_trades_sender ON public.trades(sender_id);
CREATE INDEX idx_trades_receiver ON public.trades(receiver_id);
CREATE INDEX idx_trades_status ON public.trades(status);
CREATE INDEX idx_trade_items_trade ON public.trade_items(trade_id);
CREATE INDEX idx_profiles_visibility ON public.profiles(collection_visibility);

View File

@@ -0,0 +1,78 @@
-- Create trade_history table to track all versions of a trade
CREATE TABLE IF NOT EXISTS public.trade_history (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
trade_id uuid REFERENCES public.trades(id) ON DELETE CASCADE NOT NULL,
version integer NOT NULL,
editor_id uuid REFERENCES public.profiles(id) NOT NULL,
message text,
created_at timestamptz DEFAULT now(),
UNIQUE(trade_id, version)
);
-- Create trade_history_items table to store cards for each version
CREATE TABLE IF NOT EXISTS public.trade_history_items (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
history_id uuid REFERENCES public.trade_history(id) ON DELETE CASCADE NOT NULL,
owner_id uuid REFERENCES public.profiles(id) NOT NULL,
card_id text NOT NULL,
quantity integer DEFAULT 1,
created_at timestamptz DEFAULT now()
);
-- Add version column to trades table to track current version
ALTER TABLE public.trades ADD COLUMN IF NOT EXISTS version integer DEFAULT 1;
-- Add editor_id to track who last edited the trade
ALTER TABLE public.trades ADD COLUMN IF NOT EXISTS editor_id uuid REFERENCES public.profiles(id);
-- Create indexes for better performance
CREATE INDEX IF NOT EXISTS idx_trade_history_trade_id ON public.trade_history(trade_id);
CREATE INDEX IF NOT EXISTS idx_trade_history_items_history_id ON public.trade_history_items(history_id);
-- Enable RLS
ALTER TABLE public.trade_history ENABLE ROW LEVEL SECURITY;
ALTER TABLE public.trade_history_items ENABLE ROW LEVEL SECURITY;
-- RLS policies for trade_history
CREATE POLICY "Users can view history of their trades"
ON public.trade_history FOR SELECT
USING (
EXISTS (
SELECT 1 FROM public.trades
WHERE trades.id = trade_history.trade_id
AND (trades.sender_id = auth.uid() OR trades.receiver_id = auth.uid())
)
);
CREATE POLICY "Users can create history for their trades"
ON public.trade_history FOR INSERT
WITH CHECK (
EXISTS (
SELECT 1 FROM public.trades
WHERE trades.id = trade_history.trade_id
AND (trades.sender_id = auth.uid() OR trades.receiver_id = auth.uid())
)
);
-- RLS policies for trade_history_items
CREATE POLICY "Users can view history items of their trades"
ON public.trade_history_items FOR SELECT
USING (
EXISTS (
SELECT 1 FROM public.trade_history th
JOIN public.trades t ON t.id = th.trade_id
WHERE th.id = trade_history_items.history_id
AND (t.sender_id = auth.uid() OR t.receiver_id = auth.uid())
)
);
CREATE POLICY "Users can create history items for their trades"
ON public.trade_history_items FOR INSERT
WITH CHECK (
EXISTS (
SELECT 1 FROM public.trade_history th
JOIN public.trades t ON t.id = th.trade_id
WHERE th.id = trade_history_items.history_id
AND (t.sender_id = auth.uid() OR t.receiver_id = auth.uid())
)
);

View File

@@ -0,0 +1,9 @@
-- Add price_usd column to collections table
ALTER TABLE collections
ADD COLUMN IF NOT EXISTS price_usd DECIMAL(10, 2) DEFAULT 0;
-- Create index for faster price calculations
CREATE INDEX IF NOT EXISTS idx_collections_price ON collections(price_usd);
-- Add comment
COMMENT ON COLUMN collections.price_usd IS 'USD price of the card at time of addition/update';

View File

@@ -0,0 +1,101 @@
-- Add is_valid column to trades table
ALTER TABLE public.trades
ADD COLUMN IF NOT EXISTS is_valid BOOLEAN DEFAULT true;
-- Create index for filtering by validity
CREATE INDEX IF NOT EXISTS idx_trades_is_valid ON public.trades(is_valid);
-- Function to validate if a trade can still be executed based on current collections
CREATE OR REPLACE FUNCTION public.validate_trade(p_trade_id uuid)
RETURNS boolean
LANGUAGE plpgsql
SECURITY DEFINER
AS $$
DECLARE
v_item RECORD;
v_collection_quantity integer;
BEGIN
-- Check each item in the trade
FOR v_item IN
SELECT owner_id, card_id, quantity
FROM public.trade_items
WHERE trade_id = p_trade_id
LOOP
-- Get the quantity of this card in the owner's collection
SELECT COALESCE(quantity, 0) INTO v_collection_quantity
FROM public.collections
WHERE user_id = v_item.owner_id
AND card_id = v_item.card_id;
-- If owner doesn't have enough of this card, trade is invalid
IF v_collection_quantity < v_item.quantity THEN
RETURN false;
END IF;
END LOOP;
-- All items are available, trade is valid
RETURN true;
END;
$$;
-- Function to check and update validity of affected trades when collections change
CREATE OR REPLACE FUNCTION public.update_affected_trades_validity()
RETURNS TRIGGER
LANGUAGE plpgsql
SECURITY DEFINER
AS $$
DECLARE
v_user_id uuid;
v_card_id text;
v_trade RECORD;
v_is_valid boolean;
BEGIN
-- Get the user_id and card_id from the changed row
IF (TG_OP = 'DELETE') THEN
v_user_id := OLD.user_id;
v_card_id := OLD.card_id;
ELSE
v_user_id := NEW.user_id;
v_card_id := NEW.card_id;
END IF;
-- Find all pending trades that involve this card from this user
FOR v_trade IN
SELECT DISTINCT t.id
FROM public.trades t
JOIN public.trade_items ti ON ti.trade_id = t.id
WHERE t.status = 'pending'
AND ti.owner_id = v_user_id
AND ti.card_id = v_card_id
LOOP
-- Validate the trade
v_is_valid := public.validate_trade(v_trade.id);
-- Update the trade's validity
UPDATE public.trades
SET is_valid = v_is_valid,
updated_at = now()
WHERE id = v_trade.id;
END LOOP;
IF (TG_OP = 'DELETE') THEN
RETURN OLD;
ELSE
RETURN NEW;
END IF;
END;
$$;
-- Create trigger to auto-update trade validity when collections change
CREATE TRIGGER update_trades_on_collection_change
AFTER UPDATE OR DELETE ON public.collections
FOR EACH ROW
EXECUTE FUNCTION public.update_affected_trades_validity();
-- Add comment
COMMENT ON COLUMN public.trades.is_valid IS 'Indicates if the trade can still be executed based on current collections. Auto-updated when collections change.';
-- Initial validation: set is_valid for all existing pending trades
UPDATE public.trades
SET is_valid = public.validate_trade(id)
WHERE status = 'pending';

File diff suppressed because one or more lines are too long