diff --git a/.gitignore b/.gitignore index 9a5aced..4efc887 100644 --- a/.gitignore +++ b/.gitignore @@ -137,3 +137,6 @@ dist # Vite logs files vite.config.js.timestamp-* vite.config.ts.timestamp-* +branch_structure.json +temp_auto_push.bat +temp_interactive_push.bat diff --git a/app/observations/page.js b/app/observations/page.js index ba141ae..1ae8e06 100644 --- a/app/observations/page.js +++ b/app/observations/page.js @@ -4,14 +4,14 @@ import { useState, useEffect } from 'react'; import { useAuth } from '@/context/AuthContext'; import { getAllSavedObservations } from '@/lib/db'; import { getAllExperiments } from '@/data/labs'; -import ExperimentCard from '@/components/ExperimentCard'; +import { fetchExperimentData } from '@/lib/actions'; import Link from 'next/link'; -import styles from '@/app/preferences/Preferences.module.css'; -import homeStyles from '@/components/SearchBar.module.css'; +import preferencesStyles from '@/app/preferences/Preferences.module.css'; +import expStyles from '@/components/experiment/Experiment.module.css'; export default function ObservationsPage() { const { user, loading: authLoading } = useAuth(); - const [observationList, setObservationList] = useState([]); + const [savedTables, setSavedTables] = useState([]); const [loading, setLoading] = useState(true); useEffect(() => { @@ -21,45 +21,64 @@ export default function ObservationsPage() { setLoading(false); } - // Listen for instant updates const handleUpdate = () => fetchObservations(); window.addEventListener('workspace-updated', handleUpdate); return () => window.removeEventListener('workspace-updated', handleUpdate); }, [user, authLoading]); const fetchObservations = async () => { - // Safety timeout to prevent infinite "Loading..." const timeout = setTimeout(() => { setLoading(false); - console.warn('Fetch timed out (4s)'); - }, 4000); + console.warn('Fetch timed out for observations'); + }, 8000); try { const { data, error } = await getAllSavedObservations(user.id); - if (!error && data) { + if (!error && data && data.length > 0) { const allExps = getAllExperiments(); - // Group by experiment_id to avoid showing the same card multiple times - const uniqueExpIds = [...new Set(data.map(item => item.experiment_id))]; - - const enriched = uniqueExpIds.map(expId => { + // Fetch full experiment JSONs to get table headers + const tableViews = await Promise.all(data.map(async (obs) => { let labId, eId; - if (String(expId).includes('/')) { - [labId, eId] = String(expId).split('/'); + if (String(obs.experiment_id).includes('/')) { + [labId, eId] = String(obs.experiment_id).split('/'); } else { - eId = expId; + eId = obs.experiment_id; } - - const found = allExps.find(e => + + const foundExpMeta = allExps.find(e => String(e.id) === String(eId) && (!labId || String(e.labId) === String(labId)) ); + + // Skip if missing lab mapping or orphaned data + if (!foundExpMeta || !labId) return null; + + // Fetch full JSON using Server Action + const expData = await fetchExperimentData(labId, eId); + if (!expData || !expData.sections || !expData.sections[obs.section_id]) return null; + + const section = expData.sections[obs.section_id]; + const tableBlock = section.content?.find(b => b.type === 'table'); - const lastUpdated = data.find(d => d.experiment_id === expId)?.updated_at; - return found ? { ...found, viewed_at: lastUpdated } : null; - }).filter(Boolean); + // Fallback to empty structure if no table found (in case of schema changes) + const headers = tableBlock?.headers || []; - setObservationList(enriched); + return { + id: `${obs.experiment_id}-${obs.section_id}`, + experimentName: foundExpMeta.name, + labName: foundExpMeta.labName, + sectionTitle: section.title || obs.section_id, + updatedAt: obs.updated_at, + headers: headers, + rows: obs.data || [], + linkUrl: `/lab/${labId}/experiment/${eId}` + }; + })); + + setSavedTables(tableViews.filter(Boolean)); + } else { + setSavedTables([]); } } finally { clearTimeout(timeout); @@ -69,16 +88,16 @@ export default function ObservationsPage() { if (authLoading || loading) { return ( -
-
Loading your saved data...
+
+
Loading your saved data...
); } if (!user) { return ( -
-
+
+

Please Log In

Log in to view your saved laboratory observations and cloud-synced data.

@@ -87,28 +106,81 @@ export default function ObservationsPage() { } return ( -
-