import React, { useEffect, useState } from 'react'; import { supabase } from '../../services/supabase'; import { Printer } from '../../types'; import { Plus, Trash2, Save, Printer as PrinterIcon, Zap } from 'lucide-react'; const Printers: React.FC = () => { const [printers, setPrinters] = useState([]); const [loading, setLoading] = useState(true); const [isAdding, setIsAdding] = useState(false); const [newPrinter, setNewPrinter] = useState>({ name: '', powerWatts: 350, depreciationPerHour: 0 }); useEffect(() => { fetchPrinters(); }, []); const fetchPrinters = async () => { try { const { data, error } = await supabase.from('printers').select('*'); if (error) throw error; const mapped = (data || []).map((item: any) => ({ id: item.id, name: item.name, powerWatts: item.power_watts, depreciationPerHour: item.depreciation_per_hour })); setPrinters(mapped); } catch (error) { console.error('Error fetching printers:', error); } finally { setLoading(false); } }; const handleSave = async () => { if (!newPrinter.name) return; try { const payload = { name: newPrinter.name, power_watts: newPrinter.powerWatts, depreciation_per_hour: newPrinter.depreciationPerHour, user_id: (await supabase.auth.getUser()).data.user?.id }; const { error } = await supabase.from('printers').insert([payload]); if (error) throw error; setIsAdding(false); setNewPrinter({ name: '', powerWatts: 350, depreciationPerHour: 0 }); fetchPrinters(); } catch (error) { console.error('Error saving printer:', error); alert('Error saving printer'); } }; const handleDelete = async (id: string) => { if (!confirm('Are you sure?')) return; try { const { error } = await supabase.from('printers').delete().eq('id', id); if (error) throw error; fetchPrinters(); } catch (error) { console.error('Error deleting printer:', error); } }; return (

Printers

Manage your 3D printers configuration.

{isAdding && (

New Printer

setNewPrinter({ ...newPrinter, name: e.target.value })} />
setNewPrinter({ ...newPrinter, powerWatts: Number(e.target.value) })} />

Found on printer label (avg 350W)

setNewPrinter({ ...newPrinter, depreciationPerHour: Number(e.target.value) })} />

Optional machine wear cost

)} {loading ? (
Loading...
) : (
{printers.map(printer => (

{printer.name}

{printer.powerWatts}W
Consumption: {(printer.powerWatts / 1000).toFixed(2)} kWh
{printer.depreciationPerHour > 0 && (
Depreciation: R$ {printer.depreciationPerHour.toFixed(2)}/h
)}
))}
)}
); }; export default Printers;