'use client'; import { useEffect, useState } from 'react'; import { useTranslations } from 'next-intl'; import { ChevronRight } from 'lucide-react'; import { Link } from '@/i18n/navigation'; import { Card } from '@/components/ui/shared/Card'; import { formatTimeForInput } from '@/components/appointments/appointmentTime'; import { purposeLabel } from '@/components/ui/appointments/appointmentPurposeStyles'; import { treatmentAppointmentHref } from '@/components/shared/treatmentSelection'; import { treatmentTypeColor } from '@/components/ui/treatment/treatmentTypeDisplay'; import { canViewMyAppointmentsWeekChart } from '@/components/shared/permissions'; import { useAuth } from '@/lib/hooks/useAuth'; import { treatmentCatalogApi } from '@/lib/api/treatment-catalog'; import { ListRowSkeleton } from '@/components/today/TodaySkeleton'; import type { TreatmentCatalogEntry } from '@/types/treatment-catalog'; import type { TodaySummaryActions } from '@/types/today'; interface TodayUpcomingAppointmentsProps { actions: TodaySummaryActions; loading?: boolean; isInitialLoad?: boolean; } export function TodayUpcomingAppointments({ actions, loading = false, isInitialLoad = false, }: TodayUpcomingAppointmentsProps) { const t = useTranslations('today'); const { currentOrganization } = useAuth(); const [treatmentCatalog, setTreatmentCatalog] = useState([]); useEffect(() => { void treatmentCatalogApi .list() .then((response) => setTreatmentCatalog(response.data)) .catch(() => {}); }, []); if ( !currentOrganization || currentOrganization.type !== 'CLINIC' || !canViewMyAppointmentsWeekChart(currentOrganization) ) { return null; } const appointments = actions.upcomingAppointmentsToday ?? []; if (isInitialLoad) { return (
{[0, 1].map((key) => ( ))}
); } return (

{t('upcomingAppointmentsTitle')}

{t('upcomingAppointmentsSubtitle')}

{t('viewAllAppointments')}
{appointments.length === 0 ? (

{t('noUpcomingAppointments')}

) : (
    {appointments.map((appointment) => { const start = new Date(appointment.startAt); const end = new Date(appointment.endAt); const timeLabel = `${formatTimeForInput(start)} – ${formatTimeForInput(end)}`; const purposeIndex = treatmentCatalog.findIndex( (entry) => entry.code === appointment.purpose, ); const purposeTextColor = treatmentTypeColor( appointment.purpose, purposeIndex < 0 ? 0 : purposeIndex, ); const purposeDisplay = purposeLabel(appointment.purpose, treatmentCatalog); return (
  • {appointment.patientName}

    {timeLabel} {appointment.purpose ? ( · {purposeDisplay} ) : null}

  • ); })}
)}
); }