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

79.48% Statements 31/39
73.52% Branches 25/34
66.66% Functions 8/12
83.78% Lines 31/37

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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255                                                                  13x 13x 13x 13x 13x 13x 13x 13x   13x     13x 13x       13x                 13x                 6x                                                         17x 17x 17x 17x 17x 17x 17x 17x     17x           17x                   1x                                                                                                                                       1x                                                                                       1x 1x 1x 1x       1x                        
import { useState } from 'react'
import { useTranslation } from 'react-i18next'
import { toast } from 'sonner'
import { Button } from '@/components/ui/button'
import {
  AlertDialog,
  AlertDialogAction,
  AlertDialogCancel,
  AlertDialogContent,
  AlertDialogDescription,
  AlertDialogFooter,
  AlertDialogHeader,
  AlertDialogTitle,
} from '@/components/ui/alert-dialog'
import {
  Dialog,
  DialogContent,
  DialogDescription,
  DialogHeader,
  DialogTitle,
} from '@/components/ui/dialog'
import type { Baby } from '@/features/babies/api/babies.schemas'
import { formatDateDisplay } from '@/lib/date'
import { config } from '@/lib/config'
import { cn } from '@/lib/utils'
import { TrashIcon } from '@/shared/icons/trash-icon'
import { babyAvatarAppearance, babyInitials } from '@/shared/utils/babyAvatarColor'
import { useDeleteMilestone } from '../api/milestones.hooks'
import type { Milestone } from '../api/milestones.schemas'
import { MILESTONE_CATEGORY_META } from './category-meta'
import { EditMilestoneDialog } from './EditMilestoneDialog'
 
export function MilestoneTimeline({ items, babies }: { items: Milestone[]; babies: Baby[] }) {
  const { i18n } = useTranslation()
  const [editTarget, setEditTarget] = useState<Milestone | null>(null)
  const groups = new Map<string, Milestone[]>()
  for (const item of items) {
    const month = item.achievedAt.slice(0, 7)
    const group = groups.get(month)
    Iif (group) group.push(item)
    else groups.set(month, [item])
  }
  return (
    <div className="space-y-8">
      {[...groups].map(([month, memories]) => {
        const [year, monthNumber] = month.split('-').map(Number)
        const label = new Intl.DateTimeFormat(i18n.language, {
          month: 'long',
          year: 'numeric',
        }).format(new Date(year!, monthNumber! - 1, 1))
        return (
          <section
            key={month}
            aria-label={label}
            className="grid gap-3 lg:grid-cols-[140px_minmax(0,1fr)] lg:gap-6"
          >
            <h2 className="pt-2 font-display text-lg font-bold capitalize text-ink">{label}</h2>
            <ol className="space-y-4 border-l border-amber-600/25 pl-3 sm:pl-5">
              {memories.map((milestone) => (
                <li key={milestone.id} className="relative min-w-0">
                  <span
                    aria-hidden="true"
                    className="absolute -left-[17px] top-7 h-2 w-2 rounded-full bg-amber-700 sm:-left-[25px]"
                  />
                  <MilestoneCard
                    milestone={milestone}
                    baby={
                      babies.length > 1
                        ? babies.find((baby) => baby.id === milestone.babyId)
                        : undefined
                    }
                    onEdit={() => setEditTarget(milestone)}
                  />
                </li>
              ))}
            </ol>
          </section>
        )
      })}
      <EditMilestoneDialog
        babies={babies}
        milestone={editTarget}
        onOpenChange={(open) => !open && setEditTarget(null)}
      />
    </div>
  )
}
 
