"use client"; import { useRouter } from "next/navigation"; import { useActionState, useEffect } from "react"; import { useTranslations } from "next-intl"; import { CheckCircle2, Clock, Info, Mail, XCircle } from "lucide-react"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Textarea } from "@/components/ui/textarea"; import { sendReportToCustomerAction } from "@/server/actions/reports/send-to-customer"; import { SEND_TO_CUSTOMER_IDLE } from "./send-to-customer-state"; export type CustomerMailingRow = { id: string; to: string; status: string; version: number | null; when: string }; const KNOWN_REASONS = new Set(["recipient_missing", "report_not_approved", "pdf_missing"]); /** /reports/[id] section „An Kunden senden" (approved + report:approve). The service enforces rights and status. */ export function SendToCustomer({ reportId, defaultTo, mailings }: { reportId: string; defaultTo: string | null; mailings: CustomerMailingRow[] }) { const t = useTranslations("reports.customerMail"); const tr = useTranslations("reports"); const router = useRouter(); const [state, action, pending] = useActionState(sendReportToCustomerAction, SEND_TO_CUSTOMER_IDLE); useEffect(() => { if (state.status === "ok") router.refresh(); }, [state, router]); let feedback: React.ReactNode = null; if (state.status === "ok") { const bad = state.result === "failed"; const hint = state.result === "duplicate"; feedback = (

{bad ? : hint ? : } {t(`result.${state.result}`, { to: state.to })}

); } else if (state.status === "error") { const key = state.reason && KNOWN_REASONS.has(state.reason) ? `errors.${state.reason}` : state.field === "to" ? "errors.invalid_to" : null; feedback = (

{key ? t(key) : tr(`errors.${state.code}`)}

); } return (

{t("title")}

{t("sub")}

{defaultTo ? t("toHint") : t("noDefault")}