diff --git a/src/app/page.tsx b/src/app/page.tsx index 8a23564..59c3a42 100644 --- a/src/app/page.tsx +++ b/src/app/page.tsx @@ -152,17 +152,16 @@ function LogModal({ type, childId, onClose }: LogModalProps) { ); } -// Helper to calculate age function calculateAge(birthDate: string) { const birth = new Date(birthDate); const now = new Date(); - const years = now.getFullYear() - birth.getFullYear(); - const months = now.getMonth() - birth.getMonth(); const days = Math.floor((now.getTime() - birth.getTime()) / (1000 * 60 * 60 * 24)); + const months = Math.floor(days / 30); + const years = Math.floor(months / 12); if (years > 0) return `${years} year${years > 1 ? "s" : ""} old`; if (months > 0) return `${months} month${months > 1 ? "s" : ""} old`; - return `${days} days old`; + return `${days} day${days > 1 ? "s" : ""} old`; } function getGreeting() { @@ -174,6 +173,10 @@ function getGreeting() { export default function HomePage() { const [modalType, setModalType] = useState<"feed" | "diaper" | "sleep" | null>(null); + const [aiOpen, setAiOpen] = useState(false); + const [aiInput, setAiInput] = useState(""); + const [aiReply, setAiReply] = useState(""); + const [aiLoading, setAiLoading] = useState(false); const [childId] = useState("5ad3b16a-1e0d-45ab-bc91-038397d75d0a"); const [pendingCount, setPendingCount] = useState(0); const [lastLogs, setLastLogs] = useState([]); @@ -212,23 +215,35 @@ export default function HomePage() { }, []); useEffect(() => { - // Fetch recent logs Promise.all([ fetch(`/api/logs?type=feed&childId=${childId}&limit=1`).then(r => r.json()), fetch(`/api/logs?type=sleep&childId=${childId}&limit=1`).then(r => r.json()), fetch(`/api/logs?type=diaper&childId=${childId}&limit=1`).then(r => r.json()), ]).then(([feed, sleep, diaper]) => { - setLastLogs([ - feed.entries?.[0], - sleep.entries?.[0], - diaper.entries?.[0], - ].filter(Boolean)); + setLastLogs([feed.entries?.[0], sleep.entries?.[0], diaper.entries?.[0]].filter(Boolean)); }).catch(() => {}); }, [childId]); + const handleAiChat = async () => { + if (!aiInput.trim() || aiLoading) return; + setAiLoading(true); + try { + const res = await fetch("/api/ai", { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ messages: [{ role: "user", content: aiInput }] }), + }); + const data = await res.json(); + setAiReply(data.reply || "Sorry, I couldn't help with that."); + } catch { + setAiReply("Something went wrong. Try again."); + } + setAiLoading(false); + setAiInput(""); + }; + return (
- {/* Header */}
- {/* Welcome */}

{getGreeting()} 👋

How is {child.name} doing today?

- {/* Age Card */}
-
- 👶 -
+
👶
{child.name}
{calculateAge(child.birthDate)}
@@ -269,14 +272,12 @@ export default function HomePage() {
- {/* Pending Offline */} {pendingCount > 0 && (
{pendingCount} pending log{pendingCount > 1 ? "s" : ""} (will sync when online)
)} - {/* Quick Actions */}

Quick Log

@@ -292,14 +293,13 @@ export default function HomePage() { 👶 Diaper - - 💊 - Medical - +
- {/* Recent Activity */}

Recent Activity

@@ -329,6 +329,44 @@ export default function HomePage() {
setModalType(null)} /> + + {aiOpen && ( +
setAiOpen(false)}> +
e.stopPropagation()}> +
+

🤖 Ask Tia AI

+ +
+ +
+ {aiReply && ( +
+ {aiReply} +
+ )} +
+ +
+ setAiInput(e.target.value)} + onKeyDown={(e) => e.key === "Enter" && handleAiChat()} + placeholder="Ask a quick question..." + className="flex-1 p-2 border rounded-xl text-sm" + disabled={aiLoading} + /> + +
+
+
+ )}
); } \ No newline at end of file