import React, { useEffect, useRef, useState } from "react"; import { View, Text, ScrollView, Image, TouchableOpacity, Share, } from "react-native"; import { router } from "expo-router"; import ScreenWrapper from "~/components/ui/ScreenWrapper"; import BackButton from "~/components/ui/backButton"; import { Button } from "~/components/ui/button"; import { Icons } from "~/assets/icons"; import { ROUTES } from "~/lib/routes"; import { useTranslation } from "react-i18next"; import { getPointsState, type PointsState } from "~/lib/services/pointsService"; import * as Clipboard from "expo-clipboard"; import ModalToast from "~/components/ui/toast"; // Placeholder referral link const REFERRAL_LINK = "ambapay.com/frars"; export default function PointsScreen() { const { t } = useTranslation(); const [pointsState, setPointsState] = useState(null); 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); useEffect(() => { let cancelled = false; (async () => { try { const state = await getPointsState(); if (!cancelled) { setPointsState(state); } } catch (error) { if (__DEV__) { console.warn("[PointsScreen] Failed to load points state", error); } } })(); return () => { cancelled = true; }; }, []); 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); }; useEffect(() => { return () => { if (toastTimeoutRef.current) { clearTimeout(toastTimeoutRef.current); } }; }, []); const handleCopyLink = async () => { try { await Clipboard.setStringAsync(REFERRAL_LINK); showToast("Copied", "Referral link copied to clipboard", "success"); } catch (error) { showToast("Error", "Failed to copy referral link", "error"); } }; const handleShare = async () => { try { await Share.share({ message: `Use my Amba referral link: ${REFERRAL_LINK}`, }); } catch (error) { // Optional: only show error toast if share actually fails (not cancelled) showToast("Error", "Failed to open share options", "error"); } }; const handleActivityPress = () => { router.push(ROUTES.POINTS_ACTIVITY); }; const handleHowToEarnPress = () => { // Navigate to how-to-earn details when available }; const handleRedeem = (rewardId: string) => { // Integrate redeem API later }; return ( {t("points.title")} {/* Total Points Card */} Your Points {pointsState?.total ?? 0} {t("points.referTitle")} {t("points.earnSubtitle")} {REFERRAL_LINK} {t("points.copyButton")} {t("points.activityButton")} {t("points.howToEarnButton")} {/* Rewards section can be wired to real redemption in the future */} ); }