diff --git a/server/db.js b/server/db.js index 84e8c8b..d1fbffe 100644 --- a/server/db.js +++ b/server/db.js @@ -50,9 +50,15 @@ const initDb = async () => { rating INTEGER, created_at BIGINT, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, - is_deleted BOOLEAN DEFAULT FALSE + is_deleted BOOLEAN DEFAULT FALSE, + recipe_data JSONB DEFAULT '{}' ) `); + + // Ensure the recipe_data column exists on older db setups + await pool.query(` + ALTER TABLE brew_logs ADD COLUMN IF NOT EXISTS recipe_data JSONB DEFAULT '{}' + `); console.log('Database initialized'); } catch (err) { console.error('Error initializing database', err); diff --git a/server/index.js b/server/index.js index f2ded22..64b6b45 100644 --- a/server/index.js +++ b/server/index.js @@ -191,9 +191,16 @@ app.post('/api/sync', authenticateToken, async (req, res) => { // 2. Process incoming brew logs for (const log of brewLogs) { + const { id, beanId, method, notes, rating, createdAt, updatedAt, isDeleted, ...recipeFields } = log; + const grind = log.grindSize || log.grind || ''; + const waterTemp = log.waterTemp || ''; + const ratio = log.brewRatio || log.ratio || ''; + const yieldVal = log.yield || ''; + const time = log.brewTime || log.time || ''; + await client.query(` - INSERT INTO brew_logs (id, user_id, bean_id, method, grind, water_temp, ratio, yield, time, notes, rating, created_at, updated_at, is_deleted) - VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14) + INSERT INTO brew_logs (id, user_id, bean_id, method, grind, water_temp, ratio, yield, time, notes, rating, created_at, updated_at, is_deleted, recipe_data) + VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15) ON CONFLICT (id) DO UPDATE SET bean_id = EXCLUDED.bean_id, method = EXCLUDED.method, @@ -206,24 +213,26 @@ app.post('/api/sync', authenticateToken, async (req, res) => { rating = EXCLUDED.rating, created_at = EXCLUDED.created_at, updated_at = EXCLUDED.updated_at, - is_deleted = EXCLUDED.is_deleted + is_deleted = EXCLUDED.is_deleted, + recipe_data = EXCLUDED.recipe_data WHERE (EXCLUDED.updated_at > brew_logs.updated_at OR brew_logs.user_id IS NULL) AND (brew_logs.user_id = EXCLUDED.user_id OR brew_logs.user_id IS NULL) `, [ - log.id, + id, userId, - log.beanId, - log.method || '', - log.grind || '', - log.waterTemp || '', - log.ratio || '', - log.yield || '', - log.time || '', - log.notes || '', - log.rating || 0, - log.createdAt ? BigInt(log.createdAt) : BigInt(Date.now()), - log.updatedAt ? new Date(log.updatedAt) : new Date(), - log.isDeleted || false + beanId, + method || '', + grind, + waterTemp, + ratio, + yieldVal, + time, + notes || '', + rating || 0, + createdAt ? BigInt(createdAt) : BigInt(Date.now()), + updatedAt ? new Date(updatedAt) : new Date(), + isDeleted || false, + JSON.stringify(recipeFields) ]); } @@ -256,21 +265,34 @@ app.post('/api/sync', authenticateToken, async (req, res) => { isDeleted: b.is_deleted })); - const mappedLogs = serverLogs.rows.map(l => ({ - id: l.id, - beanId: l.bean_id, - method: l.method, - grind: l.grind, - waterTemp: l.water_temp, - ratio: l.ratio, - yield: l.yield, - time: l.time, - notes: l.notes, - rating: l.rating, - createdAt: Number(l.created_at), - updatedAt: l.updated_at.toISOString(), - isDeleted: l.is_deleted - })); + const mappedLogs = serverLogs.rows.map(l => { + const baseLog = { + id: l.id, + beanId: l.bean_id, + method: l.method, + notes: l.notes, + rating: l.rating, + createdAt: Number(l.created_at), + updatedAt: l.updated_at.toISOString(), + isDeleted: l.is_deleted + }; + + const recipeData = l.recipe_data || {}; + + // Fallback for old database rows that don't have recipe_data populated + const fallback = {}; + if (!recipeData.grindSize && l.grind) fallback.grindSize = l.grind; + if (!recipeData.waterTemp && l.water_temp) fallback.waterTemp = l.water_temp; + if (!recipeData.brewRatio && l.ratio) fallback.brewRatio = l.ratio; + if (!recipeData.yield && l.yield) fallback.yield = l.yield; + if (!recipeData.brewTime && l.time) fallback.brewTime = l.time; + + return { + ...baseLog, + ...fallback, + ...recipeData + }; + }); res.json({ serverTime, diff --git a/src/App.jsx b/src/App.jsx index dcea2b7..6731531 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -20,7 +20,7 @@ import SyncIndicator from "./components/SyncIndicator"; // Import constants -import { METHODS, METHOD_LABELS, METHOD_ICONS, METHOD_COLORS } from "./constants"; +import { METHODS, METHOD_LABELS, METHOD_ICONS, METHOD_COLORS, getRoastAgingInfo } from "./constants"; // ─── Storage helpers ─── const STORAGE_KEY = "coffee-logbook-data"; @@ -62,6 +62,121 @@ const LoadingScreen = () => ( ); +function MethodIcon({ method, className }) { + const props = { + className: className || "w-8 h-8", + viewBox: "0 0 24 24", + fill: "none", + stroke: "currentColor", + strokeWidth: "1.5", + strokeLinecap: "round", + strokeLinejoin: "round" + }; + + switch (method) { + case "pourover": + return ( + + ); + case "espresso": + return ( + + ); + case "coldbrew": + return ( + + ); + case "aeropress": + return ( + + ); + case "frenchpress": + return ( + + ); + case "chemex": + return ( + + ); + case "mokapot": + return ( + + ); + case "v60": + return ( + + ); + case "syphon": + return ( + + ); + case "dripbrew": + return ( + + ); + default: + return ☕; + } +} + // ─── Main App ─── export default function CoffeeLogbook() { const { token, user, loading, logout } = useContext(AuthContext); @@ -79,8 +194,13 @@ export default function CoffeeLogbook() { const [syncing, setSyncing] = useState(false); const [showSyncedStatus, setShowSyncedStatus] = useState(false); + const [prepActivePage, setPrepActivePage] = useState(0); + useEffect(() => { loadData().then(setData); }, []); - useEffect(() => { setBrewSearchQuery(""); }, [view]); + useEffect(() => { + setBrewSearchQuery(""); + setPrepActivePage(0); + }, [view]); const dataRef = useRef(data); dataRef.current = data; @@ -212,7 +332,8 @@ export default function CoffeeLogbook() { return queryWords.every(word => searchableText.includes(word)); }) .sort((a, b) => b.createdAt - a.createdAt); - const methodCounts = { pourover: 0, espresso: 0, coldbrew: 0 }; + const methodCounts = {}; + METHODS.forEach(m => methodCounts[m] = 0); brewLogs.forEach(l => { if (methodCounts[l.method] !== undefined) methodCounts[l.method]++; }); const filterPillCls = (active) => `px-3.5 py-1.5 rounded-full border text-xs font-medium whitespace-nowrap cursor-pointer transition-all ${active ? "bg-[#2C1810] text-[#FAF6F1] border-[#2C1810] dark:bg-[#FAF6F1] dark:text-[#2C1810] dark:border-[#FAF6F1]" : "bg-white border-[#E8DFD3] text-[#6B5744] dark:bg-[#22120B] dark:border-[#3B2217] dark:text-[#C8B9A6]"}`; @@ -240,6 +361,53 @@ export default function CoffeeLogbook() { {/* ── Dashboard ── */} {view === "dashboard" && (