-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathUserOwnedItems.tsx
234 lines (200 loc) · 7.43 KB
/
UserOwnedItems.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import React, { useState, useEffect } from "react";
import { useSupabaseClient, useSession } from "@supabase/auth-helpers-react";
import Link from "next/link";
const OwnedItemsList: React.FC = () => {
const supabase = useSupabaseClient();
const session = useSession();
const [itemDetails, setItemDetails] = useState([]);
useEffect(() => {
const fetchOwnedItems = async () => {
try {
if (!session) return;
const user = session.user.id;
const { data: ownedItemsData, error: ownedItemsError } = await supabase
.from('inventoryUSERS')
.select('*')
.eq('owner', user);
if (ownedItemsError) {
throw ownedItemsError;
}
if (ownedItemsData) {
const itemIds = ownedItemsData.map(item => item.item);
const { data: itemDetailsData, error: itemDetailsError } = await supabase
.from('inventoryITEMS')
.select('*')
.in('id', itemIds)
.gt('id', 10);
if (itemDetailsError) {
throw itemDetailsError;
}
if (itemDetailsData) {
setItemDetails(itemDetailsData.slice(0, 8)); // Limit
}
}
} catch (error) {
console.error('Error fetching owned items:', error.message);
}
};
fetchOwnedItems();
}, [session]);
// Function to calculate the position of each item around the circle
const calculatePosition = (index: number, totalItems: number) => {
const angle = (index / totalItems) * 360;
const radius = 15; // Adjust as needed
const centerX = 15; // Adjust as needed
const centerY = 175; // Adjust as needed
const x = centerX + radius * Math.cos(angle);
const y = centerY + radius * Math.sin(angle);
return { x, y };
};
const radius = 100;
return (
<div className="relative w-80 h-80 flex -mx-20">
{itemDetails.map((item, index) => {
const angle = (index / itemDetails.length) * 2 * Math.PI;
const x = Math.cos(angle) * radius;
const y = Math.sin(angle) * radius;
return (
<div
key={item.id}
className="absolute flex flex-col items-center justify-center -mx-10 pb-3"
style={{ top: `calc(50% - ${y}px)`, left: `calc(50% + ${x}px)` }}
>
<div className="w-20 h-20 rounded-full overflow-hidden mb-2">
<img src={item.icon_url} alt={item.name} className="w-full h-full object-cover" />
</div>
<p className="text-xs text-center">{item.name}</p>
</div>
);
})}
</div>
);
};
export default OwnedItemsList;
interface InventoryItem {
id: number;
name: string;
icon_url: string;
quantity: number;
}
export const ItemsVerticalList: React.FC = () => {
const session = useSession();
const [itemDetails, setItemDetails] = useState<InventoryItem[]>([]);
const supabase = useSupabaseClient();
useEffect(() => {
const fetchOwnedItems = async () => {
try {
if (!session) return;
const user = session.user.id;
// Fetch owned items from the database
const { data: ownedItemsData, error: ownedItemsError } = await supabase
.from('inventoryUSERS')
.select('*')
.eq('owner', user)
.gt('id', 20);
if (ownedItemsError) {
throw ownedItemsError;
}
if (ownedItemsData) {
const itemIds = ownedItemsData.map(item => item.item);
// Fetch details of owned items
const { data: itemDetailsData, error: itemDetailsError } = await supabase
.from('inventoryITEMS')
.select('*')
.in('id', itemIds);
if (itemDetailsError) {
throw itemDetailsError;
}
if (itemDetailsData) {
setItemDetails(itemDetailsData);
}
}
} catch (error) {
console.error('Error fetching owned items:', error.message);
}
};
fetchOwnedItems();
}, [session]);
return (
<div className="w-full">
{itemDetails.map(item => (
<div key={item.id} className="flex items-center justify-between mb-2">
<div className="flex items-center space-x-2">
<div className="w-10 h-10 rounded-full overflow-hidden">
<img src={item.icon_url} alt={item.name} className="w-full h-full object-cover" />
</div>
<p className="text-sm">{item.name}</p>
</div>
<p className="text-sm">x{item.quantity}</p>
</div>
))}
</div>
);
};
export const SectorStructureOwned: React.FC<{ sectorid: string }> = ({ sectorid }) => {
const supabase = useSupabaseClient();
const session = useSession();
const [ownedItems, setOwnedItems] = useState([]);
const [itemDetails, setItemDetails] = useState([]);
// Add support for moving items/entities between planets
useEffect(() => {
async function fetchOwnedItems() {
if (session) {
try {
const user = session.user.id;
const { data: ownedItemsData, error: ownedItemsError } = await supabase
.from('inventoryUSERS')
.select('*')
.eq("planetSector" || 'sector', sectorid);
if (ownedItemsError) {
throw ownedItemsError;
}
if (ownedItemsData) {
setOwnedItems(ownedItemsData);
}
} catch (error) {
console.error('Error fetching owned items:', error);
}
}
}
fetchOwnedItems();
}, [session]);
useEffect(() => {
async function fetchItemDetails() {
if (ownedItems.length > 0) {
const itemIds = ownedItems.map(item => item.item);
const { data: itemDetailsData, error: itemDetailsError } = await supabase
.from('inventoryITEMS')
.select('*')
.in('id', itemIds);
if (itemDetailsError) {
console.error('Error fetching item details:', itemDetailsError);
}
if (itemDetailsData) {
setItemDetails(itemDetailsData);
}
}
}
fetchItemDetails();
}, [ownedItems]);
return (
<div className="bg-gray-100 p-4">
<h2 className="text-2xl font-semibold mb-4">Your Items</h2>
<ul className="grid gap-4 grid-cols-2 sm:grid-cols-3 md:grid-cols-4 lg:grid-cols-5">
{itemDetails.map(item => {
const ownedItem = ownedItems.find(ownedItem => ownedItem.item === item.id);
return (
<li key={item.id} className="bg-white shadow-md p-4 rounded-md">
<h3 className="text-lg font-medium mb-2">{item.name}</h3>
<div className="mb-2">
<img src={item.icon_url} alt={item.name} className="w-full h-auto" />
</div>
<p className="text-gray-600">Quantity: {ownedItem?.quantity}</p>
<p className="text-gray-600">On planet: {ownedItem?.sector}</p>
</li>
);
})}
</ul>
</div>
);
};