'use client' import { useState, useEffect, useCallback } from 'react' import { useRouter } from 'next/navigation' import { ClipboardList, Plus, CheckCircle2, Circle, Users, Clock, AlertCircle, ChevronRight } from 'lucide-react' import { Card, Button, showToast } from '@/components/ui' import { Header, PageContainer } from '@/components/layout/header' import { useApp } from '../provider' interface CareTask { id: string title: string description?: string assignedTo?: { id: string; name: string } dueAt?: string completedAt?: string completedBy?: { id: string; name: string } priority: 'LOW' | 'MEDIUM' | 'HIGH' | 'URGENT' category: 'MEDICATION' | 'APPOINTMENT' | 'SYMPTOM' | 'GENERAL' createdAt: string createdBy: { id: string; name: string } } interface HandoffNote { id: string content: string category: 'GENERAL' | 'SYMPTOM' | 'MEDICATION' | 'MOOD' priority: 'LOW' | 'NORMAL' | 'HIGH' createdAt: string createdBy: { id: string; name: string } expiresAt: string acknowledgedBy: string[] } const PRIORITY_COLORS = { LOW: 'bg-blue-100 text-blue-700', MEDIUM: 'bg-yellow-100 text-yellow-700', HIGH: 'bg-orange-100 text-orange-700', URGENT: 'bg-red-100 text-red-700', } const CATEGORY_ICONS = { MEDICATION: '💊', APPOINTMENT: '📅', SYMPTOM: '😷', GENERAL: '📝', } export default function CareCoordinationPage() { const router = useRouter() const { currentWorkspace } = useApp() const [tasks, setTasks] = useState([]) const [handoffNotes, setHandoffNotes] = useState([]) const [loading, setLoading] = useState(true) const [activeTab, setActiveTab] = useState<'tasks' | 'notes'>('tasks') const fetchData = useCallback(async () => { try { const [tasksRes, notesRes] = await Promise.all([ fetch(`/api/workspaces/${currentWorkspace.id}/care-tasks`), fetch(`/api/workspaces/${currentWorkspace.id}/handoff-notes`) ]) if (tasksRes.ok) { const tasksData = await tasksRes.json() setTasks(tasksData.tasks) } if (notesRes.ok) { const notesData = await notesRes.json() setHandoffNotes(notesData.notes) } } catch (err) { console.error('Failed to fetch care data:', err) } finally { setLoading(false) } }, [currentWorkspace.id]) useEffect(() => { fetchData() }, [fetchData]) const handleCompleteTask = async (taskId: string) => { try { const response = await fetch( `/api/workspaces/${currentWorkspace.id}/care-tasks/${taskId}/complete`, { method: 'POST' } ) if (!response.ok) throw new Error('Failed to complete task') showToast('Task completed!', 'success') fetchData() } catch { showToast('Failed to complete task', 'error') } } const handleAcknowledgeNote = async (noteId: string) => { try { await fetch( `/api/workspaces/${currentWorkspace.id}/handoff-notes/${noteId}/acknowledge`, { method: 'POST' } ) fetchData() } catch { console.error('Failed to acknowledge note') } } // Separate active and completed tasks const activeTasks = tasks.filter(t => !t.completedAt) const completedTasks = tasks.filter(t => t.completedAt).slice(0, 5) // Filter unacknowledged notes const unacknowledgedNotes = handoffNotes.filter( n => !n.acknowledgedBy.includes(currentWorkspace.userId || '') ) if (loading) { return ( <>
) } return ( <>
, label: 'Add', onClick: () => router.push('/care/new-task') }} /> {/* Tab Switcher */}
{/* Tasks View */} {activeTab === 'tasks' && ( <> {/* Active Tasks */}

Active Tasks

{activeTasks.length === 0 ? (

No active tasks

Add tasks to coordinate care

{currentWorkspace.role !== 'VIEWER' && ( )}
) : (
{activeTasks.map(task => ( handleCompleteTask(task.id)} canComplete={currentWorkspace.role !== 'VIEWER'} /> ))}
)}
{/* Recently Completed */} {completedTasks.length > 0 && (

Recently Completed

{completedTasks.map(task => (
{task.title}
))}
)} )} {/* Handoff Notes View */} {activeTab === 'notes' && ( <> {/* Quick Add Note */} {currentWorkspace.role !== 'VIEWER' && (

Quick Handoff Note

Leave a note for other caregivers about symptoms, observations, or important updates.

)} {/* Unacknowledged Notes */} {unacknowledgedNotes.length > 0 && (

New for You

{unacknowledgedNotes.map(note => ( handleAcknowledgeNote(note.id)} /> ))}
)} {/* All Notes */}

Recent Notes

{handoffNotes.length === 0 ? (

No handoff notes

Notes help coordinate care between family members

) : (
{handoffNotes.slice(0, 10).map(note => ( ))}
)}
)}
) } interface TaskCardProps { task: CareTask onComplete: () => void canComplete: boolean } function TaskCard({ task, onComplete, canComplete }: TaskCardProps) { const isOverdue = task.dueAt && new Date(task.dueAt) < new Date() && !task.completedAt return (
{canComplete ? ( ) : ( )}
{CATEGORY_ICONS[task.category]}

{task.title}

{task.priority}
{task.description && (

{task.description}

)}
{task.assignedTo && ( {task.assignedTo.name} )} {task.dueAt && ( {isOverdue ? 'Overdue: ' : 'Due: '} {new Date(task.dueAt).toLocaleDateString()} )}
) } interface HandoffNoteCardProps { note: HandoffNote isNew: boolean onAcknowledge?: () => void } function HandoffNoteCard({ note, isNew, onAcknowledge }: HandoffNoteCardProps) { const CATEGORY_COLORS = { GENERAL: 'bg-blue-50 border-blue-200', SYMPTOM: 'bg-red-50 border-red-200', MEDICATION: 'bg-green-50 border-green-200', MOOD: 'bg-purple-50 border-purple-200', } const PRIORITY_BADGES = { LOW: '', NORMAL: '', HIGH: '🔴 High Priority', } return (
{note.priority === 'HIGH' && ( {PRIORITY_BADGES[note.priority]} )}

{note.content}

{note.createdBy.name} • {new Date(note.createdAt).toLocaleDateString()} {isNew && onAcknowledge && ( )}
) }