"use client"; import { RiCalendarCheckLine, RiLoader4Line } from "@remixicon/react"; import { useCallback, useEffect, useState } from "react"; import { toast } from "sonner"; import { getInstallmentAnticipationsAction } from "@/app/(dashboard)/lancamentos/anticipation-actions"; import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, DialogTrigger, } from "@/components/ui/dialog"; import { Empty, EmptyDescription, EmptyHeader, EmptyMedia, EmptyTitle, } from "@/components/ui/empty"; import { useControlledState } from "@/hooks/use-controlled-state"; import type { InstallmentAnticipationWithRelations } from "@/lib/installments/anticipation-types"; import { AnticipationCard } from "../../shared/anticipation-card"; interface AnticipationHistoryDialogProps { trigger?: React.ReactNode; seriesId: string; lancamentoName: string; open?: boolean; onOpenChange?: (open: boolean) => void; onViewLancamento?: (lancamentoId: string) => void; } export function AnticipationHistoryDialog({ trigger, seriesId, lancamentoName, open, onOpenChange, onViewLancamento, }: AnticipationHistoryDialogProps) { const [isLoading, setIsLoading] = useState(false); const [anticipations, setAnticipations] = useState< InstallmentAnticipationWithRelations[] >([]); // Use controlled state hook for dialog open state const [dialogOpen, setDialogOpen] = useControlledState( open, false, onOpenChange, ); // Define loadAnticipations before it's used in useEffect const loadAnticipations = useCallback(async () => { setIsLoading(true); try { const result = await getInstallmentAnticipationsAction(seriesId); if (result.success && result.data) { setAnticipations(result.data); } else { toast.error( result.error || "Erro ao carregar histórico de antecipações", ); setAnticipations([]); } } catch (error) { console.error("Erro ao buscar antecipações:", error); toast.error("Erro ao carregar histórico de antecipações"); setAnticipations([]); } finally { setIsLoading(false); } }, [seriesId]); // Buscar antecipações ao abrir o dialog useEffect(() => { if (dialogOpen) { loadAnticipations(); } }, [dialogOpen, loadAnticipations]); const handleCanceled = () => { // Recarregar lista após cancelamento loadAnticipations(); }; return ( {trigger && {trigger}} Histórico de Antecipações {lancamentoName}
{isLoading ? (
Carregando histórico...
) : anticipations.length === 0 ? ( Nenhuma antecipação registrada As antecipações realizadas para esta compra parcelada aparecerão aqui. ) : ( anticipations.map((anticipation) => ( )) )}
{!isLoading && anticipations.length > 0 && (
{anticipations.length}{" "} {anticipations.length === 1 ? "antecipação encontrada" : "antecipações encontradas"}
)}
); }