All files / src/features/vaccines/components VaccinesOverviewCard.tsx

53.33% Statements 8/15
26.92% Branches 7/26
40% Functions 2/5
53.84% Lines 7/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                        2x                       7x 12x   7x   7x   7x               7x                                                                                                                                                                    
import { useTranslation } from 'react-i18next'
import { Link } from 'react-router-dom'
 
import type { Baby } from '@/features/babies/api/babies.schemas'
import { useCountUp } from '@/hooks/useCountUp'
import { cn } from '@/lib/utils'
import { SyringeIcon } from '@/shared/icons/syringe-icon'
import { babyAvatarAppearance, babyInitials } from '@/shared/utils/babyAvatarColor'
 
import type { VaccineItemWithBaby } from '../api/vaccines.hooks'
import { vaccineProgress } from './vaccine-progress'
 
const MAX_ITEMS = 5
 
interface VaccinesOverviewCardProps {
  babies: Baby[]
  items: VaccineItemWithBaby[]
  isPending: boolean
  isError: boolean
}
 
// Household-wide vaccines widget for the dashboard: one merged, urgency-sorted
// list across every child instead of repeating a full vaccines section per baby.
export function VaccinesOverviewCard({ babies, items, isPending, isError }: VaccinesOverviewCardProps) {
  const { t } = useTranslation()
  const babyById = new Map(babies.map((baby) => [baby.id, baby]))
 
  const progress = vaccineProgress(items)
  // Counts up to the value, and the bar fills from empty (option 36).
  const shownPercent = useCountUp(progress.percent)
 
  const urgentItems = [...items]
    .filter((item) => item.status !== 'APPLIED' && item.status !== 'GUIDANCE')
    .sort((a, b) => {
      if (a.status !== b.status) return a.status === 'DELAYED' ? -1 : 1
      return a.recommendedAgeInMonths - b.recommendedAgeInMonths
    })
    .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-emerald-50 dark:bg-emerald-950/40 text-emerald-700 dark:text-emerald-300">
            <SyringeIcon className="h-[18px] w-[18px]" />
          </span>
          <h3 className="font-display text-lg font-medium text-ink">{t('nav.vaccines')}</h3>
        </div>
        <Link to="/vaccines" className="text-xs font-semibold text-emerald-700 dark:text-emerald-300">
          {t('babies.dashboard.viewAll')}
        </Link>
      </div>
 
      {!isPending && !isError && items.length > 0 && (
        <div className="mb-4">
          <div className="mb-1.5 flex items-center justify-between text-[13px]">
            <span className="font-semibold text-ink">{t('vaccines.progressLabel')}</span>
            <span className="font-sans tabular-nums text-primary">{shownPercent}%</span>
          </div>
          <div className="h-2 overflow-hidden rounded-full bg-muted">
            <div
              className="h-full origin-left animate-grow-x rounded-full bg-gradient-to-r from-emerald-500 to-emerald-400 transition-all duration-500"
              style={{ width: `${progress.percent}%` }}
            />
          </div>
        </div>
      )}
 
      <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('vaccines.genericError')}</p>
        ) : urgentItems.length === 0 ? (
          <p className="py-6 text-center text-[13px] text-ink-muted">{t('babies.dashboard.vaccinesAllUpToDate')}</p>
        ) : (
          urgentItems.map((item) => {
            const baby = babyById.get(item.babyId)
            const avatarAppearance = baby ? babyAvatarAppearance(baby.id, baby.avatarColor) : null
            return (
              <div
                key={`${item.babyId}-${item.vaccineId}`}
                className={cn(
                  'flex items-center gap-2.5 rounded-lg px-3 py-2.5',
                  item.status === 'DELAYED' ? 'bg-rose-50 dark:bg-rose-950/40' : 'bg-surface',
                )}
              >
                {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">{item.name}</p>
                  <p className="truncate text-[11px] text-ink-muted">
                    {baby?.name} ยท {t('vaccines.doseLabel', { count: item.doseNumber })}
                  </p>
                </div>
                <span
                  className={cn(
                    'flex-shrink-0 rounded-full px-2 py-0.5 text-[11px] font-semibold',
                    item.status === 'DELAYED' ? 'bg-rose-100 dark:bg-rose-900/40 text-rose-700 dark:text-rose-300' : 'bg-amber-100 dark:bg-amber-900/40 text-amber-800 dark:text-amber-300',
                  )}
                >
                  {item.status === 'DELAYED' ? t('vaccines.status.delayed') : t('vaccines.status.pending')}
                </span>
              </div>
            )
          })
        )}
      </div>
    </div>
  )
}