All files / src/features/milestones/components MilestonesOverviewCard.tsx

100% Statements 11/11
80% Branches 8/10
100% Functions 5/5
100% Lines 10/10

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                      2x                                 7x 12x   7x 2x     7x                                                             3x                                 5x 5x 5x                                                                                  
import { useTranslation } from 'react-i18next'
import { Link } from 'react-router-dom'
 
import type { Baby } from '@/features/babies/api/babies.schemas'
import { formatDateDisplay } from '@/lib/date'
import { SparkleIcon } from '@/shared/icons/sparkle-icon'
 
import type { Milestone } from '../api/milestones.schemas'
import { MILESTONE_CATEGORY_META } from './category-meta'
import { MILESTONE_SUGGESTIONS } from './milestone-suggestions'
 
const MAX_ITEMS = 4
 
interface MilestonesOverviewCardProps {
  babies: Baby[]
  items: Milestone[]
  isPending: boolean
  isError: boolean
}
 
// Household-wide milestones widget for the dashboard: the most recent
// milestones across every child, newest first.
export function MilestonesOverviewCard({
  babies,
  items,
  isPending,
  isError,
}: MilestonesOverviewCardProps) {
  const { t, i18n } = useTranslation()
  const babyById = new Map(babies.map((baby) => [baby.id, baby]))
 
  const latest = [...items]
    .sort((a, b) => b.achievedAt.localeCompare(a.achievedAt))
    .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-amber-50 dark:bg-amber-950/40 text-amber-600 dark:text-amber-300">
            <SparkleIcon className="h-[18px] w-[18px]" />
          </span>
          <h3 className="font-display text-lg font-medium text-ink">{t('nav.milestones')}</h3>
        </div>
        <Link to="/milestones" className="text-xs font-semibold text-emerald-700 dark:text-emerald-300">
          {t('babies.dashboard.viewAll')}
        </Link>
      </div>
 
      <div className="flex flex-1 flex-col gap-3">
        {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('milestones.genericError')}</p>
        ) : latest.length === 0 ? (
          /* Antes: "Nenhum marco registrado ainda", que relata a ausência e não
             convida a nada. Um painel recém-criado tinha seis vazios assim.
             Agora mostra o que outras famílias registram — exemplos, nunca um
             calendário do que a criança "deveria" fazer — e leva para a tela
             onde eles abrem o formulário já preenchido. */
          <div className="py-4">
            <p className="text-ink-muted mb-3 text-center text-[13px]">
              {t('babies.dashboard.noMilestonesYet')}
            </p>
            <ul className="mb-4 space-y-2">
              {MILESTONE_SUGGESTIONS.slice(0, 3).map((item) => (
                <li key={item.titleKey} className="flex items-center gap-2.5">
                  <span aria-hidden="true" className="text-base">
                    {MILESTONE_CATEGORY_META[item.category].emoji}
                  </span>
                  <span className="text-ink-muted text-[13px]">{t(item.titleKey)}</span>
                </li>
              ))}
            </ul>
            <Link
              to="/milestones"
              className="text-primary text-[13px] font-bold underline-offset-4 hover:underline"
            >
              {t('babies.dashboard.milestonesEmptyCta')}
            </Link>
          </div>
        ) : (
          latest.map((milestone) => {
            const baby = babyById.get(milestone.babyId)
            const meta = MILESTONE_CATEGORY_META[milestone.category]
            return (
              <div key={milestone.id} className="flex items-start gap-3">
                {milestone.photoUrl ? (
                  <img
                    src={milestone.photoUrl}
                    alt=""
                    loading="lazy"
                    className="size-16 shrink-0 rounded-xl object-cover"
                  />
                ) : (
                  <span
                    aria-hidden
                    className="flex size-12 shrink-0 items-center justify-center rounded-xl bg-amber-50 text-xl dark:bg-amber-950/40"
                  >
                    {meta.emoji}
                  </span>
                )}
                <div className="min-w-0 flex-1">
                  <p className="mb-0.5 truncate text-[13px] font-bold text-ink">
                    {milestone.title}
                  </p>
                  <div className="flex items-center gap-2">
                    {baby && (
                      <span className="truncate text-[11px] font-semibold text-ink-muted">
                        {baby.name}
                      </span>
                    )}
                    <span className="text-[11px] text-ink-faint">·</span>
                    <span className="font-sans tabular-nums text-[11px] text-ink-muted">
                      {formatDateDisplay(milestone.achievedAt, i18n.language)}
                    </span>
                  </div>
                </div>
              </div>
            )
          })
        )}
      </div>
    </div>
  )
}