import React, {useEffect, useState} from 'react'; import { Dialog, DialogTitle, DialogContent, DialogActions, Button, Box, Typography, IconButton, Tab, Tabs, Table, TableBody, TableCell, TableRow, Rating, TextField, Avatar, Chip, Alert, Snackbar } from '@mui/material'; import CloseIcon from '@mui/icons-material/Close'; import AutoFixHighIcon from '@mui/icons-material/AutoFixHigh'; import FavoriteIcon from '@mui/icons-material/Favorite'; import FavoriteBorderIcon from '@mui/icons-material/FavoriteBorder'; import PersonIcon from "@mui/icons-material/Person"; import {onGetBuildDetails, onSetReview, onToggleFavorite, onCloneBuild, onSetRating} from '../pages/+Layout.telefunc'; import {onGetBuildState} from '../pages/forge/forge.telefunc'; const formatPrice = (price: any) => new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD' }).format(Number(price) || 0); export default function BuildDetailsDialog({open, buildId, onClose, currentUser, isDashboardView = false}: { open: boolean; buildId: number | null; onClose: () => void; currentUser: any; isDashboardView?: boolean; }) { const [details, setDetails] = useState(null); const [loading, setLoading] = useState(false); const [tabIndex, setTabIndex] = useState(0); const [cloneDialogOpen, setCloneDialogOpen] = useState(false); const [cloningBuildId, setCloningBuildId] = useState(null); const [isOwner, setIsOwner] = useState(false); const [reviewText, setReviewText] = useState(""); const [ratingVal, setRatingVal] = useState(5); const [snackbar, setSnackbar] = useState<{ open: boolean; message: string; severity: 'error' | 'warning' | 'info' | 'success'; }>({ open: false, message: '', severity: 'warning' }); useEffect(() => { if (open && buildId !== null) { setLoading(true); setReviewText(""); setRatingVal(5); onGetBuildDetails({buildId}) .then(data => { setDetails(data); if (data.userRating) setRatingVal(data.userRating); if (data.userReview) setReviewText(data.userReview); }) .finally(() => setLoading(false)); } }, [open, buildId]); useEffect(() => { if (open && buildId !== null) { onGetBuildState({buildId}) .then(state => { setIsOwner(!!state); }) .catch(() => setIsOwner(false)); } else { setIsOwner(false); } }, [open, buildId]); useEffect(() => { if (open) { setTabIndex(0); } }, [open, buildId]); const handleFavorite = async () => { if (!currentUser || buildId === null) return alert("Please login to favorite builds."); const res = await onToggleFavorite({buildId}); setDetails((prev: any) => ({...prev, isFavorite: res})); }; const handleSubmitReview = async () => { if (!currentUser || buildId === null) return alert("Please login to review."); await onSetReview({ buildId, content: reviewText, }); await onSetRating({ buildId, value: ratingVal }); const refreshed = await onGetBuildDetails({buildId}); setDetails(refreshed); }; const handleCloneConfirm = async () => { if (!cloningBuildId) return; if(!currentUser){ window.location.href="/auth/login"; return; } try { const newBuildId = await onCloneBuild({buildId: cloningBuildId}); window.location.href = `/forge?buildId=${newBuildId}`; setCloneDialogOpen(false); setCloningBuildId(null); } catch (e) { // alert("Failed to clone build. Please try again."); setCloneDialogOpen(false); setSnackbar({ open: true, message: 'Failed to clone build. Please try again!', severity: 'error' }) } }; if (!open) return null; return ( <> {loading || !details ? ( Loading Forge Schematics... ) : ( <> {details.name} by {details.creator} setTabIndex(v)}> {tabIndex === 0 && ( {details.components.map((comp: any) => ( {comp.type?.substring(0, 3)?.toUpperCase()} {comp.type} {comp.brand} {comp.name} {formatPrice(comp.price)} ))} TOTAL {formatPrice(details.totalPrice)}
Builder's Notes "{details.description || "No notes provided."}" {isDashboardView && isOwner ? ( ) : ( )}
)} {tabIndex === 1 && ( {details.ratingStatistics.averageRating.toFixed(1)} {details.ratingStatistics.ratingCount} ratings {currentUser && details.userId !== currentUser.id && ( Your Review setRatingVal(v || 5)}/> setReviewText(e.target.value)} sx={{mb: 1}} /> )} {currentUser && details.userId === currentUser.id && ( You cannot rate your own builds. )} {details.reviews.map((rev: any, i: number) => ( {rev.username} {rev.createdAt} {rev.content} ))} {details.reviews.length === 0 && ( No reviews yet. Be the first! )} )}
)}
setCloneDialogOpen(false)}> Clone Build This will create a copy of "{details?.name}" in your Forge so you can edit it. All components will be copied over. setSnackbar(prev => ({...prev, open: false}))} anchorOrigin={{vertical: 'bottom', horizontal: 'center'}} > setSnackbar(prev => ({...prev, open: false}))} severity={snackbar.severity} variant="filled" sx={{width: '100%'}} > {snackbar.message} ); }