import React, { useMemo, useRef, useState } from "react"; import { View, ScrollView, TouchableOpacity } from "react-native"; import { Text } from "~/components/ui/text"; import ScreenWrapper from "~/components/ui/ScreenWrapper"; import BackButton from "~/components/ui/backButton"; import { Input } from "~/components/ui/input"; import ModalToast from "~/components/ui/toast"; import { useRecipientsStore } from "~/lib/stores"; import { BellIcon, MessageCircle, LucideSlidersHorizontal, } from "lucide-react-native"; import BottomSheet from "~/components/ui/bottomSheet"; const getInitials = (name: string) => { return name .split(" ") .map((word) => word.charAt(0).toUpperCase()) .slice(0, 2) .join(""); }; export default function SendNotificationScreen() { const { recipients } = useRecipientsStore(); const [searchQuery, setSearchQuery] = useState(""); const [toastVisible, setToastVisible] = useState(false); const [toastTitle, setToastTitle] = useState(""); const [toastDescription, setToastDescription] = useState( undefined ); const [toastVariant, setToastVariant] = useState< "success" | "error" | "warning" | "info" >("info"); const toastTimeoutRef = useRef | null>(null); const [sheetVisible, setSheetVisible] = useState(false); const [selectedRecipient, setSelectedRecipient] = useState(null); const showToast = ( title: string, description?: string, variant: "success" | "error" | "warning" | "info" = "info" ) => { if (toastTimeoutRef.current) { clearTimeout(toastTimeoutRef.current); } setToastTitle(title); setToastDescription(description); setToastVariant(variant); setToastVisible(true); toastTimeoutRef.current = setTimeout(() => { setToastVisible(false); toastTimeoutRef.current = null; }, 2500); }; const normalizedSearch = searchQuery.trim().toLowerCase(); const filteredRecipients = useMemo(() => { const base = recipients || []; const bySearch = !normalizedSearch ? base : base.filter((recipient) => { const name = recipient.fullName.toLowerCase(); const phone = recipient.phoneNumber.toLowerCase(); return ( name.includes(normalizedSearch) || phone.includes(normalizedSearch) ); }); return bySearch; }, [recipients, normalizedSearch]); const handleSendInApp = (name: string) => { showToast( "In-app notification", `We'd notify ${name} in the app (stub).`, "success" ); }; const handleSendWhatsApp = (name: string) => { showToast( "WhatsApp notification", `We'd open WhatsApp to message ${name} (stub).`, "info" ); }; return ( Send notification Pick who you want to notify about upcoming or recent payments. } /> {filteredRecipients.length === 0 && ( No clients found. Add recipients first to send notifications. )} {filteredRecipients.length > 0 && ( {filteredRecipients.map((recipient, index) => { const initials = getInitials(recipient.fullName); const lowerName = recipient.fullName.toLowerCase(); const isBusiness = lowerName.includes("ltd") || lowerName.includes("plc") || lowerName.includes("inc") || lowerName.includes("company"); const clientType = isBusiness ? "Business" : "Individual"; const hasSchedule = index % 2 === 1; return ( {initials} {recipient.fullName} {clientType} {recipient.phoneNumber} Notification context {hasSchedule ? "Upcoming scheduled payment this week" : "One-off payment reminder"} {hasSchedule && ( Has schedules )} { setSelectedRecipient(recipient); setSheetVisible(true); }} > Send notification ); })} )} { setSheetVisible(false); setSelectedRecipient(null); }} maxHeightRatio={0.4} > {selectedRecipient && ( Choose notification type {`Who: ${selectedRecipient.fullName}`} { handleSendInApp(selectedRecipient.fullName); setSheetVisible(false); setSelectedRecipient(null); }} > In-app notification Show inside Amba when they open the app. { handleSendWhatsApp(selectedRecipient.fullName); setSheetVisible(false); setSelectedRecipient(null); }} > WhatsApp message Open WhatsApp to send them a quick update. )} ); }