Co-authored-by: renee-png <262607627+renee-png@users.noreply.github.com>
This commit is contained in:
gpt-engineer-app[bot]
2026-04-19 18:22:44 +00:00
co-authored by renee-png
parent 04f36c6cab
commit 2a97b5f739
+36 -13
View File
@@ -6,10 +6,10 @@ import { Label } from "@/components/ui/label";
import { Textarea } from "@/components/ui/textarea";
import { supabase } from "@/integrations/supabase/client";
import { useAuth } from "@/lib/auth";
import { Plus, Trash2, Loader2, FileDown, Pencil } from "lucide-react";
import { Plus, Trash2, Loader2, FileDown, Pencil, Save } from "lucide-react";
import { formatDateTime } from "@/lib/format";
import { toast } from "sonner";
import { downloadStatusReport } from "@/lib/status-pdf";
import { downloadStatusReport, saveStatusReportToDb } from "@/lib/status-pdf";
export function CaseStatusTab({ caseId, caseLabel }: { caseId: string; caseLabel?: string }) {
const { user, isAdmin } = useAuth();
@@ -79,24 +79,47 @@ export function CaseStatusTab({ caseId, caseLabel }: { caseId: string; caseLabel
if (error) toast.error(error.message); else { toast.success("Deleted"); load(); }
};
const buildReportOpts = () => ({
title: "Case Status Report",
subtitle: caseLabel,
entries: items.map((u) => ({
title: u.title,
body: u.body,
created_at: u.created_at,
author_name: u.user?.full_name,
author_email: u.user?.email,
})),
});
const exportPdf = () => {
if (items.length === 0) { toast.error("No status updates to export"); return; }
downloadStatusReport({
title: "Case Status Report",
subtitle: caseLabel,
entries: items.map((u) => ({
title: u.title,
body: u.body,
created_at: u.created_at,
author_name: u.user?.full_name,
author_email: u.user?.email,
})),
}, `status-report-${(caseLabel || caseId).replace(/[^a-z0-9]+/gi, "-")}.pdf`);
downloadStatusReport(
buildReportOpts(),
`status-report-${(caseLabel || caseId).replace(/[^a-z0-9]+/gi, "-")}.pdf`,
);
};
const saveReport = async () => {
if (items.length === 0) { toast.error("No status updates to save"); return; }
if (!user?.id) { toast.error("Not signed in"); return; }
const filename = `status-report-${(caseLabel || caseId).replace(/[^a-z0-9]+/gi, "-")}-${new Date().toISOString().slice(0, 10)}.pdf`;
try {
await saveStatusReportToDb(buildReportOpts(), filename, {
userId: user.id,
payload: { case_id: caseId, case_label: caseLabel ?? null, entry_count: items.length, scope: "case" },
});
toast.success("Saved to Reports");
} catch (err: any) {
toast.error(err?.message || "Could not save report");
}
};
return (
<div className="space-y-4">
<div className="flex justify-end gap-2">
<Button variant="outline" onClick={saveReport} disabled={items.length === 0}>
<Save className="h-4 w-4 mr-2" /> Save to Reports
</Button>
<Button variant="outline" onClick={exportPdf} disabled={items.length === 0}>
<FileDown className="h-4 w-4 mr-2" /> Export PDF
</Button>