-
Notifications
You must be signed in to change notification settings - Fork 6
/
page.test.tsx
262 lines (238 loc) · 8.22 KB
/
page.test.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
import { useEffect, useState } from 'react'
import { StructureInfo } from './components/structure-info'
import { PlanetInfo } from './components/planet-info'
import { PlantGrid } from './components/plant-grid'
import { PlantDetails } from './components/plant-details'
import { PlantMapView } from './components/plant-map-view'
import { AnimalFollowUp } from './components/animal-follow-up'
import { Plant, Structure, PlantMapData, Animal, Comment } from './types/greenhouse'
import { Button } from "@/components/ui/button"
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs"
import { useSession, useSupabaseClient } from '@supabase/auth-helpers-react'
const mockStructure: Structure = {
id: '1',
type: 'biolab',
level: 2,
substructures: [
{ id: '1a', type: 'greenhouse', level: 1 },
{ id: '1b', type: 'greenhouse', level: 2 },
],
}
const mockPlanet = {
id: '1',
name: 'Kepler-186f',
starName: 'Kepler-186',
temperature: 20,
humidity: 32,
atmosphere: 72,
}
const mockPlants: PlantMapData[] = [
{
id: '1',
name: 'Space Fern',
species: 'Nephrolepis exaltata',
image: '/placeholder.svg?height=200&width=200',
location: 'Greenhouse A',
status: 'healthy',
oxygenProduction: 5,
lastWatered: '2 days ago',
nextWateringDue: 'Tomorrow',
waterLevel: 60,
position: { x: 20, y: 30, z: 0.5 },
},
{
id: '2',
name: 'Cosmic Aloe',
species: 'Aloe vera',
image: '/placeholder.svg?height=200&width=200',
location: 'Biolab',
status: 'warning',
oxygenProduction: 3,
lastWatered: '5 days ago',
nextWateringDue: 'Today',
waterLevel: 20,
position: { x: 50, y: 60, z: 0.7 },
},
{
id: '3',
name: 'Star Monstera',
species: 'Monstera deliciosa',
image: '/placeholder.svg?height=200&width=200',
location: 'Greenhouse B',
status: 'healthy',
oxygenProduction: 7,
lastWatered: '1 day ago',
nextWateringDue: 'In 3 days',
waterLevel: 80,
position: { x: 80, y: 40, z: 0.6 },
},
]
const mockComments: Record<string, Comment[]> = {
'A001': [
{ id: 'C001', text: 'Observed increased hopping height', user: 'Researcher1', timestamp: '1 day ago' },
{ id: 'C002', text: 'Fur appears fluffier in low gravity', user: 'Researcher2', timestamp: '12 hours ago' },
],
'A002': [
{ id: 'C003', text: 'Showing signs of stress, monitoring closely', user: 'Researcher3', timestamp: '2 days ago' },
],
'A003': [
{ id: 'C004', text: 'Bioluminescence intensity has increased', user: 'Researcher4', timestamp: '3 days ago' },
{ id: 'C005', text: 'Wing patterns evolving, documenting changes', user: 'Researcher5', timestamp: '1 day ago' },
],
}
export default function Greenhouse() {
const supabase = useSupabaseClient();
const session = useSession();
const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL;
// const imageUrl = `${supabaseUrl}/storage/v1/object/public/telescope/automaton-aiForMars/${anomalyid}.jpeg`;
const [animals, setAnimals] = useState<Animal[]>([]);
const [comments, setComments] = useState<Record<string, Comment[]>>({});
const [error, setError] = useState<string | null>(null);
const [selectedPlant, setSelectedPlant] = useState<PlantMapData | null>(null);
const [isMapView, setIsMapView] = useState(false);
const [plants, setPlants] = useState(mockPlants);
useEffect(() => {
const fetchAnimals = async () => {
if (!session) {
return;
};
try {
const { data: classifications, error: classificationError } = await supabase
.from('classifications')
.select('anomaly')
.eq('author', session.user.id);
if (classificationError) throw classificationError;
const classifiedAnomalies = classifications?.map((item) => item.anomaly);
const { data, error } = await supabase
.from('anomalies')
.select(`
id,
content,
anomalytype,
avatar_url,
anomalySet
`)
.in('id', classifiedAnomalies)
.eq('anomalytype', 'zoodexOthers');
if (error) throw error;
const formattedAnimals: Animal[] = data?.map((item) => ({
id: item.id.toString(),
name: item.content || 'Unknown Animal',
species: 'Unknown Species',
image: item.avatar_url || '/placeholder.svg?height=200&width=200',
projectName: 'Zoodex Project',
classification: 'Unknown',
lastObserved: 'Not Available',
status: 'Unknown', // Default value for missing 'status'
})) || [];
setAnimals(formattedAnimals);
} catch (err) {
console.error(err);
setError('Failed to fetch animal data.');
}
};
if (session) {
fetchAnimals();
}
}, [session, supabase]);
const handleWaterPlant = (plantId: string) => {
setPlants(prevPlants =>
prevPlants.map(plant =>
plant.id === plantId
? { ...plant, waterLevel: Math.min(plant.waterLevel + 20, 100) }
: plant
),
);
};
const handleAddStat = (plantId: string, statName: string, statValue: number) => {
setPlants(prevPlants =>
prevPlants.map(plant =>
plant.id === plantId
? { ...plant, [statName]: statValue }
: plant
)
)
}
const handleAddComment = (animalId: string, comment: string) => {
const newComment: Comment = {
id: `C${Date.now()}`,
text: comment,
user: 'Current User',
timestamp: 'Just now',
}
setComments(prevComments => ({
...prevComments,
[animalId]: [...(prevComments[animalId] || []), newComment],
}))
}
const handleUpdateClassification = (animalId: string, classification: string) => {
setAnimals(prevAnimals =>
prevAnimals.map(animal =>
animal.id === animalId
? { ...animal, classification }
: animal
)
)
}
return (
<div className="min-h-screen bg-gray-50/50 p-4">
<div className="max-w-2xl mx-auto space-y-4">
<StructureInfo structure={mockStructure} />
<PlanetInfo planet={mockPlanet} />
<Tabs defaultValue="animals" className="w-full">
<TabsList className="w-full">
<TabsTrigger value="plants" className="flex-1">Plants</TabsTrigger>
<TabsTrigger value="animals" className="flex-1">Animals</TabsTrigger>
</TabsList>
<TabsContent value="plants">
<div className="space-y-4">
<div className="flex justify-between items-center">
<h2 className="text-xl font-semibold text-[#2C4F64]">Your Plants</h2>
<Button
variant="outline"
onClick={() => setIsMapView(!isMapView)}
>
{isMapView ? 'Grid View' : 'Map View'}
</Button>
</div>
<div className="transition-all duration-300 ease-in-out">
{selectedPlant ? (
<PlantDetails
plant={selectedPlant}
onBack={() => setSelectedPlant(null)}
onWater={handleWaterPlant}
onAddStat={handleAddStat}
/>
) : isMapView ? (
<PlantMapView
plants={plants}
onPlantClick={(plant) => setSelectedPlant(plant)}
/>
) : (
<PlantGrid
plants={plants}
onPlantClick={(plant) => setSelectedPlant(plant)}
/>
)}
</div>
</div>
</TabsContent>
<TabsContent value="animals">
<div className="space-y-4">
<h2 className="text-xl font-semibold text-[#2C4F64]">Animal Follow-ups</h2>
{animals.map((animal) => (
<AnimalFollowUp
key={animal.id}
animal={animal}
comments={comments[animal.id] || []}
onAddComment={handleAddComment}
onUpdateClassification={handleUpdateClassification}
/>
))}
</div>
</TabsContent>
</Tabs>
</div>
</div>
);
};