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 | 2x 2x 55x 43x 43x 43x 43x 127x 43x 43x 43x 43x 43x 76x 76x 45x 48x 43x 43x 48x 29x 43x 29x 43x 43x 1x 1x 43x 1x 1x 2x 1x 43x 43x 2x 1x 1x 12x 174x 1x 174x 1x 1x | import { useSearchDestination } from '@/shared/stores/searchDestination.store'
import { useState } from 'react'
import { useQueryClient } from '@tanstack/react-query'
import { useTranslation } from 'react-i18next'
import { Navigate } from 'react-router-dom'
import { Button } from '@/components/ui/button'
import { useDebouncedValue } from '@/hooks/useDebouncedValue'
import { usePagedList } from '@/hooks/usePagedList'
import { LoadMoreButton } from '@/shared/components/LoadMoreButton'
import { useSelectedBabyStore } from '@/shared/stores/selectedBaby.store'
import { SearchInput } from '@/shared/components/SearchInput'
import { SparkleIcon } from '@/shared/icons/sparkle-icon'
import { useAllBabiesMilestones } from '../api/milestones.hooks'
import type { MilestoneCategory } from '../api/milestones.schemas'
import { AddMilestoneDialog } from '../components/AddMilestoneDialog'
import {
MILESTONE_SUGGESTIONS,
type MilestoneSuggestion,
} from '../components/milestone-suggestions'
import { MILESTONE_CATEGORY_META } from '../components/category-meta'
import { MilestoneTimeline } from '../components/MilestoneTimeline'
import { MilestoneTimelineSkeleton } from '../components/MilestoneTimelineSkeleton'
const CATEGORIES: (MilestoneCategory | 'ALL')[] = [
'ALL',
'MOTOR',
'LANGUAGE',
'SOCIAL',
'COGNITIVE',
'OTHER',
]
const normalize = (value: string) =>
value
.normalize('NFD')
.replace(/[\u0300-\u036f]/g, '')
.trim()
.replace(/\s+/g, ' ')
.toLowerCase()
export function MilestonesRoute() {
const { t } = useTranslation()
const all = useAllBabiesMilestones()
const client = useQueryClient()
const [activeCategory, setActiveCategory] = useState<MilestoneCategory | 'ALL'>('ALL')
const babyFilter = useSelectedBabyStore((state) => state.selectedBabyId)
const [isAddOpen, setIsAddOpen] = useState(false)
const [suggestion, setSuggestion] = useState<MilestoneSuggestion | null>(null)
const [search, setSearch] = useState('')
const debouncedSearch = useDebouncedValue(search)
const normalizedSearch = normalize(debouncedSearch)
const babies = all.babies.filter((baby) => !babyFilter || baby.id === babyFilter)
const entries = all.perBaby.filter((entry) => !babyFilter || entry.baby.id === babyFilter)
const isPending = all.babies.length ? entries.some((entry) => entry.isPending) : all.isPending
const isError = all.babies.length ? entries.some((entry) => entry.isError) : all.isError
// Preserve successfully loaded memories when another child's request fails.
const destination = useSearchDestination('/milestones')
const items = entries
.flatMap((entry) => entry.items)
.filter((item) => !destination || destination.keys.includes(`milestone:${item.id}`))
const filteredItems = items
.filter(
(item) =>
(activeCategory === 'ALL' || item.category === activeCategory) &&
(!normalizedSearch ||
normalize([item.title, item.description ?? ''].join(' ')).includes(normalizedSearch)),
)
.sort((a, b) => b.achievedAt.localeCompare(a.achievedAt))
const { visibleItems, hasMore, loadMore } = usePagedList(
filteredItems,
`${normalizedSearch}|${babyFilter}|${activeCategory}`,
)
const openAdd = (from: MilestoneSuggestion | null) => {
setSuggestion(from)
setIsAddOpen(true)
}
const retry = () => {
Iif (!all.babies.length) void client.invalidateQueries({ queryKey: ['babies'] })
entries
.filter((entry) => entry.isError)
.forEach(
(entry) =>
void client.invalidateQueries({ queryKey: ['babies', entry.baby.id, 'milestones'] }),
)
}
Iif (all.isEmpty) return <Navigate to="/dashboard" replace />
return (
<div>
<div className="mb-6 flex flex-col justify-between gap-4 sm:flex-row sm:items-end">
<div>
<h1 className="font-display text-3xl font-extrabold text-ink">{t('nav.milestones')}</h1>
<p className="mt-1 text-sm text-ink-muted">{t('milestones.page.intro')}</p>
</div>
{(items.length > 0 || isPending || isError) && (
<Button
size="cta"
onClick={() => openAdd(null)}
className="rounded-2xl shadow-lg shadow-emerald-900/20"
>
{t('milestones.action')}
</Button>
)}
</div>
<AddMilestoneDialog
babies={babies.length ? babies : undefined}
open={isAddOpen}
onOpenChange={setIsAddOpen}
suggestion={
suggestion ? { title: t(suggestion.titleKey), category: suggestion.category } : undefined
}
/>
{isError && (
<div role="alert" className="mb-5 rounded-2xl border border-border bg-card p-5">
<p>
{t('milestones.genericError')}{' '}
{entries
.filter((entry) => entry.isError)
.map((entry) => entry.baby.name)
.join(', ')}
</p>
<button type="button" onClick={retry} className="min-h-11 font-semibold text-primary">
{t('nav.shell.retry')}
</button>
</div>
)}
{isPending && <MilestoneTimelineSkeleton />}
{!isPending && !isError && !items.length && (
<section className="grid gap-6 rounded-3xl border border-amber-600/20 bg-card p-6 sm:p-8 lg:grid-cols-[minmax(0,3fr)_minmax(0,2fr)] lg:gap-12">
<div>
<span className="mb-5 inline-flex h-14 w-14 items-center justify-center rounded-2xl bg-amber-50 text-amber-800 dark:bg-amber-950/40 dark:text-amber-300">
<SparkleIcon className="h-7 w-7" />
</span>
<h2 className="font-display text-2xl font-extrabold text-ink">
{t('milestones.page.emptyTitle')}
</h2>
<p className="mt-3 max-w-lg text-sm leading-relaxed text-ink-muted">
{t('milestones.page.emptyDescription')}
</p>
<Button size="cta" onClick={() => openAdd(null)} className="mt-6 rounded-xl">
{t('milestones.empty.cta')}
</Button>
</div>
<div className="rounded-2xl bg-amber-50/60 p-5 dark:bg-amber-950/20">
<h3 className="font-semibold text-ink">{t('milestones.page.ideas')}</h3>
<p className="mt-1 text-sm text-ink-muted">{t('milestones.page.ideasHint')}</p>
<div className="mt-4 space-y-2">
{MILESTONE_SUGGESTIONS.slice(0, 3).map((item) => (
<button
key={item.titleKey}
type="button"
onClick={() => openAdd(item)}
className="flex min-h-11 w-full items-center gap-3 rounded-xl bg-card px-4 py-3 text-left text-sm font-semibold text-ink hover:bg-muted"
>
<span aria-hidden="true">{MILESTONE_CATEGORY_META[item.category].emoji}</span>
{t(item.titleKey)}
</button>
))}
</div>
</div>
</section>
)}
{items.length > 0 && (
<>
<div className="mb-6 space-y-4">
<SearchInput
id="milestone-search"
label={t('milestones.page.search')}
placeholder={t('milestones.page.search')}
value={search}
onChange={setSearch}
clearLabel={t('vaccines.searchUi.clear')}
inputClassName="h-12 rounded-xl"
className="w-full sm:max-w-lg"
/>
<div
className="flex flex-wrap gap-2"
role="group"
aria-label={t('milestones.categoryFilter.groupLabel')}
>
{CATEGORIES.map((category) => (
<button
key={category}
type="button"
aria-pressed={activeCategory === category}
onClick={() => setActiveCategory(category)}
className={`min-h-11 rounded-xl px-4 py-2 text-sm font-semibold transition-colors ${activeCategory === category ? 'bg-amber-800 text-white dark:bg-amber-700' : 'bg-card text-ink-muted hover:bg-muted'}`}
>
{category === 'ALL'
? t('milestones.filterAll')
: t(`milestones.category.${category.toLowerCase()}`)}{' '}
({items.filter((item) => category === 'ALL' || item.category === category).length}
)
</button>
))}
</div>
<p role="status" className="text-sm text-ink-muted">
{t('milestones.page.results', { count: filteredItems.length })}
{(isError || isPending) && ` ยท ${t('milestones.page.partial')}`}
</p>
</div>
{!filteredItems.length ? (
<div className="rounded-2xl bg-card p-8 text-center">
<p className="text-ink-muted">
{t(normalizedSearch ? 'milestones.page.noSearch' : 'milestones.page.noCategory')}
</p>
<Button
variant="ghost"
className="mt-3 min-h-11"
onClick={() => {
setSearch('')
setActiveCategory('ALL')
}}
>
{t('milestones.page.reset')}
</Button>
</div>
) : (
<>
<MilestoneTimeline items={visibleItems} babies={babies} />
{hasMore && <LoadMoreButton onClick={loadMore} label={t('common.loadMore')} />}
</>
)}
</>
)}
</div>
)
}
|