-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathref.jsx
648 lines (589 loc) · 27.9 KB
/
ref.jsx
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
import React, { useRef, useState, useEffect } from 'react';
import { Box, Button, List, ListItem, ListItemIcon, ListItemText, Typography, AppBar, Toolbar, CssBaseline, Link, Divider, useTheme, IconButton, Avatar, Menu, MenuItem, TextField, InputAdornment } from '@mui/material';
import HomeIcon from '@mui/icons-material/Home';
import MenuIcon from '@mui/icons-material/Menu';
import SearchIcon from '@mui/icons-material/Search';
import LibraryMusicIcon from '@mui/icons-material/LibraryMusic';
import AccountCircleIcon from '@mui/icons-material/AccountCircle';
import EmojiEmotionsIcon from '@mui/icons-material/EmojiEmotions';
import { Modal, Fade, Paper } from '@mui/material';
import CameraAltIcon from '@mui/icons-material/CameraAlt';
import InsertDriveFileIcon from '@mui/icons-material/InsertDriveFile';
import { useNavigate } from 'react-router-dom';
import Bubble from '../LandingPage/homebubble';
import RecommendedSongs from './recommendedSongs';
import { logout } from '../../userAuth/firebase';
import { ToastContainer, toast } from 'react-toastify';
import 'react-toastify/dist/ReactToastify.css';
import FeaturedPlayListIcon from '@mui/icons-material/FeaturedPlayList';
import Tooltip from '@mui/material/Tooltip';
import { initializeApp } from 'firebase/app';
import { getFirestore, collection, getDocs, addDoc ,} from 'firebase/firestore';
import { getStorage } from 'firebase/storage';
import { useAuth } from '../../userAuth/AuthProvider';
import GalleryModal from './galleryModal';
import { getDownloadURL, ref, uploadBytes } from 'firebase/storage';
//import { AuthProvider } from '../../userAuth/AuthProvider';
// State to store selected image data
// Your Firebase configuration
const firebaseConfig = {
apiKey: "AIzaSyB51kbIHKtjhKdC9WLoHn1n5rva-6kdPiU",
authDomain: "moodsphere-dev-dynasty.firebaseapp.com",
projectId: "moodsphere-dev-dynasty",
storageBucket: "moodsphere-dev-dynasty.appspot.com",
messagingSenderId: "31582792465",
appId: "1:31582792465:web:ed7e040408bb81f4d4fab0",
measurementId: "G-WBHFGXF8FQ"
};
const firebaseApp = initializeApp(firebaseConfig);
const db = getFirestore(firebaseApp);
const storage = getStorage(firebaseApp);
const MoodStatus = () => {
const navigate = useNavigate();
const [selectedImage, setSelectedImage] = useState([]);
const [anchorEl, setAnchorEl] = React.useState(null);
const [openModal, setOpenModal] = React.useState(false);
const [uploadedImage, setUploadedImage] = useState(null);
const [mood, setMood] = useState(null);
const [predictedSongs, setPredictedSongs] = useState([]);
const [image, setImage] = useState(null);
const [images, setImages] = useState([]);
const fileInputRef = useRef(null);
const [showInfoText, setShowInfoText] = useState(true);
const { user } = useAuth(); // Access the user object
const isGuest = user?.isGuest; // Determine if the logged in user is a guest
const userId = user?.uid;
const [openGallery, setOpenGallery] = useState(false);
const handleOpenGallery = () => {
setOpenGallery(true);
};
const handleCloseGallery = () => {
setOpenGallery(false);
};
// const handleUploadFromSavedImages = () => {
// // Define the logic for handling upload from saved images
// console.log('Handle upload from saved images');
// // You can open a modal or navigate to another page where users can select images
// };
// const checkImagesCollection = async () => {
// const userId = user.uid;
// const imagesRef = collection(db, 'userPic', userId, 'images'); // Assuming 'db' is your Firestore instance
// try {
// const snapshot = await getDocs(imagesRef);
// if (snapshot.empty) {
// // Collection does not exist, create it
// await addDoc(collection(db, 'userPic', userId, 'images'), {});
// // Show popup indicating collection was created
// toast.info("Images collection created successfully!", {
// position: "top-center",
// autoClose: 2000,
// hideProgressBar: false,
// closeOnClick: true,
// pauseOnHover: true,
// draggable: true,
// progress: undefined,
// });
// }
// } catch (error) {
// console.error('Error checking images collection:', error);
// // Show popup indicating error
// toast.error("Error checking images collection. Please try again later.", {
// position: "top-center",
// autoClose: 2000,
// hideProgressBar: false,
// closeOnClick: true,
// pauseOnHover: true,
// draggable: true,
// progress: undefined,
// });
// }
// };
const handleFileUpload = async (event) => {
const file = event.target.files[0];
if (file) {
try {
// Prepare a reference to the Firebase Storage bucket
const storageRef = ref(storage, `userPic/${user.uid}/images/${file.name}`);
// Upload the file to Firebase Storage
await uploadBytes(storageRef, file);
// Get the download URL of the uploaded image
const imageUrl = await getDownloadURL(storageRef);
// Store image metadata (including URL) in Firestore
const imagesRef = collection(db, `userPic/${user.uid}/images`);
const docRef = await addDoc(imagesRef, {
fileName: file.name,
imageUrl: imageUrl
});
// Set uploaded image preview (optional)
setUploadedImage(URL.createObjectURL(file));
// Display success message
toast.success('Image uploaded successfully to Firestore!');
} catch (error) {
console.error('Error uploading image to Firestore:', error);
toast.error('Failed to upload image to Firestore. Please try again.');
}
} else {
// Handle case when no file is selected
setUploadedImage(null);
// You can also handle other state updates or display messages here
}
};
const handleImageSelect = (image) => {
setSelectedImage(image); // Update selected image state
handleCloseGallery(); // Close the gallery modal after selecting an image
};
const handleUseSelectedImage = async () => {
if (selectedImage) {
console.log('selectedImage' + selectedImage +'' + typeof selectedImage + '' + JSON.stringify(selectedImage))
const { fileName } = selectedImage;
console.log(JSON.stringify(selectedImage))
console.log(fileName)
// Prepare FormData for image upload using selectedImage data
const formData = new FormData();
formData.append('image', selectedImage);
setSelectedImage(URL.createObjectURL(selectedImage));
// Set uploaded image preview (if needed)
// Assuming URL.createObjectURL(selectedImage) could be used for preview
// Set FormData for fetch call
setImage(formData);
}
}
// const fetchImages = async () => {
// const userId = user.uid;
// const imagesRef = collection(db, `userPic/${userId}/images`);
// try {
// const snapshot = await getDocs(imagesRef);
// const imageList = snapshot.docs.map(doc => ({ id: doc.id, ...doc.data() }));
// setImages(imageList);
// } catch (error) {
// console.error('Error fetching images from Firestore:', error);
// toast.error('Failed to fetch images. Please try again.');
// }
// useEffect(() => {
// if (userId) {
// fetchImages();
// }
// }, [userId]); // Only re-run effect if userId changes
// const handleImageClick = (imageId) => {
// const clickedImage = images.find(image => image.id === imageId);
// if (clickedImage) {
// const file = new File([null], clickedImage.fileName, { type: 'image/*' });
// const event = { target: { files: [file] } };
// handleFileUpload(event);
// }
// };
const handleImageRemove = () => {
setUploadedImage(null);
setImage(null);
setPredictedSongs(null);
setMood(null);
setShowInfoText(true);
};
const handleMenu = (event) => {
setAnchorEl(event.currentTarget);
};
const handleClose = () => {
setAnchorEl(null);
};
const handleOpenModal = () => {
// Check if the images collection exists and create it if necessary
const userId = user.uid;
const imagesRef = collection(db, 'userPic', userId, 'images');
getDocs(imagesRef)
.then((snapshot) => {
if (snapshot.empty) {
// Collection does not exist, create it
return addDoc(imagesRef, {})
.then(() => {
// Show popup indicating collection was created
toast.info("Images collection created successfully!", {
position: "top-center",
autoClose: 2000,
hideProgressBar: false,
closeOnClick: true,
pauseOnHover: true,
draggable: true,
progress: undefined,
});
});
}
})
.catch((error) => {
console.error('Error checking images collection:', error);
// Show popup indicating error
toast.error("Error checking images collection. Please try again later.", {
position: "top-center",
autoClose: 2000,
hideProgressBar: false,
closeOnClick: true,
pauseOnHover: true,
draggable: true,
progress: undefined,
});
});
// Open the modal for uploading images
setOpenModal(true);
};
const handleCloseModal = () => {
setOpenModal(false);
};
const userLogOut = () => {
logout();
navigate('/');
}
const predictSongs = async () => {
console.log(uploadedImage)
if (!image) {
console.error('Please upload an image first.');
toast.error("Please Upload Your Image", {
position: "top-center",
autoClose: 2000,
hideProgressBar: false,
closeOnClick: true,
pauseOnHover: true,
draggable: true,
progress: undefined,
});
return;
}
try {
console.log("image->>", image)
const response = await fetch('http://127.0.0.1:5000/predict', {
method: 'POST',
body: image,
});
if (!response.ok) {
throw new Error('Failed to predict emotion');
}
const data = await response.json();
console.log('Prediction:', data.prediction);
console.log('Recommended Songs:', data.recommended_songs);
setMood(data.prediction);
setPredictedSongs(data.recommended_songs);
} catch (err) {
console.error('Error predicting songs:', err.message);
}
};
// AppBar styles
const appBarStyle = {
backgroundColor: '#121212', // Dark background
color: '#b71c1c', // Red accent text
boxShadow: '0 2px 4px -1px rgba(183, 28, 28, 0.2), 0 4px 5px 0 rgba(183, 28, 28, 0.14), 0 1px 10px 0 rgba(183, 28, 28, 0.12)', // Red-toned shadow for depth
};
const menuItems = [
{ text: 'Playlist', icon: <FeaturedPlayListIcon />, onClick: () => navigate('/playlists'), disabled: isGuest },
{ text: 'Home', icon: <HomeIcon />, onClick: () => navigate('/home'), disabled: isGuest },
{ text: 'Library', icon: <LibraryMusicIcon />, onClick: () => navigate('/library'), disabled: isGuest },
{ text: 'Profile', icon: <AccountCircleIcon />, onClick: () => navigate('/profile'), disabled: isGuest },
];
const displayImage = uploadedImage ? uploadedImage : selectedImage;
return (
<> <div style={{ overflowX: 'hidden', overflowY: 'hidden' }}>
<AppBar position="static" sx={appBarStyle}>
<Toolbar>
<IconButton
size="large"
edge="start"
color="inherit"
aria-label="menu"
sx={{ mr: 2 }}
onClick={handleMenu}
>
<MenuIcon />
</IconButton>
<Menu
id="menu-appbar"
anchorEl={anchorEl}
anchorOrigin={{
vertical: 'top',
horizontal: 'right',
}}
keepMounted
transformOrigin={{
vertical: 'top',
horizontal: 'right',
}}
open={Boolean(anchorEl)}
onClose={handleClose}
>
<MenuItem onClick={userLogOut}>Logout</MenuItem>
</Menu>
<Typography variant="h6" sx={{ flexGrow: 1, color: '#b71c1c' }}>
MoodSphere
</Typography>
<IconButton
size="large"
edge="end"
aria-label="account of current user"
aria-controls="menu-appbar"
aria-haspopup="true"
onClick={handleMenu}
color="inherit"
>
<Avatar sx={{ bgcolor: '#b71c1c' }} />
</IconButton>
</Toolbar>
</AppBar>
<div style={{ display: 'flex', height: '91.1vh', backgroundColor: '#121212' }}>
<CssBaseline />
<Box
sx={{
width: 240,
flexShrink: 0,
bgcolor: 'black',
display: 'flex',
flexDirection: 'column',
color: 'white',
overflowX: 'hidden',
borderRight: '1px solid #b71c1c',
}}>
<Modal
aria-labelledby="transition-modal-title"
aria-describedby="transition-modal-description"
open={openModal}
onClose={handleCloseModal}
closeAfterTransition>
<Fade in={openModal}>
<Paper elevation={3} sx={{
position: 'absolute',
top: '50%',
left: '50%',
transform: 'translate(-50%, -50%)',
width: 300,
bgcolor: '#121212', // Black background
color: '#b71c1c', // Red text
boxShadow: 24,
p: 4,
display: 'flex',
flexDirection: 'column',
alignItems: 'center',
gap: 2
}}>
<Typography variant="h6" component="h2" sx={{ color: 'white' }}>
Upload Your Image
</Typography>
<Button
startIcon={<CameraAltIcon sx={{ color: '#b71c1c' }} />}
sx={{ color: 'white' }}
>
Camera
</Button>
<><input
type="file"
ref={fileInputRef}
style={{ display: 'none' }}
onChange={handleFileUpload}
/>
</>
<Button
startIcon={<InsertDriveFileIcon sx={{ color: '#b71c1c' }} />}
onClick={() => fileInputRef.current.click()}
sx={{ color: 'white' }}
>
Browse Files
</Button>
<Button
startIcon={<InsertDriveFileIcon sx={{ color: '#b71c1c' }} />}
onClick={handleOpenGallery} // Call the function to check and create the collection
sx={{ color: 'white' }}
>
Upload from Saved Images
</Button>
{/* GalleryModal component */}
<GalleryModal
open={openGallery}
onClose={handleCloseGallery}
userId={user?.uid}
images={images} // Pass your gallery images array to the modal
onSelectImage={handleUseSelectedImage} // Function to handle image selection
db={db}
/>
</Paper>
</Fade>
</Modal>
<List>
{menuItems.map((item, index) => (
<Tooltip
key={index}
title={isGuest ? "This feature is not available for guest users." : ""}
placement="right"
>
<div> {/* Wrap the ListItem in a div because Tooltip children must be able to hold a ref */}
<ListItem
button
onClick={() => {
if (!isGuest) {
item.onClick();
}
}}
sx={{
'&:hover': {
bgcolor: !isGuest ? '#757575' : 'transparent',
},
opacity: !isGuest ? 1 : 0.5,
pointerEvents: isGuest ? 'none' : 'auto',
}}
>
<ListItemIcon sx={{ color: 'white' }}>{item.icon}</ListItemIcon>
<ListItemText primary={item.text} />
</ListItem>
</div>
</Tooltip>
))}
</List>
<Box mt="auto" py={2}>
<Divider sx={{ bgcolor: 'gray' }} />
<List dense>
<ListItem sx={{ py: 1, px: 2 }}>
<Link href="#" color="inherit" underline="hover">
Legal
</Link>
</ListItem>
</List>
<Box px={2} py={1}>
<Link href="#" color="inherit" underline="hover">
Privacy Policy
</Link>
</Box>
</Box>
</Box>
<ToastContainer />
<Box sx={{ position: 'relative', width: '100%' }}>
<Box
sx={{
display: 'flex',
flexDirection: 'row',
justifyContent: 'center',
position: 'relative', // Adjusted to be part of the dynamic content
top: 0,
left: 0,
zIndex: 2,
backgroundColor: 'black', // Dark background
}}
>
{/* Check for showInfoText to conditionally render the info text */}
{showInfoText && (
<Typography
sx={{
position: 'absolute',
top: 70, // Adjust as needed for positioning
left: '50%',
transform: 'translateX(-50%)',
width: '80%',
textAlign: 'center',
zIndex: 2,
color: 'white',
backgroundColor: 'rgba(0, 0, 0, 0.5)', // Semi-transparent black background
padding: '100px',
borderRadius: '5px',
fontSize: '29px',
}}
>
Please click on "Upload Image" and predict songs based on your mood.
</Typography>
)}
<Button
startIcon={<CameraAltIcon />}
onClick={handleOpenModal}
sx={{ backgroundColor: '#b71c1c', color: 'white', marginRight: '10px', marginTop: '20px', zIndex: 3 }}
>
Upload Your Image
</Button>
<Button
startIcon={<EmojiEmotionsIcon />}
onClick={predictSongs}
sx={{ backgroundColor: '#b71c1c', color: 'white', marginTop: '20px', zIndex: 3 }}
>
Predict
</Button>
</Box>
<Bubble style={{ position: 'absolute', zIndex: 0 }} />
<Box
component="main"
sx={{
display: 'flex',
flexDirection: 'column',
alignItems: 'center',
justifyContent: 'flex-start', // Changed to flex-start to align items from the top
height: '100%',
color: 'white',
// paddingTop: '100px', // Increased padding to account for buttons
}}
>
<input
type="file"
ref={fileInputRef}
style={{ display: 'none' }}
onChange={handleFileUpload}
/>
{displayImage && (
<div style={{ width: '100%', textAlign: 'center', zIndex: '2', paddingTop: '20px' }}>
<img
style={{
width: '250px', // Adjust based on your needs, ensuring it's square for a perfect circle
height: '250px', // Match width for square aspect ratio
objectFit: 'cover', // This will ensure the image covers the area, useful for non-square images
borderRadius: '50%', // This makes the image round
border: '1px solid white', // Optional: adds a white border around the circle
padding: '4px', // Optional: adds space between the image content and the border
}}
src={displayImage}
alt="Uploaded"
/>
<Button
onClick={handleImageRemove}
sx={{ backgroundColor: '#b71c1c', color: 'white', marginTop: '20px', zIndex: 3 }}
>
{/* <RemoveCircleIcon /> */}
Remove
</Button>
</div>
)}
{displayImage && mood && (
<Box sx={{ width: '100%', textAlign: 'center', marginTop: '20px', zIndex: '2' }}> {/* Encapsulated in a Box for better control */}
<Typography variant="h5">
Mood: {mood}
</Typography>
</Box>
)}
{/* Recommended songs display */}
{displayImage && predictedSongs && predictedSongs.length > 0 && (
<Box sx={{
display: 'flex',
justifyContent: 'center',
overflow: 'hidden',
width: '1200px', // Adjust the width as necessary
maxHeight: '250px', // Adjust the height as necessary
border: '1px solid red',
overflowY: 'hidden',
'&:hover': {
overflowY: 'auto',
},
zIndex: '2',
paddingTop: '20px',
marginTop: '20px',
}}>
<Box sx={{
width: '100%',
overflowX: 'auto',
'&::-webkit-scrollbar': {
height: '2px',
backgroundColor: '#b71c1c',
},
'&::-webkit-scrollbar-thumb': {
background: 'red',
borderRadius: '3px',
}
}}>
<RecommendedSongs recommendedSongs={predictedSongs} />
</Box>
</Box>
)}
</Box>
</Box>
</div>
</div>
</>
);
};
export default MoodStatus;