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

82.6% Statements 19/23
84.61% Branches 22/26
66.66% Functions 4/6
90% Lines 18/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                                                                                                  289x 289x 289x 289x 289x 289x 4x 4x 3x   289x 56x 13x 13x   43x 56x     289x                       18x 18x                                                                                                                                                                                      
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 { SyringeIcon } from '@/shared/icons/syringe-icon'
 
interface Props {
  open: boolean
  onOpenChange: (open: boolean) => void
  title: string
  baby?: Baby
  vaccine?: string
  dose?: string
  date?: string
  stage: string
  dirty: boolean
  busy: boolean
  progress?: ReactNode
  children: ReactNode
  footer: (close: () => void) => ReactNode
}
 
export function VaccineDialogLayout({
  open,
  onOpenChange,
  title,
  baby,
  vaccine,
  dose,
  date,
  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
    if (dirty) setDiscard(true)
    else onOpenChange(false)
  }
  useEffect(() => {
    if (!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 className="flex min-w-0 flex-col bg-emerald-900 px-5 py-4 text-white md:px-7 md:py-8">
            <div className="mb-8 hidden items-center gap-3 md:flex">
              <SyringeIcon className="size-6" />
              <span className="text-sm font-bold">{t('vaccines.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 ?? '') || <SyringeIcon className="size-5" />
                )}
              </span>
              <div className="min-w-0">
                <p className="text-xs text-white/80">{t('vaccines.editor.child')}</p>
                <p className="truncate font-bold">{baby?.name ?? t('vaccines.editor.chooseChild')}</p>
              </div>
            </div>
            <div className="mt-3 md:mt-8">
              <p className="hidden text-xs font-bold uppercase tracking-wider text-white/80 md:block">
                {t('vaccines.editor.vaccine')}
              </p>
              <p className="font-display text-lg font-extrabold md:mt-3 md:break-words md:text-3xl">
                {vaccine || t('vaccines.editor.preview')}
              </p>
              {dose && <p className="mt-1 text-sm text-white/80">{dose}</p>}
              {date && <p className="mt-3 hidden 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 md:block">
              <p className="font-semibold">{t('vaccines.editor.railTitle')}</p>
              <p className="mt-2 text-sm leading-relaxed text-white/80">{t('vaccines.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('vaccines.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('vaccines.editor.discardTitle')}</AlertDialogTitle>
            <AlertDialogDescription>{t('vaccines.editor.discardHint')}</AlertDialogDescription>
          </AlertDialogHeader>
          <AlertDialogFooter>
            <AlertDialogCancel>{t('vaccines.editor.keepEditing')}</AlertDialogCancel>
            <AlertDialogAction onClick={() => onOpenChange(false)}>
              {t('vaccines.editor.discard')}
            </AlertDialogAction>
          </AlertDialogFooter>
        </AlertDialogContent>
      </AlertDialog>
    </>
  )
}