import { Ionicons } from "@expo/vector-icons"; import { router } from "expo-router"; import { useMemo, useState } from "react"; import { Alert, Image, Pressable, Share, ScrollView, View } from "react-native"; import { useTranslation } from "react-i18next"; import { Badge } from "@/components/ui/Badge"; import { Button } from "@/components/ui/Button"; import { Card, Section } from "@/components/ui/Card"; import { DoseReminderPanel } from "@/components/ui/DoseReminderPanel"; import { EmptyState } from "@/components/ui/EmptyState"; import { Screen, TAB_BAR_INSET } from "@/components/ui/Screen"; import { TabTopBar } from "@/components/ui/TabTopBar"; import { Text } from "@/components/ui/Text"; import { routes } from "@/lib/routes"; import { useFamilyStore } from "@/lib/stores/familyStore"; import { usePrescriptionStore, type Prescription, type Reminder, } from "@/lib/stores/prescriptionStore"; import { colors, gutter } from "@/lib/theme"; const statusTone = { submitted: "neutral", under_review: "warn", approved: "brand", queried: "info", rejected: "danger", filled: "brand", } as const; function refillDays(rx: Prescription) { if (!rx.lastFilledAt) return null; const due = new Date(rx.lastFilledAt); due.setDate(due.getDate() + rx.daysSupply); return Math.ceil((due.getTime() - Date.now()) / (1000 * 60 * 60 * 24)); } function RxCareCard({ rx, reminder, expanded, onToggleExpand, }: { rx: Prescription; reminder?: Reminder; expanded: boolean; onToggleExpand: () => void; }) { const dueIn = refillDays(rx); const thumb = rx.imageUris[0]; return ( router.push(routes.rxDetail(rx.id))} style={{ padding: 14 }} > {thumb ? ( ) : ( )} {rx.medicineName} {rx.patientName} · {rx.medicines.length} medicine {rx.medicines.length === 1 ? "" : "s"} · {rx.daysSupply}d supply {reminder?.enabled ? ( ) : ( )} {dueIn !== null ? ( ) : ( )} {expanded ? "Hide setup" : reminder?.enabled ? "Edit reminder" : "Set reminder"} router.push(routes.rxCalendar(rx.id))} style={{ flex: 1, paddingVertical: 12, alignItems: "center", flexDirection: "row", justifyContent: "center", gap: 6, }} > Calendar {expanded ? ( ) : null} ); } export default function PrescriptionsTabScreen() { const { t } = useTranslation(); const prescriptions = usePrescriptionStore((s) => s.prescriptions); const reminders = usePrescriptionStore((s) => s.reminders); const members = useFamilyStore((s) => s.members); const inviteCode = useFamilyStore((s) => s.inviteCode); const acceptInvite = useFamilyStore((s) => s.acceptInvite); const removeMember = useFamilyStore((s) => s.removeMember); const setActiveMember = useFamilyStore((s) => s.setActiveMember); const activeMemberId = useFamilyStore((s) => s.activeMemberId); const [expandedId, setExpandedId] = useState(null); const reminderByRx = useMemo(() => { const map = new Map(); reminders.forEach((r) => map.set(r.prescriptionId, r)); return map; }, [reminders]); const activeReminderCount = reminders.filter((r) => r.enabled).length; const needingSetup = prescriptions.filter((p) => !reminderByRx.get(p.id)?.enabled).length; return ( {t("common.prescriptions")} Manage Rx, dose calendars, and family care shares Active reminders {activeReminderCount} Need setup {needingSetup}