feat: Apple Health CSV import for sleep data (v9d Phase 2c)
Backend: - New endpoint POST /api/sleep/import/apple-health - Parses Apple Health sleep CSV format - Maps German phase names (Kern→light, REM→rem, Tief→deep, Wach→awake) - Aggregates segments by night (wake date) - Stores raw segments in JSONB (sleep_segments) - Does NOT overwrite manual entries (source='manual') Frontend: - Import button in SleepPage with file picker - Progress indicator during import - Success/error messages - Auto-refresh after import Documentation: - Added architecture rules reference to CLAUDE.md Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
1 parent
b65efd3b71
commit
b1a92c01fc
4 files changed
+272
-27
No files matched your search
@@ -1,5 +1,5 @@
|
||||
import { useState, useEffect } from 'react'
|
||||
import { Moon, Plus, Edit2, Trash2, X, Save, TrendingUp } from 'lucide-react'
|
||||
import { useState, useEffect, useRef } from 'react'
|
||||
import { Moon, Plus, Edit2, Trash2, X, Save, TrendingUp, Upload } from 'lucide-react'
|
||||
import { api } from '../utils/api'
|
||||
|
||||
/**
|
||||
@@ -20,6 +20,8 @@ export default function SleepPage() {
|
||||
const [editingId, setEditingId] = useState(null)
|
||||
const [loading, setLoading] = useState(true)
|
||||
const [saving, setSaving] = useState(false)
|
||||
const [importing, setImporting] = useState(false)
|
||||
const fileInputRef = useRef(null)
|
||||
|
||||
// Form state
|
||||
const [formData, setFormData] = useState({
|
||||
@@ -133,6 +135,31 @@ export default function SleepPage() {
|
||||
}
|
||||
}
|
||||
|
||||
const handleImport = async (e) => {
|
||||
const file = e.target.files[0]
|
||||
if (!file) return
|
||||
|
||||
if (!file.name.endsWith('.csv')) {
|
||||
alert('Bitte eine CSV-Datei auswählen')
|
||||
return
|
||||
}
|
||||
|
||||
setImporting(true)
|
||||
|
||||
try {
|
||||
const result = await api.importAppleHealthSleep(file)
|
||||
await load()
|
||||
alert(result.message || `✅ ${result.imported} Nächte importiert, ${result.skipped} übersprungen`)
|
||||
} catch (err) {
|
||||
alert('Import fehlgeschlagen: ' + err.message)
|
||||
} finally {
|
||||
setImporting(false)
|
||||
if (fileInputRef.current) {
|
||||
fileInputRef.current.value = '' // Reset input
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const formatDuration = (minutes) => {
|
||||
const h = Math.floor(minutes / 60)
|
||||
const m = minutes % 60
|
||||
@@ -184,15 +211,41 @@ export default function SleepPage() {
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Add Button */}
|
||||
{/* Action Buttons */}
|
||||
{!showForm && (
|
||||
<button
|
||||
onClick={startCreate}
|
||||
className="btn btn-primary btn-full"
|
||||
style={{ marginBottom: 16 }}
|
||||
>
|
||||
<Plus size={16} /> Schlaf erfassen
|
||||
</button>
|
||||
<div style={{ display: 'flex', gap: 8, marginBottom: 16 }}>
|
||||
<button
|
||||
onClick={startCreate}
|
||||
className="btn btn-primary"
|
||||
style={{ flex: 1 }}
|
||||
>
|
||||
<Plus size={16} /> Schlaf erfassen
|
||||
</button>
|
||||
<button
|
||||
onClick={() => fileInputRef.current?.click()}
|
||||
disabled={importing}
|
||||
className="btn btn-secondary"
|
||||
style={{ flex: 1 }}
|
||||
>
|
||||
{importing ? (
|
||||
<div style={{ display: 'flex', alignItems: 'center', gap: 6, justifyContent: 'center' }}>
|
||||
<div className="spinner" style={{ width: 14, height: 14 }} />
|
||||
Importiere...
|
||||
</div>
|
||||
) : (
|
||||
<>
|
||||
<Upload size={16} /> CSV Import
|
||||
</>
|
||||
)}
|
||||
</button>
|
||||
<input
|
||||
ref={fileInputRef}
|
||||
type="file"
|
||||
accept=".csv"
|
||||
onChange={handleImport}
|
||||
style={{ display: 'none' }}
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Entry Form */}
|
||||
|
||||
Reference in new issue
Block a user