import { useState, useEffect } from 'react' import { dataEntryManagement, type ExperimentDataEntry, type ExperimentPhase, type ExperimentPhaseData } from '../lib/supabase' interface PhaseSelectorProps { dataEntry: ExperimentDataEntry onPhaseSelect: (phase: ExperimentPhase) => void } interface PhaseInfo { name: ExperimentPhase title: string description: string icon: string color: string } const phases: PhaseInfo[] = [ { name: 'pre-soaking', title: 'Pre-Soaking', description: 'Initial measurements before soaking process', icon: '🌰', color: 'bg-blue-500' }, { name: 'air-drying', title: 'Air-Drying', description: 'Post-soak measurements and air-drying data', icon: '💨', color: 'bg-green-500' }, { name: 'cracking', title: 'Cracking', description: 'Cracking process timing and parameters', icon: '🔨', color: 'bg-yellow-500' }, { name: 'shelling', title: 'Shelling', description: 'Final measurements and yield data', icon: '📊', color: 'bg-purple-500' } ] export function PhaseSelector({ dataEntry, onPhaseSelect }: PhaseSelectorProps) { const [phaseData, setPhaseData] = useState>({ 'pre-soaking': null, 'air-drying': null, 'cracking': null, 'shelling': null }) const [loading, setLoading] = useState(true) useEffect(() => { loadPhaseData() }, [dataEntry.id]) const loadPhaseData = async () => { try { setLoading(true) const allPhaseData = await dataEntryManagement.getPhaseDataForEntry(dataEntry.id) const phaseDataMap: Record = { 'pre-soaking': null, 'air-drying': null, 'cracking': null, 'shelling': null } allPhaseData.forEach(data => { phaseDataMap[data.phase_name] = data }) setPhaseData(phaseDataMap) } catch (error) { console.error('Failed to load phase data:', error) } finally { setLoading(false) } } const getPhaseCompletionStatus = (phaseName: ExperimentPhase): 'empty' | 'partial' | 'complete' => { const data = phaseData[phaseName] if (!data) return 'empty' // Check if phase has any data const hasAnyData = Object.entries(data).some(([key, value]) => { if (['id', 'data_entry_id', 'phase_name', 'created_at', 'updated_at', 'diameter_measurements'].includes(key)) { return false } return value !== null && value !== undefined && value !== '' }) if (!hasAnyData) return 'empty' // For now, consider any data as partial completion // You could implement more sophisticated completion logic here return 'partial' } const getStatusIcon = (status: 'empty' | 'partial' | 'complete') => { switch (status) { case 'empty': return (
) case 'partial': return (
) case 'complete': return (
) } } const getLastUpdated = (phaseName: ExperimentPhase): string | null => { const data = phaseData[phaseName] if (!data) return null return new Date(data.updated_at).toLocaleString() } if (loading) { return (

Loading phase data...

) } return (

Select Experiment Phase

Click on any phase card to enter or edit data for that phase

{phases.map((phase) => { const status = getPhaseCompletionStatus(phase.name) const lastUpdated = getLastUpdated(phase.name) return ( ) })}
{/* Phase Navigation */}

Phase Progress

{phases.map((phase, index) => { const status = getPhaseCompletionStatus(phase.name) return (
{index < phases.length - 1 && ( )}
) })}
) }