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

69.56% Statements 16/23
76.47% Branches 26/34
66.66% Functions 4/6
80% Lines 16/20

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                                                                                                              151x 151x 151x 151x 151x 151x 1x 1x 1x   151x 34x       34x 34x     151x                       14x 14x                                                                                                                                                                                                                                                        
import { useEffect, useRef, useState, type ReactNode } from 'react'
import { useTranslation } from 'react-i18next'
import { Dialog, DialogContent, DialogTitle, DialogDescription } from '@/components/ui/dialog'
import {
  AlertDialog,
  AlertDialogContent,
  AlertDialogHeader,
  AlertDialogTitle,
  AlertDialogDescription,
  AlertDialogFooter,
  AlertDialogCancel,
  AlertDialogAction,
} from '@/components/ui/alert-dialog'
import type { Baby } from '@/features/babies/api/babies.schemas'
import { babyAvatarAppearance, babyInitials } from '@/shared/utils/babyAvatarColor'
import { CloseIcon } from '@/shared/icons/close-icon'
import { SparkleIcon } from '@/shared/icons/sparkle-icon'
 
interface Props {
  open: boolean
  onOpenChange: (open: boolean) => void
  title: string
  baby?: Baby
  moment?: string
  category?: string
  categoryClassName?: string
  photo?: string
  date?: string
  description?: string
  stage: string
  dirty: boolean
  busy: boolean
  progress?: ReactNode
  children: ReactNode
  footer: (close: () => void) => ReactNode
}
 
export function MilestoneDialogLayout({
  open,
  onOpenChange,
  title,
  baby,
  moment,
  category,
  categoryClassName,
  photo,
  date,
  description,
  stage,
  dirty,
  busy,
  progress,
  children,
  footer,
}: Props) {
  const { t } = useTranslation()
  const [discard, setDiscard] = useState(false)
  const heading = useRef<HTMLHeadingElement>(null)
  const content = useRef<HTMLDivElement>(null)
  const avatar = babyAvatarAppearance(baby?.id ?? '', baby?.avatarColor)
  const close = () => {
    Iif (busy) return
    Iif (dirty) setDiscard(true)
    else onOpenChange(false)
  }
  useEffect(() => {
    Iif (!open) {
      setDiscard(false)
      return
    }
    content.current?.scrollTo?.({ top: 0 })
    heading.current?.focus()
  }, [stage, open])
 
  return (
    <>
      <Dialog
        open={open}
        onOpenChange={(value) => {
          if (!value) close()
        }}
      >
        <DialogContent
          showCloseButton={false}
          className="grid h-[min(680px,calc(100dvh-32px))] grid-rows-[auto_minmax(0,1fr)] gap-0 overflow-hidden p-0 sm:max-w-[900px] md:grid-cols-[280px_minmax(0,1fr)] md:grid-rows-1"
          onOpenAutoFocus={(event) => {
            event.preventDefault()
            heading.current?.focus()
          }}
        >
          <aside
            aria-label={t('milestones.editor.railTitle')}
            className="flex min-w-0 flex-col bg-amber-950 px-5 py-4 text-white md:px-7 md:py-8"
          >
            <div className="mb-8 hidden items-center gap-3 md:flex">
              <SparkleIcon className="size-6" />
              <span className="text-sm font-bold">{t('milestones.editor.record')}</span>
            </div>
            <div className="flex items-center gap-3 pr-9 md:pr-0">
              <span
                className={`flex size-11 shrink-0 items-center justify-center overflow-hidden rounded-full text-lg font-bold ${avatar.className}`}
                style={avatar.style}
              >
                {baby?.avatarUrl ? (
                  <img src={baby.avatarUrl} alt="" className="size-full object-cover" />
                ) : (
                  babyInitials(baby?.name ?? '') || <SparkleIcon className="size-5" />
                )}
              </span>
              <div className="min-w-0">
                <p className="text-xs text-white/80">{t('milestones.editor.child')}</p>
                <p className="truncate font-bold">
                  {baby?.name ?? t('milestones.editor.chooseChild')}
                </p>
              </div>
            </div>
            <div className="mt-3 md:mt-6">
              {photo && (
                <img
                  src={photo}
                  alt=""
                  className="mb-4 hidden h-36 w-full rounded-xl object-cover md:block"
                />
              )}
              <p className="hidden text-xs font-bold uppercase tracking-wider text-white/80 md:block">
                {t('milestones.editor.moment')}
              </p>
              <p className="font-display text-lg font-extrabold break-words line-clamp-2 md:mt-3 md:text-2xl">
                {moment || t('milestones.editor.preview')}
              </p>
              {category && (
                <p
                  className={`mt-2 inline-flex rounded-full px-2.5 py-1 text-xs font-semibold ${categoryClassName ?? 'bg-white/10 text-white'}`}
                >
                  {category}
                </p>
              )}
              {description && (
                <p className="mt-3 hidden break-words text-sm text-white/80 md:line-clamp-2">
                  {description}
                </p>
              )}
              {date && (
                <p className="mt-3 hidden break-words font-sans tabular-nums text-sm text-white/80 md:block">
                  {date}
                </p>
              )}
            </div>
            <div
              className={`mt-auto hidden border-t border-white/20 pt-6 ${photo ? '' : 'md:block'}`}
            >
              <p className="font-semibold">{t('milestones.editor.railTitle')}</p>
              <p className="mt-2 text-sm leading-relaxed text-white/80">
                {t('milestones.editor.railHint')}
              </p>
            </div>
          </aside>
          <div className="flex min-h-0 min-w-0 flex-col">
            <header className="shrink-0 border-b border-border px-5 py-5 md:px-8 md:py-6">
              <button
                type="button"
                onClick={close}
                disabled={busy}
                aria-label={t('common.close')}
                className="absolute top-2 right-2 flex size-11 items-center justify-center rounded-full bg-background text-ink-muted hover:bg-muted disabled:opacity-50"
              >
                <CloseIcon className="size-4" />
              </button>
              <DialogTitle
                ref={heading}
                tabIndex={-1}
                className="pr-8 font-display text-xl font-extrabold outline-none md:text-2xl"
              >
                {title}
              </DialogTitle>
              <DialogDescription className="mt-1">
                {t('milestones.editor.subtitle')}
              </DialogDescription>
              {progress && <div className="mt-4">{progress}</div>}
            </header>
            <div
              ref={content}
              className="min-h-0 flex-1 overflow-y-auto overscroll-contain px-5 py-5 md:px-8 md:py-6"
            >
              <fieldset disabled={busy} className="min-w-0 disabled:opacity-60">
                {children}
              </fieldset>
            </div>
            <footer className="shrink-0 border-t border-border bg-background px-5 py-4 md:px-8">
              <div className="flex items-center justify-between gap-3">{footer(close)}</div>
            </footer>
          </div>
        </DialogContent>
      </Dialog>
      <AlertDialog open={discard} onOpenChange={setDiscard}>
        <AlertDialogContent>
          <AlertDialogHeader>
            <AlertDialogTitle>{t('milestones.editor.discardTitle')}</AlertDialogTitle>
            <AlertDialogDescription>{t('milestones.editor.discardHint')}</AlertDialogDescription>
          </AlertDialogHeader>
          <AlertDialogFooter>
            <AlertDialogCancel>{t('milestones.editor.keepEditing')}</AlertDialogCancel>
            <AlertDialogAction onClick={() => onOpenChange(false)}>
              {t('milestones.editor.discard')}
            </AlertDialogAction>
          </AlertDialogFooter>
        </AlertDialogContent>
      </AlertDialog>
    </>
  )
}