All files / src/features/search/components SearchDialog.tsx

76.31% Statements 29/38
80.64% Branches 25/31
72.72% Functions 8/11
78.78% Lines 26/33

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                                  13x   2x         105x 105x 329x 105x 105x 105x 105x 12x 12x           105x       12x 12x         79x 79x               1x 78x     78x 31x 31x 1x   1x         1x 30x                                                                                       1x                  
import { SearchScopeSelector } from './SearchScopeSelector'
import { useEffect, useState } from 'react'
import { useTranslation } from 'react-i18next'
import { Dialog, DialogContent, DialogTitle } from '@/components/ui/dialog'
import { useBabies } from '@/features/babies/api/babies.hooks'
import { SearchInput } from '@/shared/components/SearchInput'
import { useSelectedBabyStore } from '@/shared/stores/selectedBaby.store'
import { SearchResultsList } from './SearchResultsList'
import { X } from 'lucide-react'
 
export function SearchDialog({
  open,
  onOpenChange,
}: {
  open: boolean
  onOpenChange: (open: boolean) => void
}) {
  return (
    <Dialog open={open} onOpenChange={onOpenChange}>
      {open && <SearchPanel onClose={() => onOpenChange(false)} />}
    </Dialog>
  )
}
function SearchPanel({ onClose }: { onClose: () => void }) {
  const { t } = useTranslation()
  const babies = useBabies()
  const selected = useSelectedBabyStore((state) => state.selectedBabyId)
  const [scope, setScope] = useState<string | null>(selected)
  const [query, setQuery] = useState('')
  const [viewportHeight, setViewportHeight] = useState<number | undefined>()
  useEffect(() => {
    const viewport = window.visualViewport
    Eif (!viewport) return
    const update = () => setViewportHeight(viewport.height)
    update()
    viewport.addEventListener('resize', update)
    return () => viewport.removeEventListener('resize', update)
  }, [])
  return (
    <DialogContent
      style={viewportHeight ? { maxHeight: Math.max(180, viewportHeight - 24) } : undefined}
      onOpenAutoFocus={(event) => {
        event.preventDefault()
        document.getElementById('app-search')?.focus()
      }}
      showCloseButton={false}
      aria-describedby={undefined}
      onKeyDown={(event) => {
        const target = event.target as HTMLElement
        if (
          (target.tagName !== 'INPUT' && !target.matches('a[data-search-result]')) ||
          event.defaultPrevented ||
          event.altKey ||
          event.ctrlKey ||
          event.metaKey ||
          event.nativeEvent.isComposing
        )
          return
        const links = [
          ...event.currentTarget.querySelectorAll<HTMLAnchorElement>('a[data-search-result]'),
        ]
        if (!links.length) return
        const index = links.indexOf(target as HTMLAnchorElement)
        if (event.key === 'ArrowDown' || event.key === 'ArrowUp') {
          event.preventDefault()
          const next =
            event.key === 'ArrowDown'
              ? (index + 1) % links.length
              : index < 0
                ? links.length - 1
                : (index - 1 + links.length) % links.length
          links[next]?.focus()
        } else Iif (event.key === 'Enter' && target.tagName === 'INPUT') {
          event.preventDefault()
          links[0]?.click()
        }
      }}
      className="top-3 flex h-[min(42rem,calc(100dvh-1.5rem))] w-[calc(100%-1.5rem)] max-w-none translate-y-0 flex-col gap-0 overflow-hidden rounded-2xl p-0 sm:top-[8%] sm:h-[min(42rem,84dvh)] sm:max-w-2xl"
    >
      <div className="shrink-0 border-b border-border p-4 sm:p-5">
        <div className="mb-3 flex items-center justify-between gap-3">
          <DialogTitle className="font-display text-xl font-extrabold">
            {t('search.title')}
          </DialogTitle>
          <button
            type="button"
            onClick={onClose}
            aria-label={t('search.ui.close')}
            className="flex size-11 items-center justify-center rounded-xl text-ink-muted hover:bg-muted"
          >
            <X className="size-5" />
          </button>
        </div>
        <SearchInput
          id="app-search"
          label={t('search.title')}
          value={query}
          onChange={setQuery}
          placeholder={t('search.placeholder')}
          clearLabel={t('search.ui.clear')}
          inputClassName="h-12 rounded-xl"
        />
        <SearchScopeSelector
          babies={babies.data ?? []}
          value={scope}
          onChange={setScope}
          loading={babies.isPending}
          error={babies.isError}
          onRetry={() => {
            void babies.refetch()
          }}
        />
      </div>
      <SearchResultsList
        query={query}
        scope={scope}
        onExpandScope={() => setScope(null)}
        onNavigate={onClose}
      />
      <p className="hidden shrink-0 border-t border-border px-5 py-3 text-xs text-ink-muted sm:block">
        {t('search.ui.keyboard')}
      </p>
    </DialogContent>
  )
}