import React, {useEffect, useState} from 'react'; import { Dialog, DialogTitle, DialogContent, DialogActions, Button, Typography, Box, Grid, Chip, CircularProgress, IconButton, Table, TableBody, TableCell, TableContainer, TableRow, Paper } from '@mui/material'; import CloseIcon from '@mui/icons-material/Close'; import {onGetComponentDetails} from '../pages/+Layout.telefunc'; export default function ComponentDetailsDialog({open, component, onClose}: any) { const [fullData, setFullData] = useState(null); const [loading, setLoading] = useState(false); useEffect(() => { if (open && component) { setLoading(true); onGetComponentDetails({componentId: component.id}) .then(data => setFullData(data)) .catch(console.error) .finally(() => setLoading(false)); } else { setFullData(null); } }, [open, component]); if (!open || !component) return null; const displayData = fullData || component; const specs = fullData?.details || {}; const formatMoney = (amount: number) => `$${Number(amount).toFixed(2)}`; const renderValue = (key: string, val: any) => { if (Array.isArray(val)) { if (val.length === 0) return 'None'; if (typeof val[0] === 'string') { return val.join(', '); } if (typeof val[0] === 'object') { const parts = val .map((v: any) => v.socket || v.formFactor || v.name || v.type || Object.values(v)[0] ) .filter(Boolean); return parts.length > 0 ? parts.join(', ') : 'None'; } return val.join(', '); } const strVal = String(val); const lowerKey = key.toLowerCase(); if (lowerKey.includes('capacity') || lowerKey.includes('vram') || lowerKey === 'memory') { return `${strVal} GB`; } if (lowerKey.includes('tdp') || lowerKey.includes('wattage')) { return `${strVal} W`; } if (lowerKey.includes('length') || lowerKey.includes('height') || lowerKey.includes('width')) { return `${strVal} mm`; } if (lowerKey.includes('clock')) { return `${strVal} GHz`; } if (lowerKey.includes('speed')) { return `${strVal} MHz`; } return strVal; }; const formatKey = (key: string) => { return key .replace(/([A-Z])/g, ' $1') .replace(/_/g, ' ') .replace(/\b\w/g, c => c.toUpperCase()); }; return ( {displayData.brand} {displayData.name} {loading ? ( ) : ( Technical Specifications Brand {displayData.brand} {Object.entries(specs).map(([key, val]) => { if (key === 'componentId' || key === 'id') return null; return ( {formatKey(key)} {renderValue(key, val)} ); })}
{formatMoney(displayData.price)}
)}
); }