import React, { useState, useEffect, useCallback } from "react"; import { View, Pressable, ActivityIndicator, FlatList, ListRenderItem, } from "react-native"; import { Text } from "@/components/ui/text"; import { Card } from "@/components/ui/card"; import { useSirouRouter } from "@sirou/react-native"; import { AppRoutes } from "@/lib/routes"; import { Plus, Send, FileText, Clock } from "@/lib/icons"; import { ScreenWrapper } from "@/components/ScreenWrapper"; import { StandardHeader } from "@/components/StandardHeader"; import { Button } from "@/components/ui/button"; import { api } from "@/lib/api"; import { useAuthStore } from "@/lib/auth-store"; interface ProformaItem { id: string; proformaNumber: string; customerName: string; amount: any; currency: string; issueDate: string; dueDate: string; description: string; } export default function ProformaScreen() { const nav = useSirouRouter(); const [proformas, setProformas] = useState([]); const [loading, setLoading] = useState(true); const [refreshing, setRefreshing] = useState(false); const [page, setPage] = useState(1); const [hasMore, setHasMore] = useState(true); const [loadingMore, setLoadingMore] = useState(false); const fetchProformas = useCallback( async (pageNum: number, isRefresh = false) => { const { isAuthenticated } = useAuthStore.getState(); if (!isAuthenticated) return; try { if (!isRefresh) { pageNum === 1 ? setLoading(true) : setLoadingMore(true); } const response = await api.proforma.getAll({ query: { page: pageNum, limit: 10 }, }); const newData = response.data; if (isRefresh) { setProformas(newData); } else { setProformas((prev) => pageNum === 1 ? newData : [...prev, ...newData], ); } setHasMore(response.meta.hasNextPage); setPage(pageNum); } catch (err: any) { console.error("[Proforma] Fetch error:", err); } finally { setLoading(false); setRefreshing(false); setLoadingMore(false); } }, [], ); useEffect(() => { fetchProformas(1); }, [fetchProformas]); const onRefresh = () => { setRefreshing(true); fetchProformas(1, true); }; const loadMore = () => { if (hasMore && !loadingMore && !loading) { fetchProformas(page + 1); } }; const renderProformaItem: ListRenderItem = ({ item }) => { const amountVal = typeof item.amount === "object" ? item.amount.value : item.amount; const dateStr = new Date(item.issueDate).toLocaleDateString(); return ( nav.go("proforma/[id]", { id: item.id })} className="mb-3" > {item.currency || "$"} {amountVal?.toLocaleString()} {item.proformaNumber} {item.customerName} {item.description && ( {item.description} )} Issued: {dateStr} { e.stopPropagation(); // Handle share }} > Share ); }; return ( item.id} contentContainerStyle={{ padding: 20, paddingBottom: 150 }} showsVerticalScrollIndicator={false} onRefresh={onRefresh} refreshing={refreshing} onEndReached={loadMore} onEndReachedThreshold={0.5} ListHeaderComponent={ } ListFooterComponent={ loadingMore ? ( ) : null } ListEmptyComponent={ !loading ? ( No proformas found ) : ( ) } /> ); }