1
+ import React , { useEffect , useState } from "react" ;
2
+ import { useSession , useSupabaseClient } from "@supabase/auth-helpers-react" ;
3
+
4
+ function CreateSectorComponent ( { planetId } ) {
5
+ const supabase = useSupabaseClient ( ) ;
6
+ const session = useSession ( ) ;
7
+ const [ isLoading , setIsLoading ] = useState ( false ) ;
8
+
9
+ const createSector = async ( ) => {
10
+ if ( ! session ?. user ?. id ) {
11
+ return ;
12
+ }
13
+
14
+ if ( ! planetId ) {
15
+ // User is not on a planet
16
+ return ;
17
+ }
18
+
19
+ // Generate random mineral deposits
20
+ const depositCount = Math . floor ( Math . random ( ) * 5 ) ;
21
+ const minerals = [ 'Gold' , 'life' , 'water' , 'carbon' , 'iron' , 'hydrogen' , 'energy' , 'minerals' ] ;
22
+ const deposits = [ ] ;
23
+ for ( let i = 0 ; i < depositCount ; i ++ ) {
24
+ const randomMineral = minerals [ Math . floor ( Math . random ( ) * minerals . length ) ] ;
25
+ deposits . push ( randomMineral ) ;
26
+ }
27
+
28
+ // Calculate cost (1 silfur for the first sector, and 1 additional silfur for each additional sector)
29
+ const cost = 1 // + (1 * /* Replace with the number of sectors on the planet */);
30
+
31
+ // Create the new sector
32
+ setIsLoading ( true ) ;
33
+ const { data, error } = await supabase . from ( 'planetsssSECTORS' ) . upsert ( [
34
+ {
35
+ ownerId : session . user . id ,
36
+ planetId : planetId ,
37
+ cost : cost ,
38
+ metadata : '' ,
39
+ deposits : deposits ,
40
+ sectorImage : '' , // Leave this blank for now
41
+ images : { } , // Leave this blank for now
42
+ } ,
43
+ ] ) ;
44
+
45
+ setIsLoading ( false ) ;
46
+
47
+ if ( error ) {
48
+ console . error ( 'Error creating sector:' , error ) ;
49
+ // Handle the error here
50
+ } else {
51
+ console . log ( 'Sector created:' , data ) ;
52
+ // Handle success, e.g., show a success message
53
+ }
54
+ } ;
55
+
56
+ return (
57
+ < div >
58
+ < button onClick = { createSector } disabled = { isLoading } >
59
+ Create Sector
60
+ </ button >
61
+ </ div >
62
+ ) ;
63
+ }
64
+
65
+ export default CreateSectorComponent ;
0 commit comments