import React, { useEffect, useState } from 'react'; import { Dialog, DialogTitle, DialogContent, DialogActions, Button, Grid, Box, Typography, IconButton, Tab, Tabs, Table, TableBody, TableCell, TableRow, Rating, TextField, Avatar, Chip, Alert } 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'; 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 }: any) { 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 [reviewText, setReviewText] = useState(""); const [ratingVal, setRatingVal] = useState(5); useEffect(() => { if (open && buildId) { 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]); const handleFavorite = async () => { if (!currentUser) return alert("Please login to favorite builds."); const res = await onToggleFavorite({ buildId }); setDetails((prev: any) => ({ ...prev, isFavorite: res })); }; const handleSubmitReview = async () => { if (!currentUser) return alert("Please login to review."); await onSetReview({ buildId, content: reviewText, // rating: ratingVal }); await onSetRating({ buildId, value: ratingVal }) const refreshed = await onGetBuildDetails({ buildId }); setDetails(refreshed); }; const handleCloneConfirm = async () => { if (!cloningBuildId) 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."); } }; 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."}"
)} {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. ); }