Skip to content

Commit b9858af

Browse files
committed
πŸŽ€πŸ§‚ ↝ [SSM-37 SSM-23]: Some fixes for the structure fetching process
1 parent c48d678 commit b9858af

File tree

11 files changed

+65
-973
lines changed

11 files changed

+65
-973
lines changed

β€Žapp/page.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ export default function Home() {
9494
<AllAutomatonsOnActivePlanet />
9595
</center>
9696
</div>
97-
{/* <div className="w-full py-2"><StructureMissionGuide />
98-
</div> */}
97+
<div className="w-full py-2"><StructureMissionGuide />
98+
</div>
9999
</EarthViewLayout>
100100
// 60: <SaturnView />,
101101
// 62: <TitanView />,

β€Žapp/scenes/globe/Structures.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { StructuresConfigForSandbox } from "@/constants/Structures/SandboxProper
88
import { InventoryStructureItem, StructureItemDetail } from "@/types/Items";
99
import { useActivePlanet } from "@/context/ActivePlanet";
1010
import "../../../styles/Anims/StarterStructureAnimations.css";
11-
import { CreateCommunityStation } from "@/components/Structures/Build/MakeCommunityStation";
11+
// import { CreateCommunityStation } from "@/components/Structures/Build/MakeCommunityStation";
1212

1313
interface StructuresOnPlanetProps {
1414
onStructuresFetch: (

β€Žapp/starnet/consensus/page.tsx

+2-4
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,12 @@ import React, { useEffect, useState } from "react";
44
import StarnetLayout from "@/components/Layout/Starnet";
55
import { ExoplanetTransitHunter } from "@/components/Projects/Telescopes/ExoplanetC23";
66
import ProfileCardModal from "@/components/profile/form";
7-
import { CommunityScienceStation } from "@/components/Structures/Community/StationModal";
8-
import { CreateCommunityStation } from "@/components/Structures/Build/MakeCommunityStation";
9-
import StationsOnPlanet from "@/components/Structures/Community/ViewAllStations";
107

118
export default function TestPage() {
129
return (
1310
<StarnetLayout>
14-
<StationsOnPlanet />
11+
{/* <StationsOnPlanet /> */}
12+
<ProfileCardModal />
1513
</StarnetLayout>
1614
);
1715
};

β€Žcomponents/Missions/Requests.tsx

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import { Button } from "@/components/ui/button";
66
import { Badge } from "@/components/ui/badge";
77
import { Lock, Rocket, MapPin, Building, Star, Clock } from "lucide-react";
88
import { useSupabaseClient } from "@supabase/auth-helpers-react";
9-
import { CreateCommunityStation } from "../Structures/Build/MakeCommunityStation";
109

1110
type CommunityMission = {
1211
id: number;

β€Žcomponents/Structures/Build/CreateDedicatedStructure.tsx

-116
This file was deleted.

β€Žcomponents/Structures/Build/EditMode.tsx

+58-74
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
"use client";
2-
3-
import React, { useEffect, useState } from 'react';
1+
import React, { useEffect, useState, useCallback } from 'react';
42
import { useSession, useSupabaseClient } from '@supabase/auth-helpers-react';
53
import { useActivePlanet } from '@/context/ActivePlanet';
64
import { Plus } from "lucide-react";
@@ -27,66 +25,61 @@ export function UnownedSurfaceStructures() {
2725
const [error, setError] = useState<string | null>(null);
2826
const [open, setOpen] = useState(false);
2927
const [selectedStructure, setSelectedStructure] = useState<InventoryItem | null>(null);
28+
const [fetchTrigger, setFetchTrigger] = useState(0); // Track changes for re-fetching
3029

31-
useEffect(() => {
32-
async function fetchStructures() {
33-
if (!session || !activePlanet) {
34-
setLoading(false);
35-
return;
36-
};
37-
38-
try {
39-
const { data: researchedStructures, error: researchError } = await supabase
40-
.from('researched')
41-
.select('tech_type')
42-
.eq('user_id', session.user.id);
43-
44-
if (researchError) {
45-
throw researchError;
46-
};
47-
48-
const researchedIds = researchedStructures.map((item: { tech_type: string }) => Number(item.tech_type));
49-
50-
const { data: userInventory, error: inventoryError } = await supabase
51-
.from('inventory')
52-
.select('item')
53-
.eq('owner', session.user.id)
54-
.eq('anomaly', activePlanet.id);
55-
56-
if (inventoryError) {
57-
throw inventoryError;
58-
};
59-
60-
const ownedItems = userInventory.map((item: { item: number }) => item.item);
61-
62-
const response = await fetch('/api/gameplay/inventory');
63-
const inventoryItems: InventoryItem[] = await response.json();
64-
65-
const surfaceStructures = inventoryItems.filter(item =>
66-
item.ItemCategory === 'Structure' && item.locationType === 'Surface' && researchedIds.includes(item.id)
67-
);
68-
69-
const unowned = surfaceStructures.filter(structure => !ownedItems.includes(structure.id));
70-
71-
setUnownedStructures(unowned);
72-
} catch (error) {
73-
console.error('Error fetching structures:', error);
74-
setError('Failed to load structures.');
75-
} finally {
76-
setLoading(false);
77-
};
30+
const fetchStructures = useCallback(async () => {
31+
if (!session || !activePlanet) {
32+
setLoading(false);
33+
return;
34+
};
35+
36+
try {
37+
const { data: researchedStructures, error: researchError } = await supabase
38+
.from('researched')
39+
.select('tech_type')
40+
.eq('user_id', session.user.id);
41+
42+
if (researchError) throw researchError;
43+
44+
const researchedIds = researchedStructures.map((item: { tech_type: string }) => Number(item.tech_type));
45+
46+
const { data: userInventory, error: inventoryError } = await supabase
47+
.from('inventory')
48+
.select('item')
49+
.eq('owner', session.user.id)
50+
.eq('anomaly', activePlanet.id);
51+
52+
if (inventoryError) throw inventoryError;
53+
54+
const ownedItems = userInventory.map((item: { item: number }) => item.item);
55+
56+
const response = await fetch('/api/gameplay/inventory');
57+
const inventoryItems: InventoryItem[] = await response.json();
58+
59+
const surfaceStructures = inventoryItems.filter(item =>
60+
item.ItemCategory === 'Structure' && item.locationType === 'Surface' && researchedIds.includes(item.id)
61+
);
62+
63+
const unowned = surfaceStructures.filter(structure => !ownedItems.includes(structure.id));
64+
65+
setUnownedStructures(unowned);
66+
} catch (error) {
67+
console.error('Error fetching structures:', error);
68+
setError('Failed to load structures.');
69+
} finally {
70+
setLoading(false);
7871
};
79-
80-
fetchStructures();
8172
}, [session, activePlanet, supabase]);
8273

83-
const [hasResearchStation, setHasResearchStation] = useState(false);
74+
useEffect(() => {
75+
fetchStructures();
76+
}, [fetchTrigger, fetchStructures]); // Re-fetch when fetchTrigger changes
8477

85-
async function addResearchStation() {
86-
if (!session || !activePlanet || hasResearchStation) return;
78+
const addResearchStation = async () => {
79+
if (!session || !activePlanet) return;
8780

8881
try {
89-
const { error: researchError } = await supabase
82+
const { error } = await supabase
9083
.from('inventory')
9184
.insert([
9285
{
@@ -98,20 +91,17 @@ export function UnownedSurfaceStructures() {
9891
},
9992
]);
10093

101-
if (researchError) {
102-
throw researchError;
103-
};
94+
if (error) throw error;
10495

105-
console.log("Research Station has been added to the inventory.");
10696
alert("You now have a Research Station in your inventory!");
107-
setHasResearchStation(true);
97+
setFetchTrigger(fetchTrigger + 1); // Trigger a re-fetch
10898
} catch (error) {
10999
console.error('Error adding research station:', error);
110100
setError('Failed to add the Research Station.');
111101
};
112102
};
113103

114-
async function addToInventory(structure: InventoryItem) {
104+
const addToInventory = async (structure: InventoryItem) => {
115105
if (!session || !activePlanet) return;
116106

117107
try {
@@ -127,21 +117,18 @@ export function UnownedSurfaceStructures() {
127117
},
128118
]);
129119

130-
if (error) {
131-
throw error;
132-
}
120+
if (error) throw error;
133121

134-
console.log(`${structure.name} has been added to the inventory.`);
122+
alert(`${structure.name} has been added to the inventory.`);
135123
setOpen(false);
124+
setFetchTrigger(fetchTrigger + 1); // Trigger a re-fetch
136125
} catch (error) {
137126
console.error('Error adding to inventory:', error);
138127
setError('Failed to add the structure to your inventory.');
139128
};
140129
};
141130

142-
if (loading) {
143-
return <p>Loading...</p>;
144-
};
131+
if (loading) return <p>Loading...</p>;
145132

146133
if (unownedStructures.length === 0) {
147134
return (
@@ -160,7 +147,7 @@ export function UnownedSurfaceStructures() {
160147
</Dialog>
161148
</div>
162149
);
163-
};
150+
}
164151

165152
return (
166153
<div className="relative">
@@ -171,7 +158,6 @@ export function UnownedSurfaceStructures() {
171158
</Button>
172159
</DialogTrigger>
173160
<DialogContent className="max-w-full max-h-[90vh] w-full sm:w-[90vw] h-full sm:h-[90vh] p-0 bg-gradient-to-br from-[#1a1b26] via-[#292e42] to-[#565f89] rounded-3xl overflow-hidden">
174-
<div className="absolute inset-0 bg-[url('data:image/svg+xml;base64,...')] opacity-20" />
175161
<AnimatePresence>
176162
{selectedStructure ? (
177163
<motion.div
@@ -192,9 +178,7 @@ export function UnownedSurfaceStructures() {
192178
<Button
193179
size="lg"
194180
className="w-full max-w-md bg-[#7aa2f7] text-[#1a1b26] hover:bg-[#89b4fa]"
195-
onClick={() => {
196-
addToInventory(selectedStructure);
197-
}}
181+
onClick={() => addToInventory(selectedStructure)}
198182
>
199183
Place
200184
</Button>

0 commit comments

Comments
Β (0)