1
- "use client" ;
2
-
3
- import React , { useEffect , useState } from 'react' ;
1
+ import React , { useEffect , useState , useCallback } from 'react' ;
4
2
import { useSession , useSupabaseClient } from '@supabase/auth-helpers-react' ;
5
3
import { useActivePlanet } from '@/context/ActivePlanet' ;
6
4
import { Plus } from "lucide-react" ;
@@ -27,66 +25,61 @@ export function UnownedSurfaceStructures() {
27
25
const [ error , setError ] = useState < string | null > ( null ) ;
28
26
const [ open , setOpen ] = useState ( false ) ;
29
27
const [ selectedStructure , setSelectedStructure ] = useState < InventoryItem | null > ( null ) ;
28
+ const [ fetchTrigger , setFetchTrigger ] = useState ( 0 ) ; // Track changes for re-fetching
30
29
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 ) ;
78
71
} ;
79
-
80
- fetchStructures ( ) ;
81
72
} , [ session , activePlanet , supabase ] ) ;
82
73
83
- const [ hasResearchStation , setHasResearchStation ] = useState ( false ) ;
74
+ useEffect ( ( ) => {
75
+ fetchStructures ( ) ;
76
+ } , [ fetchTrigger , fetchStructures ] ) ; // Re-fetch when fetchTrigger changes
84
77
85
- async function addResearchStation ( ) {
86
- if ( ! session || ! activePlanet || hasResearchStation ) return ;
78
+ const addResearchStation = async ( ) => {
79
+ if ( ! session || ! activePlanet ) return ;
87
80
88
81
try {
89
- const { error : researchError } = await supabase
82
+ const { error } = await supabase
90
83
. from ( 'inventory' )
91
84
. insert ( [
92
85
{
@@ -98,20 +91,17 @@ export function UnownedSurfaceStructures() {
98
91
} ,
99
92
] ) ;
100
93
101
- if ( researchError ) {
102
- throw researchError ;
103
- } ;
94
+ if ( error ) throw error ;
104
95
105
- console . log ( "Research Station has been added to the inventory." ) ;
106
96
alert ( "You now have a Research Station in your inventory!" ) ;
107
- setHasResearchStation ( true ) ;
97
+ setFetchTrigger ( fetchTrigger + 1 ) ; // Trigger a re-fetch
108
98
} catch ( error ) {
109
99
console . error ( 'Error adding research station:' , error ) ;
110
100
setError ( 'Failed to add the Research Station.' ) ;
111
101
} ;
112
102
} ;
113
103
114
- async function addToInventory ( structure : InventoryItem ) {
104
+ const addToInventory = async ( structure : InventoryItem ) => {
115
105
if ( ! session || ! activePlanet ) return ;
116
106
117
107
try {
@@ -127,21 +117,18 @@ export function UnownedSurfaceStructures() {
127
117
} ,
128
118
] ) ;
129
119
130
- if ( error ) {
131
- throw error ;
132
- }
120
+ if ( error ) throw error ;
133
121
134
- console . log ( `${ structure . name } has been added to the inventory.` ) ;
122
+ alert ( `${ structure . name } has been added to the inventory.` ) ;
135
123
setOpen ( false ) ;
124
+ setFetchTrigger ( fetchTrigger + 1 ) ; // Trigger a re-fetch
136
125
} catch ( error ) {
137
126
console . error ( 'Error adding to inventory:' , error ) ;
138
127
setError ( 'Failed to add the structure to your inventory.' ) ;
139
128
} ;
140
129
} ;
141
130
142
- if ( loading ) {
143
- return < p > Loading...</ p > ;
144
- } ;
131
+ if ( loading ) return < p > Loading...</ p > ;
145
132
146
133
if ( unownedStructures . length === 0 ) {
147
134
return (
@@ -160,7 +147,7 @@ export function UnownedSurfaceStructures() {
160
147
</ Dialog >
161
148
</ div >
162
149
) ;
163
- } ;
150
+ }
164
151
165
152
return (
166
153
< div className = "relative" >
@@ -171,7 +158,6 @@ export function UnownedSurfaceStructures() {
171
158
</ Button >
172
159
</ DialogTrigger >
173
160
< 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" />
175
161
< AnimatePresence >
176
162
{ selectedStructure ? (
177
163
< motion . div
@@ -192,9 +178,7 @@ export function UnownedSurfaceStructures() {
192
178
< Button
193
179
size = "lg"
194
180
className = "w-full max-w-md bg-[#7aa2f7] text-[#1a1b26] hover:bg-[#89b4fa]"
195
- onClick = { ( ) => {
196
- addToInventory ( selectedStructure ) ;
197
- } }
181
+ onClick = { ( ) => addToInventory ( selectedStructure ) }
198
182
>
199
183
Place
200
184
</ Button >
0 commit comments