import { useState, useEffect } from 'react' import { phaseDraftManagement, type ExperimentRepetition, type ExperimentPhase, type ExperimentPhaseDraft, type User } from '../lib/supabase' interface RepetitionPhaseSelectorProps { repetition: ExperimentRepetition currentUser: User 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 RepetitionPhaseSelector({ repetition, currentUser: _currentUser, onPhaseSelect }: RepetitionPhaseSelectorProps) { const [phaseDrafts, setPhaseDrafts] = useState>({ 'pre-soaking': [], 'air-drying': [], 'cracking': [], 'shelling': [] }) const [loading, setLoading] = useState(true) const [error, setError] = useState(null) useEffect(() => { loadPhaseDrafts() }, [repetition.id]) const loadPhaseDrafts = async () => { try { setLoading(true) setError(null) const allDrafts = await phaseDraftManagement.getUserPhaseDraftsForRepetition(repetition.id) // Group drafts by phase const groupedDrafts: Record = { 'pre-soaking': [], 'air-drying': [], 'cracking': [], 'shelling': [] } allDrafts.forEach(draft => { groupedDrafts[draft.phase_name].push(draft) }) setPhaseDrafts(groupedDrafts) } catch (err: any) { setError(err.message || 'Failed to load phase drafts') console.error('Load phase drafts error:', err) } finally { setLoading(false) } } const getPhaseStatus = (phase: ExperimentPhase) => { const drafts = phaseDrafts[phase] if (drafts.length === 0) return 'empty' const hasSubmitted = drafts.some(d => d.status === 'submitted') const hasDraft = drafts.some(d => d.status === 'draft') const hasWithdrawn = drafts.some(d => d.status === 'withdrawn') if (hasSubmitted) return 'submitted' if (hasDraft) return 'draft' if (hasWithdrawn) return 'withdrawn' return 'empty' } const getStatusBadge = (status: string) => { switch (status) { case 'submitted': return Submitted case 'draft': return Draft case 'withdrawn': return Withdrawn case 'empty': return No Data default: return null } } const getDraftCount = (phase: ExperimentPhase) => { return phaseDrafts[phase].length } if (loading) { return (

Loading phases...

) } if (error) { return (
{error}
) } return (

Select Phase

Choose a phase to enter or view data. Each phase can have multiple drafts.

{repetition.is_locked && (
🔒 This repetition is locked by an admin

You can view existing data but cannot create new drafts or modify existing ones.

)}
{phases.map((phase) => { const status = getPhaseStatus(phase.name) const draftCount = getDraftCount(phase.name) return (
onPhaseSelect(phase.name)} className="bg-white rounded-lg shadow-md border border-gray-200 p-6 cursor-pointer hover:shadow-lg hover:border-blue-300 transition-all duration-200" >
{phase.icon}

{phase.title}

{phase.description}

{getStatusBadge(status)}
{draftCount === 0 ? 'No drafts' : `${draftCount} draft${draftCount === 1 ? '' : 's'}`}
{draftCount > 0 && (
{phaseDrafts[phase.name].slice(0, 3).map((draft, index) => ( {draft.draft_name || `Draft ${index + 1}`} ))} {draftCount > 3 && ( +{draftCount - 3} more )}
)}
) })}
) }