import { useState, useEffect, useRef } from 'react' import { Upload, CheckCircle, TrendingUp, Info } from 'lucide-react' import { LineChart, Line, BarChart, Bar, XAxis, YAxis, Tooltip, ResponsiveContainer, CartesianGrid, Legend, ReferenceLine, ScatterChart, Scatter } from 'recharts' import { api as nutritionApi } from '../utils/api' import dayjs from 'dayjs' import isoWeek from 'dayjs/plugin/isoWeek' dayjs.extend(isoWeek) // ── Helpers ─────────────────────────────────────────────────────────────────── const KCAL_PER_KG_FAT = 7700 function rollingAvg(arr, key, window=7) { return arr.map((d,i) => { const slice = arr.slice(Math.max(0,i-window+1),i+1).map(x=>x[key]).filter(v=>v!=null) return slice.length ? {...d, [`${key}_avg`]: Math.round(slice.reduce((a,b)=>a+b,0)/slice.length*10)/10} : d }) } // ── Import Panel ────────────────────────────────────────────────────────────── function ImportPanel({ onImported }) { const fileRef = useRef() const [status, setStatus] = useState(null) const [error, setError] = useState(null) const [dragging,setDragging]= useState(false) const [tab, setTab] = useState('file') // 'file' | 'paste' const [pasteText, setPasteText] = useState('') const runImport = async (file) => { setStatus('loading'); setError(null) try { const result = await nutritionApi.importCsv(file) if (result.days_imported === undefined) throw new Error(JSON.stringify(result)) setStatus(result) onImported() } catch(err) { setError('Import fehlgeschlagen: ' + err.message) setStatus(null) } } const handleFile = async e => { const file = e.target.files[0]; if (!file) return await runImport(file) e.target.value = '' } const handleDrop = async e => { e.preventDefault(); setDragging(false) const file = e.dataTransfer.files[0] if (!file) return await runImport(file) } const handlePasteImport = async () => { if (!pasteText.trim()) return const blob = new Blob([pasteText], { type: 'text/csv' }) const file = new File([blob], 'paste.csv', { type: 'text/csv' }) await runImport(file) } return (
📥 FDDB CSV Import

In FDDB: Mein Tagebuch → Exportieren → CSV — dann hier importieren.

{/* Tab switcher */}
{[['file','📁 Datei / Drag & Drop'],['paste','📋 Text einfügen']].map(([k,l])=>( ))}
{tab==='file' && ( <> {/* Drag & Drop Zone */}
{e.preventDefault();setDragging(true)}} onDragLeave={()=>setDragging(false)} onDrop={handleDrop} onClick={()=>fileRef.current.click()} style={{ border:`2px dashed ${dragging?'var(--accent)':'var(--border2)'}`, borderRadius:10, padding:'24px 16px', textAlign:'center', background: dragging?'var(--accent-light)':'var(--surface2)', cursor:'pointer', transition:'all 0.15s', }}>
{dragging ? 'Datei loslassen…' : 'CSV hierher ziehen oder tippen zum Auswählen'}
.csv Dateien
)} {tab==='paste' && ( <>