All files / src/features/appointments/components AppointmentsOverviewCard.tsx

64.28% Statements 9/14
66.66% Branches 12/18
66.66% Functions 4/6
61.53% Lines 8/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139                      2x                                 7x 12x   7x 5x   7x     5x           7x                                                                                                                                                                                              
import { useTranslation } from 'react-i18next'
import { Link } from 'react-router-dom'
 
import type { Baby } from '@/features/babies/api/babies.schemas'
import { formatDateDisplay, splitScheduledAt } from '@/lib/date'
import { cn } from '@/lib/utils'
import { StethoscopeIcon } from '@/shared/icons/stethoscope-icon'
import { babyAvatarAppearance, babyInitials } from '@/shared/utils/babyAvatarColor'
 
import type { Appointment } from '../api/appointments.schemas'
 
const MAX_ITEMS = 4
 
interface AppointmentsOverviewCardProps {
  babies: Baby[]
  items: Appointment[]
  isPending: boolean
  isError: boolean
}
 
// Household-wide appointments widget for the dashboard: the next scheduled
// visits across every child, merged into a single chronological list.
export function AppointmentsOverviewCard({
  babies,
  items,
  isPending,
  isError,
}: AppointmentsOverviewCardProps) {
  const { t, i18n } = useTranslation()
  const babyById = new Map(babies.map((baby) => [baby.id, baby]))
 
  const pastScheduled = items.filter(
    (item) => item.status === 'SCHEDULED' && new Date(item.scheduledAt).getTime() < Date.now(),
  ).length
  const upcoming = items
    .filter(
      (appointment) =>
        appointment.status === 'SCHEDULED' &&
        new Date(appointment.scheduledAt).getTime() >= Date.now(),
    )
    .sort((a, b) => a.scheduledAt.localeCompare(b.scheduledAt))
    .slice(0, MAX_ITEMS)
 
  return (
    <div className="card-lift flex flex-col rounded-2xl border border-border bg-card p-5 sm:p-6">
      <div className="mb-4 flex items-center justify-between">
        <div className="flex items-center gap-2.5">
          <span className="flex h-9 w-9 flex-shrink-0 items-center justify-center rounded-xl bg-violet-50 dark:bg-violet-950/40 text-violet-600 dark:text-violet-300">
            <StethoscopeIcon className="h-[18px] w-[18px]" />
          </span>
          <h3 className="font-display text-lg font-medium text-ink">
            {t('nav.appointments')}
          </h3>
        </div>
        <Link
          to="/appointments"
          className="text-xs font-semibold text-emerald-700 dark:text-emerald-300"
        >
          {t('babies.dashboard.viewAll')}
        </Link>
      </div>
 
      {!isPending && !isError && pastScheduled > 0 && (
        <Link
          to="/appointments"
          className="mb-3 block rounded-xl bg-muted p-3 text-sm text-ink-muted"
        >
          {t('babies.home.pastAppointments', { count: pastScheduled })}
        </Link>
      )}
      <div className="flex flex-1 flex-col gap-2">
        {isPending ? (
          <p className="py-6 text-center text-sm text-ink-muted">{t('common.loading')}</p>
        ) : isError ? (
          <p className="py-6 text-center text-sm text-ink-muted">
            {t('appointments.genericError')}
          </p>
        ) : upcoming.length === 0 ? (
          /* A ausência de consulta agendada não é só ausência: desde 01/09 dá
             para registrar uma que já aconteceu, então o vazio tem dois
             caminhos e não nenhum. Antes esta linha relatava o nada e parava
             ali. */
          <div className="py-4 text-center">
            <p className="text-ink-muted text-[13px]">
              {t('babies.dashboard.noAppointmentsScheduled')}
            </p>
            <p className="text-ink-faint mt-1 text-xs">
              {t('babies.dashboard.appointmentsEmptyHint')}
            </p>
            <Link
              to="/appointments"
              className="text-primary mt-3 inline-block text-[13px] font-bold underline-offset-4 hover:underline"
            >
              {t('babies.dashboard.appointmentsEmptyCta')}
            </Link>
          </div>
        ) : (
          upcoming.map((appointment) => {
            const baby = babyById.get(appointment.babyId)
            const avatarAppearance = baby ? babyAvatarAppearance(baby.id, baby.avatarColor) : null
            const { date, time } = splitScheduledAt(appointment.scheduledAt)
            return (
              <div
                key={appointment.id}
                className="flex items-center gap-2.5 rounded-lg bg-surface px-3 py-2.5"
              >
                {baby && (
                  <span
                    title={baby.name}
                    className={cn(
                      'flex h-6 w-6 flex-shrink-0 items-center justify-center rounded-full text-[10px] font-black',
                      avatarAppearance?.className,
                    )}
                    style={avatarAppearance?.style}
                  >
                    {babyInitials(baby.name)}
                  </span>
                )}
                <div className="min-w-0 flex-1">
                  <p className="truncate text-[13px] font-semibold text-ink">
                    {appointment.doctorName}
                  </p>
                  <p className="truncate text-[11px] text-ink-muted">{baby?.name}</p>
                </div>
                {/* Peso normal de propósito: a pílula ganha destaque pelo fundo
                    tintado, não pelo peso. Algarismos tabulares, como todo dado
                    factual — o porquê está em `--font-mono`, no `index.css`. */}
                <span className="font-sans tabular-nums flex-shrink-0 rounded-full bg-violet-100 dark:bg-violet-900/40 px-2 py-0.5 text-[11px] text-violet-700 dark:text-violet-300">
                  {formatDateDisplay(date, i18n.language)} {time}
                </span>
              </div>
            )
          })
        )}
      </div>
    </div>
  )
}