export function MilestoneCard({
  milestone,
  baby,
  onEdit,
}: {
  milestone: Milestone
  baby?: Baby
  onEdit: () => void
}) {
  const { t, i18n } = useTranslation()
  const deletion = useDeleteMilestone(milestone.babyId)
  const [confirm, setConfirm] = useState(false)
  const [photoOpen, setPhotoOpen] = useState(false)
  const [failedPhoto, setFailedPhoto] = useState<string | null>(null)
  const meta = MILESTONE_CATEGORY_META[milestone.category]
  const avatar = baby ? babyAvatarAppearance(baby.id, baby.avatarColor) : null
  const hasPhoto = !!milestone.photoUrl && failedPhoto !== milestone.photoUrl
  // API assets use CORS; other externally hosted photos may not support it.
  const photoCrossOrigin =
    milestone.photoUrl &&
    /^https?:/.test(config.apiBaseUrl) &&
    milestone.photoUrl.startsWith(new URL(config.apiBaseUrl).origin + '/')
      ? ('anonymous' as const)
      : undefined
 
  return (
    <>
      <article
        className="overflow-hidden rounded-2xl border border-border bg-card shadow-sm"
        aria-label={milestone.title}
      >
        <div className={cn('grid', hasPhoto && 'md:grid-cols-[minmax(0,2fr)_minmax(0,3fr)]')}>
          {hasPhoto && (
            <button
              type="button"
              onClick={() => setPhotoOpen(true)}
              aria-label={t('milestones.page.enlarge', { title: milestone.title })}
              className="group relative min-w-0 bg-muted focus-visible:outline-2 focus-visible:-outline-offset-4 focus-visible:outline-primary"
            >
              <img
                crossOrigin={photoCrossOrigin}
                src={milestone.photoUrl!}
                alt={milestone.title}
                loading="lazy"
                onError={() => setFailedPhoto(milestone.photoUrl)}
                className="h-56 w-full object-cover md:h-full md:max-h-96 md:min-h-64"
              />
              <span className="absolute right-3 bottom-3 rounded-lg bg-card px-3 py-2 text-xs font-semibold text-ink shadow-sm">
                {t('milestones.page.viewPhoto')}
              </span>
            </button>
          )}
          <div className="flex min-w-0 flex-col p-5 sm:p-6">
            <div className="mb-3 flex flex-wrap items-center gap-2 text-xs">
              <time dateTime={milestone.achievedAt} className="font-medium text-ink-muted">
                {formatDateDisplay(milestone.achievedAt, i18n.language)}
              </time>
              <span className={cn('rounded-full px-2.5 py-1 font-semibold', meta.badgeClass)}>
                <span aria-hidden="true">{meta.emoji} </span>
                {t(`milestones.category.${milestone.category.toLowerCase()}`)}
              </span>
            </div>
            <h3 className="break-words font-display text-xl font-extrabold text-ink">
              {milestone.title}
            </h3>
            {baby && (
              <div className="mt-2 flex items-center gap-2 text-xs font-semibold text-ink-muted">
                <span
                  style={avatar?.style}
                  className={cn(
                    'flex h-6 w-6 shrink-0 items-center justify-center rounded-full text-[10px]',
                    avatar?.className,
                  )}
                >
                  {babyInitials(baby.name)}
                </span>
                {baby.name}
              </div>
            )}
            {milestone.description && (
              <p className="mt-3 whitespace-pre-wrap break-words text-sm leading-relaxed text-ink-muted">
                {milestone.description}
              </p>
            )}
            {milestone.photoUrl && !hasPhoto && (
              <p role="status" className="mt-3 text-sm text-ink-muted">
                {t('milestones.page.photoUnavailable')}
              </p>
            )}
            <div className="mt-auto flex items-center justify-between gap-3 pt-5">
              <Button
                variant="ghost"
                disabled={deletion.isPending}
                onClick={onEdit}
                aria-label={t('milestones.edit.action', { title: milestone.title })}
                className="min-h-11 text-amber-800 dark:text-amber-300"
              >
                {t('milestones.page.edit')}
              </Button>
              <Button
                variant="ghost"
                size="icon"
                disabled={deletion.isPending}
                onClick={() => setConfirm(true)}
                aria-label={t('milestones.delete.action', { title: milestone.title })}
                className="h-11 w-11 text-ink-muted hover:text-destructive"
              >
                <TrashIcon className="h-4 w-4" />
              </Button>
            </div>
          </div>
        </div>
      </article>
      <Dialog open={photoOpen} onOpenChange={setPhotoOpen}>
        <DialogContent className="max-h-[90dvh] overflow-y-auto sm:max-w-4xl">
          <DialogHeader>
            <DialogTitle className="break-words pr-6">{milestone.title}</DialogTitle>
            <DialogDescription>
              {formatDateDisplay(milestone.achievedAt, i18n.language)}
            </DialogDescription>
          </DialogHeader>
          {milestone.photoUrl && (
            <img
              crossOrigin={photoCrossOrigin}
              src={milestone.photoUrl}
              alt={milestone.title}
              className="max-h-[65dvh] w-full rounded-xl object-contain"
            />
          )}
        </DialogContent>
      </Dialog>
      <AlertDialog open={confirm} onOpenChange={(open) => !deletion.isPending && setConfirm(open)}>
        <AlertDialogContent>
          <AlertDialogHeader>
            <AlertDialogTitle>{t('milestones.delete.confirmTitle')}</AlertDialogTitle>
            <AlertDialogDescription>
              {t('milestones.delete.confirmDescription', { title: milestone.title })}
            </AlertDialogDescription>
          </AlertDialogHeader>
          <AlertDialogFooter>
            <AlertDialogCancel disabled={deletion.isPending}>
              {t('milestones.delete.confirmDismiss')}
            </AlertDialogCancel>
            <AlertDialogAction
              variant="destructive"
              disabled={deletion.isPending}
              onClick={async (event) => {
                event.preventDefault()
                Iif (deletion.isPending) return
                try {
                  await deletion.mutateAsync(milestone.id)
                  setConfirm(false)
                  toast.success(t('milestones.delete.successToast'))
                } catch {
                  toast.error(t('milestones.delete.genericError'))
                }
              }}
            >
              {t(deletion.isPending ? 'common.loading' : 'milestones.delete.confirmAction')}
            </AlertDialogAction>
          </AlertDialogFooter>
        </AlertDialogContent>
      </AlertDialog>
    </>
  )
}