diff --git a/CLAUDE.md b/CLAUDE.md index e71847a..435a951 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -124,11 +124,12 @@ const { theme, toggle, setMode } = useTheme(); ### Audit (2026-05-10) -Current status - needs migration to database: -- `tia_medicines` → `/api/medicines` -- `tia_allergies` → `/api/allergies` -- `tia_visits` → `/api/visits` -- `tia_illnesses` → `/api/illnesses` +All data now consistently uses database: +- ✅ Medicines → `/api/medicines` +- ✅ Allergies → `/api/allergies` +- ✅ Doctor Visits → `/api/visits` +- ✅ Illness Log → `/api/illnesses` +- ✅ Chat Sessions → `/api/chat` ## R2 Storage (Cloudflare) diff --git a/src/app/FamilyProvider.tsx b/src/app/FamilyProvider.tsx new file mode 100644 index 0000000..298f2b2 --- /dev/null +++ b/src/app/FamilyProvider.tsx @@ -0,0 +1,102 @@ +"use client"; + +import { useState, useEffect, createContext, useContext } from "react"; +import { ReactNode } from "react"; + +interface Child { + id: string; + name: string; + birthDate: string; + sex: string; +} + +interface FamilyContextType { + familyId: string | null; + childId: string | null; + child: Child | null; + children: Child[]; + loading: boolean; + tier: "free" | "pro"; + memberCount: number; +} + +const FamilyContext = createContext({ + familyId: null, + childId: null, + child: null, + children: [], + loading: true, + tier: "free", + memberCount: 2, +}); + +export function useFamily() { + return useContext(FamilyContext); +} + +export function FamilyProvider({ children: providerChildren }: { children: ReactNode }) { + const [familyId, setFamilyId] = useState(null); + const [childId, setChildId] = useState(null); + const [child, setChild] = useState(null); + const [children, setChildren] = useState([]); + const [loading, setLoading] = useState(true); + const [tier, setTier] = useState<"free" | "pro">("free"); + const [memberCount, setMemberCount] = useState(2); + + useEffect(() => { + async function fetchFamilyData() { + try { + const res = await fetch("/api/children?familyId=default"); + const data = await res.json(); + + if (data.children?.length > 0) { + const childList = data.children.map((c: any) => ({ + id: c.id, + name: c.name, + birthDate: c.birthDate, + sex: c.sex, + })); + + setChildren(childList); + setChild(childList[0]); + setChildId(childList[0].id); + } + + setFamilyId("default"); + setTier("free"); + setMemberCount(2); + } catch (err) { + console.error("Failed to fetch family:", err); + } finally { + setLoading(false); + } + } + + fetchFamilyData(); + }, []); + + return ( + + {providerChildren} + + ); +} + +// Note: Call useFamily() in any page to get: +// - familyId: The current family ID +// - childId: The selected child ID +// - child: The selected child object +// - children: List of all children +// - loading: Whether data is loading +// - tier: "free" or "pro" +// - memberCount: Number of family members \ No newline at end of file diff --git a/src/app/layout.tsx b/src/app/layout.tsx index b548b95..c79e4f3 100644 --- a/src/app/layout.tsx +++ b/src/app/layout.tsx @@ -1,6 +1,7 @@ import type { Metadata } from "next"; import { Geist, Geist_Mono } from "next/font/google"; import { ThemeProvider } from "./ThemeProvider"; +import { FamilyProvider } from "./FamilyProvider"; import "./globals.css"; const geistSans = Geist({ @@ -31,7 +32,9 @@ export default function RootLayout({ return ( - {children} + + {children} + ); diff --git a/src/app/medical/page.tsx b/src/app/medical/page.tsx index 38459c3..2cf488d 100644 --- a/src/app/medical/page.tsx +++ b/src/app/medical/page.tsx @@ -1,6 +1,7 @@ "use client"; import { useState, useEffect } from "react"; +import { useFamily } from "../FamilyProvider"; interface Medicine { id: string; @@ -70,9 +71,9 @@ function calculateDueDate(birthDate: string, weeks: number): string { } export default function MedicalPage() { - const [childId] = useState("5ad3b16a-1e0d-45ab-bc91-038397d75d0a"); const [vaccinations, setVaccinations] = useState([]); const [loading, setLoading] = useState(true); + const { childId: sessionChildId, child, loading: loadingChild } = useFamily(); const [tab, setTab] = useState<"vaccinations" | "medicine" | "allergies" | "visits" | "illness">("vaccinations"); const [showAddDate, setShowAddDate] = useState(null); const [givenDate, setGivenDate] = useState(""); @@ -357,7 +358,8 @@ export default function MedicalPage() { setShowAddIllness(false); }; - const birthDate = "2024-01-15"; + const childId = sessionChildId || "default"; + const birthDate = child?.birthDate || "2024-01-15"; // Common supplements for babies const SUPPLEMENTS = [ diff --git a/src/app/page.tsx b/src/app/page.tsx index 99345a0..857868e 100644 --- a/src/app/page.tsx +++ b/src/app/page.tsx @@ -3,6 +3,7 @@ import { useState, useEffect } from "react"; import Link from "next/link"; import { useTheme } from "./ThemeProvider"; +import { useFamily } from "./FamilyProvider"; const OFFLINE_QUEUE_KEY = "tia_offline_queue"; @@ -133,14 +134,18 @@ export default function HomePage() { const [aiInput, setAiInput] = useState(""); const [aiChats, setAiChats] = useState([]); const [aiLoading, setAiLoading] = useState(false); - const [childId] = useState("5ad3b16a-1e0d-45ab-bc91-038397d75d0a"); const [pendingCount, setPendingCount] = useState(0); const [lastLogs, setLastLogs] = useState([]); const { theme, toggle: toggleTheme } = useTheme(); + const { childId, child, loading } = useFamily(); - const child = { name: "Baby Tia", birthDate: "2024-01-15" }; + if (loading) { + return
Loading...
; + } - // Load latest session on mount + if (!childId) { + return
No child found. Add a child in Family settings.
; + } useEffect(() => { getSessions(childId).then(sessions => { if (sessions.length > 0) { @@ -235,13 +240,13 @@ export default function HomePage() {

{getGreeting()} 👋

-

How is {child.name} doing today?

+

How is {child?.name || "your baby"} doing today?

👶
-
{child.name}
{calculateAge(child.birthDate)}
+
{child?.name || "Baby"}
{calculateAge(child?.birthDate || "")}