import { useState, useEffect, useCallback, useContext } from "react"; import { AuthContext } from "./AuthContext"; import Login from "./Login"; import Register from "./Register"; // ─── Storage helpers with Browser/localStorage Fallback ─── const STORAGE_KEY = "coffee-logbook-data"; const defaultData = { beans: [], brewLogs: [], lastSyncedAt: null }; const storage = { get: async (key) => { try { if (window.storage && typeof window.storage.get === "function") { return await window.storage.get(key); } const val = localStorage.getItem(key); return val ? { value: val } : null; } catch { return null; } }, set: async (key, val) => { try { if (window.storage && typeof window.storage.set === "function") { await window.storage.set(key, val); return; } localStorage.setItem(key, val); } catch (e) { console.error("Storage set failed:", e); } } }; async function loadData() { try { const result = await storage.get(STORAGE_KEY); if (result) { const parsed = JSON.parse(result.value); return { beans: parsed.beans || [], brewLogs: parsed.brewLogs || [], lastSyncedAt: parsed.lastSyncedAt || null }; } return defaultData; } catch { return defaultData; } } async function saveData(data) { try { await storage.set(STORAGE_KEY, JSON.stringify(data)); } catch (e) { console.error("Save failed:", e); } } const uid = () => Date.now().toString(36) + Math.random().toString(36).slice(2, 7); const ROAST_TYPES = ["Light", "Light-Medium", "Medium", "Medium-Dark", "Dark"]; const METHODS = ["pourover", "espresso", "coldbrew"]; const METHOD_LABELS = { pourover: "Pour Over", espresso: "Espresso", coldbrew: "Cold Brew" }; const METHOD_ICONS = { pourover: "☕", espresso: "⚡", coldbrew: "❄️" }; const METHOD_COLORS = { pourover: "#8B6914", espresso: "#5C3317", coldbrew: "#2F4F6F" }; // ─── Styles ─── const styles = ` @import url('https://fonts.googleapis.com/css2?family=Playfair+Display:ital,wght@0,400;0,600;0,700;1,400&family=Source+Sans+3:wght@300;400;500;600&display=swap'); * { box-sizing: border-box; margin: 0; padding: 0; } :root { --bg: #FAF6F1; --bg2: #F3EDE4; --card: #FFFFFF; --text: #2C1810; --text2: #6B5744; --text3: #9C8B7A; --accent: #8B6914; --accent2: #C4941A; --border: #E8DFD3; --espresso: #5C3317; --coldbrew: #2F4F6F; --pourover: #8B6914; --danger: #B44040; --shadow: 0 1px 3px rgba(44,24,16,0.06), 0 4px 12px rgba(44,24,16,0.04); --shadow-lg: 0 4px 16px rgba(44,24,16,0.08), 0 12px 32px rgba(44,24,16,0.06); --radius: 12px; --radius-sm: 8px; } .app { font-family: 'Source Sans 3', sans-serif; background: var(--bg); color: var(--text); min-height: 100vh; max-width: 480px; margin: 0 auto; position: relative; overflow-x: hidden; } h1, h2, h3, .display { font-family: 'Playfair Display', serif; } /* ── Header ── */ .header { padding: 24px 20px 16px; display: flex; align-items: center; justify-content: space-between; position: sticky; top: 0; background: var(--bg); z-index: 50; border-bottom: 1px solid var(--border); } .header h1 { font-size: 22px; font-weight: 600; letter-spacing: -0.3px; } .header-sub { font-size: 12px; color: var(--text3); font-weight: 400; letter-spacing: 1.5px; text-transform: uppercase; } /* ── Navigation ── */ .nav { display: flex; gap: 4px; padding: 8px 20px; background: var(--bg); position: sticky; top: 73px; z-index: 49; } .nav-btn { flex: 1; padding: 10px 8px; border: none; background: transparent; color: var(--text3); font-family: 'Source Sans 3', sans-serif; font-size: 13px; font-weight: 500; cursor: pointer; border-radius: var(--radius-sm); transition: all 0.2s; letter-spacing: 0.3px; } .nav-btn.active { background: var(--text); color: var(--bg); font-weight: 600; } .nav-btn:hover:not(.active) { background: var(--bg2); color: var(--text); } /* ── Content ── */ .content { padding: 16px 20px 100px; } /* ── Cards ── */ .card { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); padding: 18px; margin-bottom: 12px; border: 1px solid var(--border); transition: box-shadow 0.2s; cursor: pointer; } .card:hover { box-shadow: var(--shadow-lg); } .bean-card-header { display: flex; justify-content: space-between; align-items: flex-start; margin-bottom: 8px; } .bean-name { font-family: 'Playfair Display', serif; font-size: 17px; font-weight: 600; } .bean-roastery { font-size: 13px; color: var(--text2); margin-top: 2px; } .bean-meta { display: flex; gap: 8px; margin-top: 10px; flex-wrap: wrap; } .tag { font-size: 11px; padding: 4px 10px; border-radius: 20px; background: var(--bg2); color: var(--text2); font-weight: 500; letter-spacing: 0.3px; } .tag.roast-light { background: #FFF3D6; color: #8B6914; } .tag.roast-medium { background: #F0E0C8; color: #6B4E2A; } .tag.roast-dark { background: #E0D0BD; color: #4A3520; } .brew-card { position: relative; } .brew-method-bar { position: absolute; left: 0; top: 12px; bottom: 12px; width: 4px; border-radius: 0 4px 4px 0; } .brew-card .card { padding-left: 24px; } .brew-date { font-size: 11px; color: var(--text3); letter-spacing: 0.5px; } .brew-bean { font-size: 14px; font-weight: 600; color: var(--text); margin-top: 2px; } .brew-method-label { font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: 1px; margin-top: 6px; } .brew-details { display: flex; flex-wrap: wrap; gap: 12px; margin-top: 10px; } .brew-detail { font-size: 12px; } .brew-detail-label { color: var(--text3); font-size: 10px; text-transform: uppercase; letter-spacing: 0.5px; } .brew-detail-value { font-weight: 600; margin-top: 1px; } .brew-notes { font-size: 13px; color: var(--text2); margin-top: 10px; font-style: italic; line-height: 1.5; } /* ── Empty State ── */ .empty { text-align: center; padding: 48px 20px; color: var(--text3); } .empty-icon { font-size: 40px; margin-bottom: 12px; opacity: 0.5; } .empty h3 { font-size: 16px; margin-bottom: 6px; color: var(--text2); } .empty p { font-size: 13px; line-height: 1.5; } /* ── Stats row ── */ .stats { display: flex; gap: 8px; margin-bottom: 20px; } .stat-card { flex: 1; background: var(--card); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 12px; text-align: center; } .stat-num { font-family: 'Playfair Display', serif; font-size: 24px; font-weight: 700; } .stat-label { font-size: 10px; color: var(--text3); text-transform: uppercase; letter-spacing: 1px; margin-top: 2px; } /* ── Forms ── */ .modal-overlay { position: fixed; inset: 0; background: rgba(44,24,16,0.4); z-index: 100; display: flex; align-items: flex-end; justify-content: center; animation: fadeIn 0.2s; } @keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } } @keyframes slideUp { from { transform: translateY(100%); } to { transform: translateY(0); } } @keyframes pulse { 0% { opacity: 0.3; } 50% { opacity: 1; } 100% { opacity: 0.3; } } .modal { background: var(--bg); border-radius: 20px 20px 0 0; width: 100%; max-width: 480px; max-height: 88vh; overflow-y: auto; animation: slideUp 0.3s ease-out; padding: 0 20px 32px; } .modal-handle { width: 36px; height: 4px; background: var(--border); border-radius: 4px; margin: 12px auto 16px; } .modal-title { font-family: 'Playfair Display', serif; font-size: 20px; font-weight: 600; margin-bottom: 20px; } .form-group { margin-bottom: 16px; } .form-label { font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.8px; color: var(--text2); margin-bottom: 6px; display: block; } .form-input, .form-select, .form-textarea { width: 100%; padding: 12px 14px; border: 1px solid var(--border); border-radius: var(--radius-sm); background: var(--card); font-family: 'Source Sans 3', sans-serif; font-size: 14px; color: var(--text); transition: border-color 0.2s; outline: none; } .form-input:focus, .form-select:focus, .form-textarea:focus { border-color: var(--accent); } .form-textarea { resize: vertical; min-height: 80px; } .form-row { display: flex; gap: 10px; } .form-row > .form-group { flex: 1; } .form-hint { font-size: 11px; color: var(--text3); margin-top: 4px; } .method-tabs { display: flex; gap: 6px; margin-bottom: 20px; } .method-tab { flex: 1; padding: 12px 8px; border: 2px solid var(--border); background: var(--card); border-radius: var(--radius-sm); cursor: pointer; text-align: center; font-family: 'Source Sans 3', sans-serif; font-size: 12px; font-weight: 600; transition: all 0.2s; letter-spacing: 0.3px; } .method-tab.active { border-color: currentColor; } .method-tab-icon { font-size: 20px; margin-bottom: 4px; } .btn { width: 100%; padding: 14px; border: none; border-radius: var(--radius-sm); font-family: 'Source Sans 3', sans-serif; font-size: 14px; font-weight: 600; cursor: pointer; letter-spacing: 0.3px; transition: opacity 0.2s; margin-top: 8px; } .btn:hover { opacity: 0.9; } .btn-primary { background: var(--text); color: var(--bg); } .btn-outline { background: transparent; border: 1px solid var(--border); color: var(--text2); } .btn-danger { background: transparent; border: 1px solid var(--danger); color: var(--danger); font-size: 12px; padding: 10px; } /* ── Image Upload ── */ .img-upload { border: 2px dashed var(--border); border-radius: var(--radius-sm); padding: 20px; text-align: center; cursor: pointer; transition: border-color 0.2s, background 0.2s; position: relative; overflow: hidden; } .img-upload:hover { border-color: var(--accent); background: rgba(139,105,20,0.03); } .img-upload.has-image { padding: 0; border-style: solid; } .img-upload-icon { font-size: 28px; margin-bottom: 6px; opacity: 0.4; } .img-upload-text { font-size: 12px; color: var(--text3); } .img-upload input[type="file"] { position: absolute; inset: 0; opacity: 0; cursor: pointer; } .img-preview { width: 100%; height: 160px; object-fit: cover; border-radius: var(--radius-sm); display: block; } .img-remove { position: absolute; top: 8px; right: 8px; width: 28px; height: 28px; border-radius: 50%; background: rgba(44,24,16,0.7); color: white; border: none; font-size: 14px; cursor: pointer; display: flex; align-items: center; justify-content: center; } .bean-card-img { width: 52px; height: 52px; border-radius: var(--radius-sm); object-fit: cover; flex-shrink: 0; } .bean-detail-img { width: 100%; height: 180px; object-fit: cover; border-radius: var(--radius); margin-bottom: 16px; } .bean-tasting { font-size: 13px; color: var(--text2); font-style: italic; margin-top: 6px; line-height: 1.4; } /* ── FAB ── */ .fab { position: fixed; bottom: 24px; right: calc(50% - 220px); width: 54px; height: 54px; border-radius: 50%; background: var(--text); color: var(--bg); border: none; font-size: 26px; cursor: pointer; box-shadow: var(--shadow-lg); z-index: 60; display: flex; align-items: center; justify-content: center; transition: transform 0.2s; } .fab:hover { transform: scale(1.08); } .fab-menu { position: fixed; bottom: 88px; right: calc(50% - 220px); display: flex; flex-direction: column; gap: 8px; z-index: 61; animation: fadeIn 0.15s; } .fab-option { display: flex; align-items: center; gap: 10px; padding: 10px 16px; background: var(--card); border: 1px solid var(--border); border-radius: 28px; font-family: 'Source Sans 3', sans-serif; font-size: 13px; font-weight: 600; cursor: pointer; box-shadow: var(--shadow); white-space: nowrap; transition: all 0.2s; } .fab-option:hover { box-shadow: var(--shadow-lg); } /* ── Section Header ── */ .section-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 12px; } .section-title { font-size: 13px; font-weight: 600; color: var(--text2); text-transform: uppercase; letter-spacing: 1px; } /* ── Filter pills ── */ .filter-pills { display: flex; gap: 6px; margin-bottom: 16px; overflow-x: auto; padding-bottom: 4px; } .filter-pill { padding: 6px 14px; border-radius: 20px; border: 1px solid var(--border); background: var(--card); font-family: 'Source Sans 3', sans-serif; font-size: 12px; font-weight: 500; color: var(--text2); cursor: pointer; white-space: nowrap; transition: all 0.2s; } .filter-pill.active { background: var(--text); color: var(--bg); border-color: var(--text); } /* ── Detail view ── */ .detail-back { display: flex; align-items: center; gap: 6px; background: none; border: none; font-family: 'Source Sans 3', sans-serif; font-size: 13px; color: var(--text2); cursor: pointer; padding: 0; margin-bottom: 16px; } .detail-section { margin-top: 20px; } `; // ─── Components ─── function BeanForm({ onSave, onClose, initial }) { const [form, setForm] = useState(initial || { name: "", roastery: "", roastDate: "", roastType: "", image: "", tastingNotes: "" }); const set = (k, v) => setForm(p => ({ ...p, [k]: v })); const canSave = form.name.trim().length > 0; const handleImage = (e) => { const file = e.target.files?.[0]; if (!file) return; if (file.size > 2 * 1024 * 1024) { alert("Image must be under 2 MB"); return; } const reader = new FileReader(); reader.onload = () => set("image", reader.result); reader.readAsDataURL(file); }; return (
e.stopPropagation()}>
{initial ? "Edit Bean" : "Add Bean"}
set("name", e.target.value)} />
set("roastery", e.target.value)} />
set("roastDate", e.target.value)} />
{form.image ? ( <> Bean ) : ( <>
📷
Tap to upload a photo
)}
Max 2 MB · JPG, PNG, or WebP