1
+ import React , { useEffect , useState } from "react" ;
2
+ import { useSupabaseClient } from "@supabase/auth-helpers-react" ;
3
+
4
+ interface Structure {
5
+ id : number ;
6
+ name : string ;
7
+ description : string ;
8
+ icon_url : string ;
9
+ } ;
10
+
11
+ interface StructureSelectionProps {
12
+ onStructureSelected : ( structure : Structure ) => void ;
13
+ } ;
14
+
15
+ const StructureSelection : React . FC < StructureSelectionProps > = ( { onStructureSelected } ) => {
16
+ const supabase = useSupabaseClient ( ) ;
17
+
18
+ const [ structures , setStructures ] = useState < Structure [ ] > ( [ ] ) ;
19
+
20
+ const [ isCalloutOpen , setIsCalloutOpen ] = useState ( false ) ;
21
+
22
+ const fetchStructures = async ( ) => {
23
+ try {
24
+ const { data, error } = await supabase
25
+ . from ( 'inventoryITEMS' )
26
+ . select ( 'id, name, description, icon_url' )
27
+ . eq ( 'ItemCategory' , 'Structure' ) ;
28
+
29
+ if ( data ) {
30
+ setStructures ( data ) ;
31
+ } ;
32
+
33
+ if ( error ) {
34
+ console . error ( error . message ) ;
35
+ } ;
36
+ } catch ( error ) {
37
+ console . error ( error . message ) ;
38
+ } ;
39
+ } ;
40
+
41
+ useEffect ( ( ) => {
42
+ fetchStructures ( ) ;
43
+ } , [ supabase ] ) ;
44
+
45
+ const handleStructureClick = ( structure : Structure ) => {
46
+ onStructureSelected ( structure ) ;
47
+ setIsCalloutOpen ( false ) ;
48
+ } ;
49
+
50
+ return (
51
+ < div className = "relative inline-block text-left" >
52
+ < button
53
+ type = "button"
54
+ className = "px-4 py-2 text-white bg-blue-500 rounded-md focus:outline-none hover:bg-blue-600"
55
+ onClick = { ( ) => setIsCalloutOpen ( ! isCalloutOpen ) }
56
+ >
57
+ Build
58
+ </ button >
59
+
60
+ { isCalloutOpen && (
61
+ < div className = "origin-top-right absolute right-0 mt-2 w-56 rounded-md shadow-lg bg-white ring-1 ring-black ring-opacity-5" >
62
+ < div className = "py-1" >
63
+ { structures . map ( ( structure ) => (
64
+ < div
65
+ key = { structure . id }
66
+ className = "flex items-center justify-between px-4 py-2 hover:bg-gray-100 cursor-pointer"
67
+ onClick = { ( ) => handleStructureClick ( structure ) }
68
+ >
69
+ < div className = "flex items-center space-x-2" >
70
+ < img src = { structure . icon_url } alt = { structure . name } className = "w-6 h-6" />
71
+ < span > { structure . name } </ span >
72
+ </ div >
73
+ < span className = "text-gray-500" > { structure . description } </ span >
74
+ </ div >
75
+ ) ) }
76
+ </ div >
77
+ </ div >
78
+ ) }
79
+ </ div >
80
+ ) ;
81
+ } ;
82
+
83
+ export default function StructureComponent ( ) {
84
+ const handleStructureSelected = ( structure ) => {
85
+ console . log ( 'Selected structure: ' , structure ) ;
86
+ } ;
87
+
88
+ return (
89
+ < div >
90
+ < StructureSelection onStructureSelected = { handleStructureSelected } />
91
+ </ div >
92
+ ) ;
93
+ } ;
0 commit comments