import React, { useEffect, useState } from 'react'; import { supabase } from '../../services/supabase'; import { Filament } from '../../types'; import { Plus, Trash2, Save, Package } from 'lucide-react'; const Filaments: React.FC = () => { const [filaments, setFilaments] = useState([]); const [loading, setLoading] = useState(true); const [isAdding, setIsAdding] = useState(false); // New Filament State const [newFilament, setNewFilament] = useState>({ name: '', brand: '', material: 'PLA', color: '', spoolWeightG: 1000, priceBRL: 0, tempNozzle: 200, tempBed: 60 }); useEffect(() => { fetchFilaments(); }, []); const fetchFilaments = async () => { try { const { data, error } = await supabase.from('filaments').select('*'); if (error) throw error; // Map snake_case database fields to camelCase TS interfaces const mapped = (data || []).map((item: any) => ({ id: item.id, name: item.name, brand: item.brand, material: item.material, color: item.color, spoolWeightG: item.spool_weight_g, priceBRL: item.price_brl, tempNozzle: item.temp_nozzle, tempBed: item.temp_bed })); setFilaments(mapped); } catch (error) { console.error('Error fetching filaments:', error); } finally { setLoading(false); } }; const handleSave = async () => { if (!newFilament.name || !newFilament.priceBRL) return; try { const payload = { name: newFilament.name, brand: newFilament.brand, material: newFilament.material, color: newFilament.color, spool_weight_g: newFilament.spoolWeightG, price_brl: newFilament.priceBRL, temp_nozzle: newFilament.tempNozzle, temp_bed: newFilament.tempBed, user_id: (await supabase.auth.getUser()).data.user?.id }; const { error } = await supabase.from('filaments').insert([payload]); if (error) throw error; setIsAdding(false); setNewFilament({ name: '', brand: '', material: 'PLA', color: '', spoolWeightG: 1000, priceBRL: 0, tempNozzle: 200, tempBed: 60 }); fetchFilaments(); } catch (error) { console.error('Error saving filament:', error); alert('Error saving filament'); } }; const handleDelete = async (id: string) => { if (!confirm('Are you sure?')) return; try { const { error } = await supabase.from('filaments').delete().eq('id', id); if (error) throw error; fetchFilaments(); } catch (error) { console.error('Error deleting filament:', error); } }; return (

Filaments Library

Manage your 3D printing materials.

{isAdding && (

New Filament

setNewFilament({ ...newFilament, name: e.target.value })} />
setNewFilament({ ...newFilament, brand: e.target.value })} />
setNewFilament({ ...newFilament, color: e.target.value })} />
setNewFilament({ ...newFilament, spoolWeightG: Number(e.target.value) })} />
setNewFilament({ ...newFilament, priceBRL: Number(e.target.value) })} />
)} {loading ? (
Loading...
) : (
{filaments.map(filament => (

{filament.name}

{filament.brand} • {filament.material}

Price/Spool: R$ {filament.priceBRL.toFixed(2)}
Weight: {filament.spoolWeightG}g
Cost/gram: R$ {(filament.priceBRL / filament.spoolWeightG).toFixed(4)}
))}
)}
); }; export default Filaments;